"""Phase 5A.1 consultant portal foundation

Revision ID: 20260508_consultant_portal_phase_5a1
Revises: 20260507_task_communication_phase_4c
Create Date: 2026-05-08
"""

from __future__ import annotations

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


revision: str = "20260508_consultant_portal_phase_5a1"
down_revision: Union[str, None] = "20260507_task_communication_phase_4c"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


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


def upgrade() -> None:
    if not _has_table("consultant_profiles"):
        op.create_table(
            "consultant_profiles",
            sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
            sa.Column("tenant_id", sa.Integer(), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False),
            sa.Column("branch_id", sa.Integer(), sa.ForeignKey("branches.id"), nullable=True),
            sa.Column("user_id", sa.Integer(), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True),
            sa.Column("consultant_type", sa.String(length=50), nullable=False, server_default="external_consultant"),
            sa.Column("firm_name", sa.String(length=200), nullable=True),
            sa.Column("contact_person", sa.String(length=200), nullable=False),
            sa.Column("email", sa.String(length=255), nullable=True),
            sa.Column("mobile", sa.String(length=20), nullable=True),
            sa.Column("specialisation", sa.String(length=200), nullable=True),
            sa.Column("gstin", sa.String(length=20), nullable=True),
            sa.Column("pan", sa.String(length=20), nullable=True),
            sa.Column("address", sa.Text(), nullable=True),
            sa.Column("status", sa.String(length=30), nullable=False, server_default="active"),
            sa.Column("onboarding_status", sa.String(length=30), nullable=False, server_default="approved"),
            sa.Column("is_platform_partner", sa.Boolean(), nullable=False, server_default=sa.false()),
            sa.Column("is_franchise_partner", sa.Boolean(), nullable=False, server_default=sa.false()),
            sa.Column("is_saas_customer", sa.Boolean(), nullable=False, server_default=sa.false()),
            sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.true()),
            sa.Column("remarks", sa.Text(), nullable=True),
            sa.Column("created_by_user_id", sa.Integer(), sa.ForeignKey("users.id"), nullable=True),
            sa.Column("updated_by_user_id", sa.Integer(), sa.ForeignKey("users.id"), 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.UniqueConstraint("tenant_id", "user_id", name="uq_consultant_profiles_tenant_user"),
            sa.UniqueConstraint("tenant_id", "email", name="uq_consultant_profiles_tenant_email"),
        )
        op.create_index("ix_consultant_profiles_tenant_id", "consultant_profiles", ["tenant_id"])
        op.create_index("ix_consultant_profiles_branch_id", "consultant_profiles", ["branch_id"])
        op.create_index("ix_consultant_profiles_user_id", "consultant_profiles", ["user_id"])
        op.create_index("ix_consultant_profiles_email", "consultant_profiles", ["email"])
        op.create_index("ix_consultant_profiles_status", "consultant_profiles", ["status"])
        op.create_index("ix_consultant_profiles_onboarding_status", "consultant_profiles", ["onboarding_status"])
        op.create_index("ix_consultant_profiles_is_active", "consultant_profiles", ["is_active"])
        op.create_index("ix_consultant_profiles_consultant_type", "consultant_profiles", ["consultant_type"])

    if not _has_table("client_consultant_links"):
        op.create_table(
            "client_consultant_links",
            sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
            sa.Column("tenant_id", sa.Integer(), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False),
            sa.Column("branch_id", sa.Integer(), sa.ForeignKey("branches.id"), nullable=True),
            sa.Column("client_id", sa.Integer(), sa.ForeignKey("clients.id", ondelete="CASCADE"), nullable=False),
            sa.Column("consultant_id", sa.Integer(), sa.ForeignKey("consultant_profiles.id", ondelete="CASCADE"), nullable=False),
            sa.Column("service_catalogue_id", sa.Integer(), sa.ForeignKey("service_catalogues.id", ondelete="SET NULL"), nullable=True),
            sa.Column("relationship_type", sa.String(length=50), nullable=False, server_default="accounts_consultant"),
            sa.Column("is_primary", sa.Boolean(), nullable=False, server_default=sa.false()),
            sa.Column("can_view_client", sa.Boolean(), nullable=False, server_default=sa.true()),
            sa.Column("can_view_services", sa.Boolean(), nullable=False, server_default=sa.true()),
            sa.Column("can_view_due_dates", sa.Boolean(), nullable=False, server_default=sa.true()),
            sa.Column("can_view_communications", sa.Boolean(), nullable=False, server_default=sa.true()),
            sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.true()),
            sa.Column("remarks", sa.Text(), nullable=True),
            sa.Column("created_by_user_id", sa.Integer(), sa.ForeignKey("users.id"), nullable=True),
            sa.Column("updated_by_user_id", sa.Integer(), sa.ForeignKey("users.id"), 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.UniqueConstraint("tenant_id", "client_id", "consultant_id", name="uq_client_consultant_links_client_consultant"),
        )
        op.create_index("ix_client_consultant_links_tenant_id", "client_consultant_links", ["tenant_id"])
        op.create_index("ix_client_consultant_links_branch_id", "client_consultant_links", ["branch_id"])
        op.create_index("ix_client_consultant_links_client_id", "client_consultant_links", ["client_id"])
        op.create_index("ix_client_consultant_links_consultant_id", "client_consultant_links", ["consultant_id"])
        op.create_index("ix_client_consultant_links_service_catalogue_id", "client_consultant_links", ["service_catalogue_id"])
        op.create_index("ix_client_consultant_links_relationship_type", "client_consultant_links", ["relationship_type"])
        op.create_index("ix_client_consultant_links_is_active", "client_consultant_links", ["is_active"])


def downgrade() -> None:
    if _has_table("client_consultant_links"):
        op.drop_index("ix_client_consultant_links_is_active", table_name="client_consultant_links")
        op.drop_index("ix_client_consultant_links_relationship_type", table_name="client_consultant_links")
        op.drop_index("ix_client_consultant_links_service_catalogue_id", table_name="client_consultant_links")
        op.drop_index("ix_client_consultant_links_consultant_id", table_name="client_consultant_links")
        op.drop_index("ix_client_consultant_links_client_id", table_name="client_consultant_links")
        op.drop_index("ix_client_consultant_links_branch_id", table_name="client_consultant_links")
        op.drop_index("ix_client_consultant_links_tenant_id", table_name="client_consultant_links")
        op.drop_table("client_consultant_links")
    if _has_table("consultant_profiles"):
        op.drop_index("ix_consultant_profiles_consultant_type", table_name="consultant_profiles")
        op.drop_index("ix_consultant_profiles_is_active", table_name="consultant_profiles")
        op.drop_index("ix_consultant_profiles_onboarding_status", table_name="consultant_profiles")
        op.drop_index("ix_consultant_profiles_status", table_name="consultant_profiles")
        op.drop_index("ix_consultant_profiles_email", table_name="consultant_profiles")
        op.drop_index("ix_consultant_profiles_user_id", table_name="consultant_profiles")
        op.drop_index("ix_consultant_profiles_branch_id", table_name="consultant_profiles")
        op.drop_index("ix_consultant_profiles_tenant_id", table_name="consultant_profiles")
        op.drop_table("consultant_profiles")
