"""GEMS Pass 3: add sbom_snapshots container entity

Revision ID: a3b4c5d6e7f8
Revises: f2a3b4c5d6e7
Create Date: 2026-03-02 00:00:00.000000
"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

revision: str = "a3b4c5d6e7f8"
down_revision: Union[str, None] = "f2a3b4c5d6e7"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ── Create sbom_snapshots table ────────────────────────────────────────────
    op.create_table(
        "sbom_snapshots",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column(
            "repo_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("managed_repos.id", ondelete="RESTRICT"),
            nullable=False,
            index=True,
        ),
        sa.Column("snapshot_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("source", sa.String(200), nullable=True),
        sa.Column("entry_count", sa.Integer, nullable=False, server_default="0"),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
    )
    # ── Add snapshot_id FK to sbom_entries (nullable during backfill) ──────────
    op.add_column(
        "sbom_entries",
        sa.Column("snapshot_id", postgresql.UUID(as_uuid=True), nullable=True),
    )
    op.create_foreign_key(
        "fk_sbom_entry_snapshot_id",
        "sbom_entries", "sbom_snapshots",
        ["snapshot_id"], ["id"],
        ondelete="RESTRICT",
    )

    # ── Backfill: create one snapshot per (repo_id, snapshot_at) group ─────────
    op.execute("""
        INSERT INTO sbom_snapshots (id, repo_id, snapshot_at, source, entry_count, created_at)
        SELECT
            gen_random_uuid(),
            repo_id,
            snapshot_at,
            'backfill' AS source,
            COUNT(*) AS entry_count,
            MIN(created_at) AS created_at
        FROM sbom_entries
        GROUP BY repo_id, snapshot_at
    """)

    # ── Assign snapshot_id to each entry ───────────────────────────────────────
    op.execute("""
        UPDATE sbom_entries e
        SET snapshot_id = s.id
        FROM sbom_snapshots s
        WHERE s.repo_id = e.repo_id
          AND s.snapshot_at = e.snapshot_at
    """)

    # ── Make snapshot_id NOT NULL ──────────────────────────────────────────────
    op.execute("""
        DO $$
        BEGIN
            IF EXISTS (SELECT 1 FROM sbom_entries WHERE snapshot_id IS NULL) THEN
                RAISE EXCEPTION 'GEMS Pass 3: sbom_entries rows with no snapshot assigned';
            END IF;
        END $$;
    """)
    op.alter_column("sbom_entries", "snapshot_id", nullable=False)
    op.create_index("ix_sbom_entries_snapshot_id", "sbom_entries", ["snapshot_id"])


def downgrade() -> None:
    op.drop_index("ix_sbom_entries_snapshot_id", table_name="sbom_entries")
    op.drop_constraint("fk_sbom_entry_snapshot_id", "sbom_entries", type_="foreignkey")
    op.drop_column("sbom_entries", "snapshot_id")
    op.drop_table("sbom_snapshots")
