"""add_coverage_snapshots

Revision ID: b015snapshots
Revises: b014compliance
Create Date: 2026-02-10 00:00:00.000000

"""
from typing import Sequence, Union

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

# revision identifiers, used by Alembic.
revision: str = "b015snapshots"
down_revision: Union[str, None] = "b014compliance"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ── coverage_snapshots ────────────────────────────────────────
    op.create_table(
        "coverage_snapshots",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column("name", sa.String, nullable=True),
        sa.Column("organization_score", sa.Float, nullable=False),
        sa.Column("total_techniques", sa.Integer, nullable=False),
        sa.Column("validated_count", sa.Integer, nullable=False),
        sa.Column("partial_count", sa.Integer, nullable=False),
        sa.Column("not_covered_count", sa.Integer, nullable=False),
        sa.Column("in_progress_count", sa.Integer, nullable=False),
        sa.Column("not_evaluated_count", sa.Integer, nullable=False),
        sa.Column(
            "created_by",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("users.id", ondelete="SET NULL"),
            nullable=True,
        ),
        sa.Column("created_at", sa.DateTime, server_default=sa.func.now()),
    )

    # ── snapshot_technique_states ─────────────────────────────────
    op.create_table(
        "snapshot_technique_states",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column(
            "snapshot_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("coverage_snapshots.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column(
            "technique_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("techniques.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column("mitre_id", sa.String, nullable=False),
        sa.Column("status", sa.String, nullable=False),
        sa.Column("score", sa.Float, nullable=True),
    )
    op.create_index(
        "ix_snapshot_technique_states_snapshot",
        "snapshot_technique_states",
        ["snapshot_id"],
    )
    op.create_index(
        "ix_snapshot_technique_states_technique",
        "snapshot_technique_states",
        ["technique_id"],
    )


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