"""Commercial Runtime tables

Revision ID: 0007_commercial_runtime
Revises: 0006_platform_catalog_seed
Create Date: 2026-07-28
"""
from __future__ import annotations

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

revision: str = "0007_commercial_runtime"
down_revision: Union[str, None] = "0006_platform_catalog_seed"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "commercial_registry_kinds",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("kind", sa.String(length=100), nullable=False),
        sa.Column("path", sa.String(length=100), nullable=False),
        sa.Column("display_name", sa.String(length=200), nullable=False),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("code_field", sa.String(length=100), nullable=False),
        sa.Column("list_envelope", sa.String(length=100), nullable=False),
        sa.Column("public_read", sa.Boolean(), nullable=False, server_default=sa.text("true")),
        sa.Column("enabled", sa.Boolean(), nullable=False, server_default=sa.text("true")),
        sa.Column("sort_order", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("metadata", sa.JSON(), 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.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("kind"),
        sa.UniqueConstraint("path"),
    )

    op.create_table(
        "commercial_registry_objects",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("kind", sa.String(length=100), nullable=False),
        sa.Column("slug", sa.String(length=200), nullable=False),
        sa.Column("stable_code", sa.String(length=200), nullable=False),
        sa.Column("version_code", sa.String(length=64), nullable=False),
        sa.Column("display_name", sa.String(length=300), nullable=False),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("status", sa.String(length=40), nullable=False),
        sa.Column("visibility", sa.String(length=40), nullable=False),
        sa.Column("tenant_id", sa.Uuid(), nullable=True),
        sa.Column("payload", sa.JSON(), nullable=False),
        sa.Column("metadata", sa.JSON(), nullable=False),
        sa.Column("created_by", sa.Uuid(), nullable=True),
        sa.Column("updated_by", sa.Uuid(), nullable=True),
        sa.Column("published_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("archived_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("is_deleted", sa.Boolean(), nullable=False, server_default=sa.text("false")),
        sa.Column("clone_of_id", sa.Uuid(), 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.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "kind",
            "stable_code",
            "version_code",
            "tenant_id",
            name="uq_commercial_obj_kind_code_ver_tenant",
        ),
    )
    op.create_index("ix_commercial_registry_objects_kind", "commercial_registry_objects", ["kind"])
    op.create_index("ix_commercial_registry_objects_status", "commercial_registry_objects", ["status"])
    op.create_index("ix_commercial_registry_objects_tenant_id", "commercial_registry_objects", ["tenant_id"])
    op.create_index("ix_commercial_registry_objects_is_deleted", "commercial_registry_objects", ["is_deleted"])
    op.create_index("ix_commercial_obj_kind_status", "commercial_registry_objects", ["kind", "status"])
    op.create_index("ix_commercial_obj_slug", "commercial_registry_objects", ["kind", "slug"])
    op.create_index("ix_commercial_obj_search", "commercial_registry_objects", ["kind", "display_name"])

    op.create_table(
        "commercial_tenant_subscriptions",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("tenant_id", sa.Uuid(), nullable=False),
        sa.Column("plan_code", sa.String(length=200), nullable=True),
        sa.Column("bundle_code", sa.String(length=200), nullable=True),
        sa.Column("pricing_item_code", sa.String(length=200), nullable=True),
        sa.Column("status", sa.String(length=40), nullable=False),
        sa.Column("trial_ends_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("current_period_start", sa.DateTime(timezone=True), nullable=True),
        sa.Column("current_period_end", sa.DateTime(timezone=True), nullable=True),
        sa.Column("cancel_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("metadata", sa.JSON(), nullable=False),
        sa.Column("created_by", sa.Uuid(), 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.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        "ix_commercial_tenant_subscriptions_tenant_id",
        "commercial_tenant_subscriptions",
        ["tenant_id"],
    )
    op.create_index(
        "ix_commercial_sub_tenant_status",
        "commercial_tenant_subscriptions",
        ["tenant_id", "status"],
    )

    op.create_table(
        "commercial_tenant_licenses",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("tenant_id", sa.Uuid(), nullable=False),
        sa.Column("license_code", sa.String(length=200), nullable=False),
        sa.Column("display_name", sa.String(length=300), nullable=True),
        sa.Column("status", sa.String(length=40), nullable=False),
        sa.Column("capability_codes", sa.JSON(), nullable=False),
        sa.Column("product_codes", sa.JSON(), nullable=False),
        sa.Column("bundle_code", sa.String(length=200), nullable=True),
        sa.Column("quota", sa.JSON(), nullable=False),
        sa.Column("metadata", sa.JSON(), nullable=False),
        sa.Column("expires_at", sa.DateTime(timezone=True), 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.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("tenant_id", "license_code", name="uq_commercial_license_tenant_code"),
    )
    op.create_index(
        "ix_commercial_tenant_licenses_tenant_id",
        "commercial_tenant_licenses",
        ["tenant_id"],
    )

    op.create_table(
        "commercial_activation_states",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("tenant_id", sa.Uuid(), nullable=False),
        sa.Column("status", sa.String(length=40), nullable=False),
        sa.Column("completion_percent", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("steps", sa.JSON(), nullable=False),
        sa.Column("checklist", sa.JSON(), nullable=False),
        sa.Column("metadata", sa.JSON(), nullable=False),
        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.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("tenant_id"),
    )


def downgrade() -> None:
    op.drop_table("commercial_activation_states")
    op.drop_index("ix_commercial_tenant_licenses_tenant_id", table_name="commercial_tenant_licenses")
    op.drop_table("commercial_tenant_licenses")
    op.drop_index("ix_commercial_sub_tenant_status", table_name="commercial_tenant_subscriptions")
    op.drop_index("ix_commercial_tenant_subscriptions_tenant_id", table_name="commercial_tenant_subscriptions")
    op.drop_table("commercial_tenant_subscriptions")
    op.drop_index("ix_commercial_obj_search", table_name="commercial_registry_objects")
    op.drop_index("ix_commercial_obj_slug", table_name="commercial_registry_objects")
    op.drop_index("ix_commercial_obj_kind_status", table_name="commercial_registry_objects")
    op.drop_index("ix_commercial_registry_objects_is_deleted", table_name="commercial_registry_objects")
    op.drop_index("ix_commercial_registry_objects_tenant_id", table_name="commercial_registry_objects")
    op.drop_index("ix_commercial_registry_objects_status", table_name="commercial_registry_objects")
    op.drop_index("ix_commercial_registry_objects_kind", table_name="commercial_registry_objects")
    op.drop_table("commercial_registry_objects")
    op.drop_table("commercial_registry_kinds")
