"""extend Fabric graph read model for financial Fabric vNext

Revision ID: z3u4v5w6x7y8
Revises: y2t3u4v5w6x7
Create Date: 2026-05-24

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

revision = "z3u4v5w6x7y8"
down_revision = "y2t3u4v5w6x7"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.add_column("fabric_graph_imports", sa.Column("schema_version", sa.String(length=100), nullable=True))
    op.add_column("fabric_graph_imports", sa.Column("netkingdom_id", sa.Text(), nullable=True))
    op.add_column("fabric_graph_imports", sa.Column("king_actor_id", sa.Text(), nullable=True))
    op.add_column("fabric_graph_imports", sa.Column("actor_count", sa.Integer(), nullable=False, server_default="0"))
    op.add_column("fabric_graph_imports", sa.Column("fabric_count", sa.Integer(), nullable=False, server_default="0"))
    op.add_column("fabric_graph_imports", sa.Column("unresolved_count", sa.Integer(), nullable=False, server_default="0"))
    op.add_column("fabric_graph_imports", sa.Column("compatibility", JSONB(), nullable=False, server_default="{}"))
    op.create_index("ix_fabric_graph_imports_schema_version", "fabric_graph_imports", ["schema_version"])

    op.add_column("fabric_graph_nodes", sa.Column("evidence_review_state", sa.String(length=20), nullable=True))
    op.add_column("fabric_graph_nodes", sa.Column("evidence_confidence", sa.Float(), nullable=True))
    op.add_column("fabric_graph_nodes", sa.Column("netkingdom_id", sa.Text(), nullable=True))
    op.add_column("fabric_graph_nodes", sa.Column("fabric_id", sa.Text(), nullable=True))
    op.add_column("fabric_graph_nodes", sa.Column("subfabric_id", sa.Text(), nullable=True))
    op.add_column("fabric_graph_nodes", sa.Column("environment", sa.String(length=100), nullable=True))
    op.add_column("fabric_graph_nodes", sa.Column("deployment_scenario_id", sa.Text(), nullable=True))
    op.add_column("fabric_graph_nodes", sa.Column("owner_actor_id", sa.Text(), nullable=True))
    op.add_column("fabric_graph_nodes", sa.Column("owner_role", sa.String(length=20), nullable=True))
    op.add_column("fabric_graph_nodes", sa.Column("ownership_resolution", sa.String(length=20), nullable=True))
    op.add_column("fabric_graph_nodes", sa.Column("cost_center_id", sa.Text(), nullable=True))
    op.add_column("fabric_graph_nodes", sa.Column("profit_center_id", sa.Text(), nullable=True))
    op.create_index("ix_fabric_graph_nodes_evidence_review_state", "fabric_graph_nodes", ["evidence_review_state"])
    op.create_index("ix_fabric_graph_nodes_fabric_id", "fabric_graph_nodes", ["fabric_id"])
    op.create_index("ix_fabric_graph_nodes_subfabric_id", "fabric_graph_nodes", ["subfabric_id"])
    op.create_index("ix_fabric_graph_nodes_environment", "fabric_graph_nodes", ["environment"])
    op.create_index("ix_fabric_graph_nodes_owner_actor_id", "fabric_graph_nodes", ["owner_actor_id"])
    op.create_index("ix_fabric_graph_nodes_owner_role", "fabric_graph_nodes", ["owner_role"])
    op.create_index("ix_fabric_graph_nodes_ownership_resolution", "fabric_graph_nodes", ["ownership_resolution"])
    op.create_index("ix_fabric_graph_nodes_cost_center_id", "fabric_graph_nodes", ["cost_center_id"])
    op.create_index("ix_fabric_graph_nodes_profit_center_id", "fabric_graph_nodes", ["profit_center_id"])

    op.add_column("fabric_graph_edges", sa.Column("evidence_review_state", sa.String(length=20), nullable=True))
    op.add_column("fabric_graph_edges", sa.Column("evidence_confidence", sa.Float(), nullable=True))
    op.add_column("fabric_graph_edges", sa.Column("relationship_category", sa.String(length=30), nullable=True))
    op.add_column("fabric_graph_edges", sa.Column("provider_owner_actor_id", sa.Text(), nullable=True))
    op.add_column("fabric_graph_edges", sa.Column("provider_fabric_id", sa.Text(), nullable=True))
    op.add_column("fabric_graph_edges", sa.Column("provider_subfabric_id", sa.Text(), nullable=True))
    op.add_column("fabric_graph_edges", sa.Column("consumer_owner_actor_id", sa.Text(), nullable=True))
    op.add_column("fabric_graph_edges", sa.Column("consumer_fabric_id", sa.Text(), nullable=True))
    op.add_column("fabric_graph_edges", sa.Column("consumer_subfabric_id", sa.Text(), nullable=True))
    op.add_column("fabric_graph_edges", sa.Column("crosses_fabric_boundary", sa.Boolean(), nullable=True))
    op.add_column("fabric_graph_edges", sa.Column("crosses_subfabric_boundary", sa.Boolean(), nullable=True))
    op.add_column("fabric_graph_edges", sa.Column("utility_type", sa.String(length=100), nullable=True))
    op.add_column("fabric_graph_edges", sa.Column("utility_contract_id", sa.Text(), nullable=True))
    op.add_column("fabric_graph_edges", sa.Column("utility_payment_schema_id", sa.Text(), nullable=True))
    op.add_column("fabric_graph_edges", sa.Column("utility_metering_basis", sa.String(length=100), nullable=True))
    op.add_column("fabric_graph_edges", sa.Column("utility_business_model", sa.String(length=100), nullable=True))
    op.add_column("fabric_graph_edges", sa.Column("cost_center_id", sa.Text(), nullable=True))
    op.add_column("fabric_graph_edges", sa.Column("profit_center_id", sa.Text(), nullable=True))
    op.add_column("fabric_graph_edges", sa.Column("provider_profit_center_id", sa.Text(), nullable=True))
    op.add_column("fabric_graph_edges", sa.Column("consumer_cost_center_id", sa.Text(), nullable=True))
    op.create_index("ix_fabric_graph_edges_evidence_review_state", "fabric_graph_edges", ["evidence_review_state"])
    op.create_index("ix_fabric_graph_edges_relationship_category", "fabric_graph_edges", ["relationship_category"])
    op.create_index("ix_fabric_graph_edges_provider_owner_actor_id", "fabric_graph_edges", ["provider_owner_actor_id"])
    op.create_index("ix_fabric_graph_edges_provider_fabric_id", "fabric_graph_edges", ["provider_fabric_id"])
    op.create_index("ix_fabric_graph_edges_provider_subfabric_id", "fabric_graph_edges", ["provider_subfabric_id"])
    op.create_index("ix_fabric_graph_edges_consumer_owner_actor_id", "fabric_graph_edges", ["consumer_owner_actor_id"])
    op.create_index("ix_fabric_graph_edges_consumer_fabric_id", "fabric_graph_edges", ["consumer_fabric_id"])
    op.create_index("ix_fabric_graph_edges_consumer_subfabric_id", "fabric_graph_edges", ["consumer_subfabric_id"])
    op.create_index("ix_fabric_graph_edges_crosses_fabric_boundary", "fabric_graph_edges", ["crosses_fabric_boundary"])
    op.create_index("ix_fabric_graph_edges_crosses_subfabric_boundary", "fabric_graph_edges", ["crosses_subfabric_boundary"])
    op.create_index("ix_fabric_graph_edges_utility_type", "fabric_graph_edges", ["utility_type"])
    op.create_index("ix_fabric_graph_edges_utility_payment_schema_id", "fabric_graph_edges", ["utility_payment_schema_id"])
    op.create_index("ix_fabric_graph_edges_utility_business_model", "fabric_graph_edges", ["utility_business_model"])
    op.create_index("ix_fabric_graph_edges_cost_center_id", "fabric_graph_edges", ["cost_center_id"])
    op.create_index("ix_fabric_graph_edges_profit_center_id", "fabric_graph_edges", ["profit_center_id"])
    op.create_index("ix_fabric_graph_edges_provider_profit_center_id", "fabric_graph_edges", ["provider_profit_center_id"])
    op.create_index("ix_fabric_graph_edges_consumer_cost_center_id", "fabric_graph_edges", ["consumer_cost_center_id"])


def downgrade() -> None:
    op.drop_index("ix_fabric_graph_edges_consumer_cost_center_id", table_name="fabric_graph_edges")
    op.drop_index("ix_fabric_graph_edges_provider_profit_center_id", table_name="fabric_graph_edges")
    op.drop_index("ix_fabric_graph_edges_profit_center_id", table_name="fabric_graph_edges")
    op.drop_index("ix_fabric_graph_edges_cost_center_id", table_name="fabric_graph_edges")
    op.drop_index("ix_fabric_graph_edges_utility_business_model", table_name="fabric_graph_edges")
    op.drop_index("ix_fabric_graph_edges_utility_payment_schema_id", table_name="fabric_graph_edges")
    op.drop_index("ix_fabric_graph_edges_utility_type", table_name="fabric_graph_edges")
    op.drop_index("ix_fabric_graph_edges_crosses_subfabric_boundary", table_name="fabric_graph_edges")
    op.drop_index("ix_fabric_graph_edges_crosses_fabric_boundary", table_name="fabric_graph_edges")
    op.drop_index("ix_fabric_graph_edges_consumer_subfabric_id", table_name="fabric_graph_edges")
    op.drop_index("ix_fabric_graph_edges_consumer_fabric_id", table_name="fabric_graph_edges")
    op.drop_index("ix_fabric_graph_edges_consumer_owner_actor_id", table_name="fabric_graph_edges")
    op.drop_index("ix_fabric_graph_edges_provider_subfabric_id", table_name="fabric_graph_edges")
    op.drop_index("ix_fabric_graph_edges_provider_fabric_id", table_name="fabric_graph_edges")
    op.drop_index("ix_fabric_graph_edges_provider_owner_actor_id", table_name="fabric_graph_edges")
    op.drop_index("ix_fabric_graph_edges_relationship_category", table_name="fabric_graph_edges")
    op.drop_index("ix_fabric_graph_edges_evidence_review_state", table_name="fabric_graph_edges")
    for column in (
        "consumer_cost_center_id",
        "provider_profit_center_id",
        "profit_center_id",
        "cost_center_id",
        "utility_business_model",
        "utility_metering_basis",
        "utility_payment_schema_id",
        "utility_contract_id",
        "utility_type",
        "crosses_subfabric_boundary",
        "crosses_fabric_boundary",
        "consumer_subfabric_id",
        "consumer_fabric_id",
        "consumer_owner_actor_id",
        "provider_subfabric_id",
        "provider_fabric_id",
        "provider_owner_actor_id",
        "relationship_category",
        "evidence_confidence",
        "evidence_review_state",
    ):
        op.drop_column("fabric_graph_edges", column)

    op.drop_index("ix_fabric_graph_nodes_profit_center_id", table_name="fabric_graph_nodes")
    op.drop_index("ix_fabric_graph_nodes_cost_center_id", table_name="fabric_graph_nodes")
    op.drop_index("ix_fabric_graph_nodes_ownership_resolution", table_name="fabric_graph_nodes")
    op.drop_index("ix_fabric_graph_nodes_owner_role", table_name="fabric_graph_nodes")
    op.drop_index("ix_fabric_graph_nodes_owner_actor_id", table_name="fabric_graph_nodes")
    op.drop_index("ix_fabric_graph_nodes_environment", table_name="fabric_graph_nodes")
    op.drop_index("ix_fabric_graph_nodes_subfabric_id", table_name="fabric_graph_nodes")
    op.drop_index("ix_fabric_graph_nodes_fabric_id", table_name="fabric_graph_nodes")
    op.drop_index("ix_fabric_graph_nodes_evidence_review_state", table_name="fabric_graph_nodes")
    for column in (
        "profit_center_id",
        "cost_center_id",
        "ownership_resolution",
        "owner_role",
        "owner_actor_id",
        "deployment_scenario_id",
        "environment",
        "subfabric_id",
        "fabric_id",
        "netkingdom_id",
        "evidence_confidence",
        "evidence_review_state",
    ):
        op.drop_column("fabric_graph_nodes", column)

    op.drop_index("ix_fabric_graph_imports_schema_version", table_name="fabric_graph_imports")
    for column in (
        "compatibility",
        "unresolved_count",
        "fabric_count",
        "actor_count",
        "king_actor_id",
        "netkingdom_id",
        "schema_version",
    ):
        op.drop_column("fabric_graph_imports", column)
