"""tpsc: third-party services catalog

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

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


def upgrade() -> None:
    op.create_table(
        "tpsc_catalog",
        sa.Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4),
        sa.Column("slug", sa.String(100), nullable=False, unique=True),
        sa.Column("name", sa.String(200), nullable=False),
        sa.Column("provider", sa.String(200), nullable=True),
        sa.Column("category", sa.String(100), nullable=True),
        sa.Column("website_url", sa.Text, nullable=True),
        sa.Column("pricing_model", sa.String(20), nullable=False, server_default="unknown"),
        sa.Column("gdpr_maturity", sa.String(20), nullable=False, server_default="unknown"),
        sa.Column("gdpr_notes", sa.Text, nullable=True),
        sa.Column("dpa_available", sa.Boolean, nullable=False, server_default="false"),
        sa.Column("tos_url", sa.Text, nullable=True),
        sa.Column("privacy_policy_url", sa.Text, nullable=True),
        sa.Column("data_processing_regions", JSON, nullable=True),
        sa.Column("data_retention_notes", sa.Text, nullable=True),
        sa.Column("status", sa.String(20), nullable=False, server_default="active"),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now()),
    )
    op.create_index("ix_tpsc_catalog_slug", "tpsc_catalog", ["slug"])
    op.create_index("ix_tpsc_catalog_gdpr_maturity", "tpsc_catalog", ["gdpr_maturity"])

    op.create_table(
        "tpsc_snapshots",
        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="SET NULL"), nullable=True),
        sa.Column("snapshot_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
        sa.Column("source_file", sa.String(200), nullable=True),
        sa.Column("entry_count", sa.Integer, nullable=False, server_default="0"),
    )
    op.create_index("ix_tpsc_snapshots_repo_id", "tpsc_snapshots", ["repo_id"])

    op.create_table(
        "tpsc_entries",
        sa.Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4),
        sa.Column("snapshot_id", UUID(as_uuid=True), sa.ForeignKey("tpsc_snapshots.id", ondelete="CASCADE"), nullable=False),
        sa.Column("catalog_id", UUID(as_uuid=True), sa.ForeignKey("tpsc_catalog.id", ondelete="SET NULL"), nullable=True),
        sa.Column("service_slug", sa.String(100), nullable=False),
        sa.Column("purpose", sa.Text, nullable=True),
        sa.Column("auth_type", sa.String(50), nullable=True),
        sa.Column("endpoint_override", sa.Text, nullable=True),
        sa.Column("notes", sa.Text, nullable=True),
    )
    op.create_index("ix_tpsc_entries_snapshot_id", "tpsc_entries", ["snapshot_id"])
    op.create_index("ix_tpsc_entries_service_slug", "tpsc_entries", ["service_slug"])


def downgrade() -> None:
    op.drop_table("tpsc_entries")
    op.drop_table("tpsc_snapshots")
    op.drop_table("tpsc_catalog")
