"""consultant workspace foundation phase 5a3

Revision ID: 20260510_consultant_workspace_phase_5a3
Revises: 20260509_consultant_managed_clients_phase_5a2
Create Date: 2026-05-10 00:00:00
"""

from __future__ import annotations

from alembic import op
import sqlalchemy as sa


revision = "20260510_consultant_workspace_phase_5a3"
down_revision = "20260509_consultant_managed_clients_phase_5a2"
branch_labels = None
depends_on = None


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


def upgrade() -> None:
    bind = op.get_bind()
    if not _table_exists(bind, "consultant_workspaces"):
        op.create_table(
            "consultant_workspaces",
            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("consultant_id", sa.Integer(), sa.ForeignKey("consultant_profiles.id", ondelete="CASCADE"), nullable=False),
            sa.Column("workspace_code", sa.String(length=50), nullable=False),
            sa.Column("workspace_name", sa.String(length=200), nullable=False),
            sa.Column("workspace_type", sa.String(length=50), nullable=False, server_default="consultant_saas"),
            sa.Column("plan_code", sa.String(length=50), nullable=False, server_default="starter"),
            sa.Column("billing_cycle", sa.String(length=30), nullable=False, server_default="manual"),
            sa.Column("subscription_status", sa.String(length=30), nullable=False, server_default="trial"),
            sa.Column("subscription_start_date", sa.Date(), nullable=True),
            sa.Column("subscription_end_date", sa.Date(), nullable=True),
            sa.Column("max_managed_clients", sa.Integer(), nullable=False, server_default="25"),
            sa.Column("max_user_accounts", sa.Integer(), nullable=False, server_default="1"),
            sa.Column("allow_client_portal", sa.Boolean(), nullable=False, server_default=sa.false()),
            sa.Column("allow_firm_referrals", sa.Boolean(), nullable=False, server_default=sa.true()),
            sa.Column("allow_service_marketplace", 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", "consultant_id", name="uq_consultant_workspaces_tenant_consultant"),
            sa.UniqueConstraint("tenant_id", "workspace_code", name="uq_consultant_workspaces_tenant_code"),
        )
        op.create_index("ix_consultant_workspaces_tenant_id", "consultant_workspaces", ["tenant_id"])
        op.create_index("ix_consultant_workspaces_branch_id", "consultant_workspaces", ["branch_id"])
        op.create_index("ix_consultant_workspaces_consultant_id", "consultant_workspaces", ["consultant_id"])
        op.create_index("ix_consultant_workspaces_workspace_code", "consultant_workspaces", ["workspace_code"])
        op.create_index("ix_consultant_workspaces_workspace_type", "consultant_workspaces", ["workspace_type"])
        op.create_index("ix_consultant_workspaces_plan_code", "consultant_workspaces", ["plan_code"])
        op.create_index("ix_consultant_workspaces_subscription_status", "consultant_workspaces", ["subscription_status"])
        op.create_index("ix_consultant_workspaces_billing_cycle", "consultant_workspaces", ["billing_cycle"])
        op.create_index("ix_consultant_workspaces_is_active", "consultant_workspaces", ["is_active"])


def downgrade() -> None:
    bind = op.get_bind()
    if _table_exists(bind, "consultant_workspaces"):
        op.drop_index("ix_consultant_workspaces_is_active", table_name="consultant_workspaces")
        op.drop_index("ix_consultant_workspaces_billing_cycle", table_name="consultant_workspaces")
        op.drop_index("ix_consultant_workspaces_subscription_status", table_name="consultant_workspaces")
        op.drop_index("ix_consultant_workspaces_plan_code", table_name="consultant_workspaces")
        op.drop_index("ix_consultant_workspaces_workspace_type", table_name="consultant_workspaces")
        op.drop_index("ix_consultant_workspaces_workspace_code", table_name="consultant_workspaces")
        op.drop_index("ix_consultant_workspaces_consultant_id", table_name="consultant_workspaces")
        op.drop_index("ix_consultant_workspaces_branch_id", table_name="consultant_workspaces")
        op.drop_index("ix_consultant_workspaces_tenant_id", table_name="consultant_workspaces")
        op.drop_table("consultant_workspaces")
