"""two-dimension service catalog (STATE-WP-0062)

Creates service_catalog + per-dimension extension tables and migrates existing
tpsc_catalog rows into it as (cloud_hosted, third_party). The service_catalog id
reuses the tpsc_catalog id so existing tpsc_entries.catalog_id values continue to
resolve against the new core table without a column change.

Revision ID: c7d8e9f0a1b2
Revises: 5733434addf4
Create Date: 2026-06-19

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import JSON, UUID

revision = "c7d8e9f0a1b2"
down_revision = "5733434addf4"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "service_catalog",
        sa.Column("id", UUID(as_uuid=True), primary_key=True),
        sa.Column("slug", sa.String(length=100), nullable=False),
        sa.Column("name", sa.String(length=200), nullable=False),
        sa.Column("owner_or_provider", sa.String(length=200), nullable=True),
        sa.Column("category", sa.String(length=100), nullable=True),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("website_url", sa.Text(), nullable=True),
        sa.Column("status", sa.String(length=20), server_default="active", nullable=False),
        sa.Column("hosting_type", sa.String(length=20), nullable=False),
        sa.Column("development_type", sa.String(length=20), nullable=False),
        sa.Column("maturity_level", sa.Integer(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.UniqueConstraint("slug", name="uq_service_catalog_slug"),
    )
    op.create_index("ix_service_catalog_slug", "service_catalog", ["slug"])
    op.create_index("ix_service_catalog_hosting_type", "service_catalog", ["hosting_type"])
    op.create_index("ix_service_catalog_development_type", "service_catalog", ["development_type"])

    op.create_table(
        "service_third_party",
        sa.Column("service_id", UUID(as_uuid=True),
                  sa.ForeignKey("service_catalog.id", ondelete="CASCADE"), primary_key=True),
        sa.Column("pricing_model", sa.String(length=20), server_default="unknown", nullable=False),
        sa.Column("upstream_packages", JSON(), nullable=True),
        sa.Column("upstream_contacts", JSON(), nullable=True),
        sa.Column("source_url", sa.Text(), nullable=True),
        sa.Column("support_url", sa.Text(), nullable=True),
        sa.Column("license", sa.String(length=100), nullable=True),
    )

    op.create_table(
        "service_first_party",
        sa.Column("service_id", UUID(as_uuid=True),
                  sa.ForeignKey("service_catalog.id", ondelete="CASCADE"), primary_key=True),
        sa.Column("repo_id", UUID(as_uuid=True),
                  sa.ForeignKey("managed_repos.id", ondelete="SET NULL"), nullable=True),
        sa.Column("owning_domain", sa.String(length=100), nullable=True),
    )
    op.create_index("ix_service_first_party_repo_id", "service_first_party", ["repo_id"])

    op.create_table(
        "service_cloud",
        sa.Column("service_id", UUID(as_uuid=True),
                  sa.ForeignKey("service_catalog.id", ondelete="CASCADE"), primary_key=True),
        sa.Column("gdpr_maturity", sa.String(length=20), server_default="unknown", nullable=False),
        sa.Column("gdpr_notes", sa.Text(), nullable=True),
        sa.Column("dpa_available", sa.Boolean(), server_default=sa.text("false"), nullable=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),
    )
    op.create_index("ix_service_cloud_gdpr_maturity", "service_cloud", ["gdpr_maturity"])

    op.create_table(
        "service_self_hosted",
        sa.Column("service_id", UUID(as_uuid=True),
                  sa.ForeignKey("service_catalog.id", ondelete="CASCADE"), primary_key=True),
        sa.Column("helix_instance", sa.String(length=100), nullable=True),
        sa.Column("host_node", sa.String(length=100), nullable=True),
        sa.Column("deployment_ref", sa.Text(), nullable=True),
        sa.Column("runbook_ref", sa.Text(), nullable=True),
        sa.Column("upstream_oss_project", sa.String(length=200), nullable=True),
    )

    # ── Data migration: tpsc_catalog → service_catalog (cloud_hosted, third_party)
    op.execute(
        """
        INSERT INTO service_catalog
            (id, slug, name, owner_or_provider, category, website_url, status,
             hosting_type, development_type, maturity_level, created_at, updated_at)
        SELECT id, slug, name, provider, category, website_url, status,
               'cloud_hosted', 'third_party', NULL, created_at, updated_at
        FROM tpsc_catalog
        """
    )
    op.execute(
        """
        INSERT INTO service_third_party (service_id, pricing_model)
        SELECT id, pricing_model FROM tpsc_catalog
        """
    )
    op.execute(
        """
        INSERT INTO service_cloud
            (service_id, gdpr_maturity, gdpr_notes, dpa_available, tos_url,
             privacy_policy_url, data_processing_regions, data_retention_notes)
        SELECT id, gdpr_maturity, gdpr_notes, dpa_available, tos_url,
               privacy_policy_url, data_processing_regions, data_retention_notes
        FROM tpsc_catalog
        """
    )


def downgrade() -> None:
    op.drop_index("ix_service_cloud_gdpr_maturity", table_name="service_cloud")
    op.drop_table("service_self_hosted")
    op.drop_table("service_cloud")
    op.drop_index("ix_service_first_party_repo_id", table_name="service_first_party")
    op.drop_table("service_first_party")
    op.drop_table("service_third_party")
    op.drop_index("ix_service_catalog_development_type", table_name="service_catalog")
    op.drop_index("ix_service_catalog_hosting_type", table_name="service_catalog")
    op.drop_index("ix_service_catalog_slug", table_name="service_catalog")
    op.drop_table("service_catalog")
