"""Phase 7T.1 domain mapping table

Revision ID: 20260613_phase_7t1_domain_mapping
Revises: 20260612_phase_7s3_email_mapping
Create Date: 2026-06-13
"""
from __future__ import annotations

from alembic import op
import sqlalchemy as sa

revision = "20260613_phase_7t1_domain_mapping"
down_revision = "20260612_phase_7s3_email_mapping"
branch_labels = None
depends_on = None


def _table_exists(table_name: str) -> bool:
    bind = op.get_bind()
    inspector = sa.inspect(bind)
    return table_name in inspector.get_table_names()


def upgrade() -> None:
    if _table_exists("domain_mappings"):
        return

    op.create_table(
        "domain_mappings",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("domain_name", sa.String(length=255), nullable=False),
        sa.Column("domain_type", sa.String(length=60), nullable=False),
        sa.Column("tenant_id", sa.Integer(), nullable=True),
        sa.Column("branch_id", sa.Integer(), nullable=True),
        sa.Column("consultant_id", sa.Integer(), nullable=True),
        sa.Column("parent_tenant_id", sa.Integer(), nullable=True),
        sa.Column("is_primary", sa.Boolean(), nullable=False, server_default=sa.text("false")),
        sa.Column("is_verified", sa.Boolean(), nullable=False, server_default=sa.text("false")),
        sa.Column("status", sa.String(length=30), nullable=False, server_default="draft"),
        sa.Column("verification_token", sa.String(length=120), nullable=True),
        sa.Column("dns_txt_name", sa.String(length=255), nullable=True),
        sa.Column("dns_txt_value", sa.String(length=255), nullable=True),
        sa.Column("ssl_mode", sa.String(length=30), nullable=False, server_default="manual"),
        sa.Column("notes", sa.Text(), nullable=True),
        sa.Column("created_by_user_id", sa.Integer(), nullable=True),
        sa.Column("updated_by_user_id", sa.Integer(), nullable=True),
        sa.Column("created_at_utc", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.Column("updated_at_utc", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.ForeignKeyConstraint(["tenant_id"], ["tenants.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["branch_id"], ["branches.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["consultant_id"], ["consultant_profiles.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["parent_tenant_id"], ["tenants.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["created_by_user_id"], ["users.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["updated_by_user_id"], ["users.id"], ondelete="SET NULL"),
        sa.UniqueConstraint("domain_name", name="uq_domain_mappings_domain_name"),
    )
    op.create_index("ix_domain_mappings_domain_name", "domain_mappings", ["domain_name"])
    op.create_index("ix_domain_mappings_domain_type", "domain_mappings", ["domain_type"])
    op.create_index("ix_domain_mappings_status", "domain_mappings", ["status"])
    op.create_index("ix_domain_mappings_tenant_id", "domain_mappings", ["tenant_id"])
    op.create_index("ix_domain_mappings_branch_id", "domain_mappings", ["branch_id"])
    op.create_index("ix_domain_mappings_consultant_id", "domain_mappings", ["consultant_id"])
    op.create_index("ix_domain_mappings_parent_tenant_id", "domain_mappings", ["parent_tenant_id"])
    op.create_index("ix_domain_mappings_is_primary", "domain_mappings", ["is_primary"])
    op.create_index("ix_domain_mappings_is_verified", "domain_mappings", ["is_verified"])
    op.create_index("ix_domain_mappings_verification_token", "domain_mappings", ["verification_token"])


def downgrade() -> None:
    if not _table_exists("domain_mappings"):
        return
    op.drop_table("domain_mappings")
