"""doi_cache: materialised DoI results with fingerprint-based invalidation

Revision ID: k8f9a0b1c2d3
Revises: j7e8f9a0b1c2
Create Date: 2026-03-20
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import UUID, JSON
import uuid

revision = "k8f9a0b1c2d3"
down_revision = "j7e8f9a0b1c2"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "doi_cache",
        sa.Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4),
        sa.Column("repo_id", UUID(as_uuid=True),
                  sa.ForeignKey("managed_repos.id", ondelete="CASCADE"),
                  nullable=False, unique=True),
        sa.Column("tier", sa.String(20), nullable=False),
        sa.Column("core_pass", sa.Boolean, nullable=False, server_default="false"),
        sa.Column("standard_pass", sa.Boolean, nullable=False, server_default="false"),
        sa.Column("full_pass", sa.Boolean, nullable=False, server_default="false"),
        sa.Column("criteria", JSON, nullable=True),      # full criterion list
        sa.Column("fingerprint", sa.Text, nullable=False),  # pipe-joined timestamps
        sa.Column("checked_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
    )
    op.create_index("ix_doi_cache_repo_id", "doi_cache", ["repo_id"])


def downgrade() -> None:
    op.drop_table("doi_cache")
