"""Phase 1 client-consultant referral and communication linkage.

Revision ID: 20260722_phase1_client_consultant_linkage
Revises: 20260720_document_vps_auto_cleanup
"""
from alembic import op
import sqlalchemy as sa

revision = "20260722_phase1_client_consultant_linkage"
down_revision = "20260720_document_vps_auto_cleanup"
branch_labels = None
depends_on = None

def upgrade() -> None:
    with op.batch_alter_table("clients") as batch:
        batch.add_column(sa.Column("referred_by_consultant_id", sa.Integer(), nullable=True))
        batch.add_column(sa.Column("referral_date", sa.Date(), nullable=True))
        batch.add_column(sa.Column("referral_reference", sa.String(length=200), nullable=True))
        batch.add_column(sa.Column("referral_status", sa.String(length=30), nullable=False, server_default="active"))
        batch.add_column(sa.Column("communication_routing_mode", sa.String(length=30), nullable=False, server_default="client_and_consultant"))
        batch.create_foreign_key("fk_clients_referred_by_consultant", "consultant_profiles", ["referred_by_consultant_id"], ["id"], ondelete="SET NULL")
        batch.create_index("ix_clients_referred_by_consultant_id", ["referred_by_consultant_id"], unique=False)
        batch.create_index("ix_clients_referral_status", ["referral_status"], unique=False)
        batch.create_index("ix_clients_communication_routing_mode", ["communication_routing_mode"], unique=False)
    additions = [
        ("can_view_engagements", True), ("can_view_task_status", True), ("can_view_assignee", True),
        ("can_view_document_requests", True), ("can_upload_documents", True),
        ("can_reply_to_clarifications", True), ("can_view_filing_details", True),
        ("can_view_final_documents", True), ("can_view_permanent_documents", False),
        ("can_receive_notifications", True), ("can_act_for_client", True),
    ]
    with op.batch_alter_table("client_consultant_links") as batch:
        for name, default in additions:
            batch.add_column(sa.Column(name, sa.Boolean(), nullable=False, server_default=sa.true() if default else sa.false()))
        batch.add_column(sa.Column("effective_from", sa.Date(), nullable=True))
        batch.add_column(sa.Column("effective_to", sa.Date(), nullable=True))

def downgrade() -> None:
    with op.batch_alter_table("client_consultant_links") as batch:
        batch.drop_column("effective_to")
        batch.drop_column("effective_from")
        for name in ["can_act_for_client", "can_receive_notifications", "can_view_permanent_documents", "can_view_final_documents", "can_view_filing_details", "can_reply_to_clarifications", "can_upload_documents", "can_view_document_requests", "can_view_assignee", "can_view_task_status", "can_view_engagements"]:
            batch.drop_column(name)
    with op.batch_alter_table("clients") as batch:
        batch.drop_index("ix_clients_communication_routing_mode")
        batch.drop_index("ix_clients_referral_status")
        batch.drop_index("ix_clients_referred_by_consultant_id")
        batch.drop_constraint("fk_clients_referred_by_consultant", type_="foreignkey")
        batch.drop_column("communication_routing_mode")
        batch.drop_column("referral_status")
        batch.drop_column("referral_reference")
        batch.drop_column("referral_date")
        batch.drop_column("referred_by_consultant_id")
