"""Phase 2 consultant visibility, communications and document requests.

Revision ID: 20260723_phase2_consultant_visibility_document_requests
Revises: 20260722_phase1_client_consultant_linkage
"""
from alembic import op
import sqlalchemy as sa
revision = "20260723_phase2_consultant_visibility_document_requests"
down_revision = "20260722_phase1_client_consultant_linkage"
branch_labels = None
depends_on = None

def upgrade():
    op.create_table(
        "service_task_document_requests",
        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("subscription_id", sa.Integer(), sa.ForeignKey("client_service_subscriptions.id", ondelete="CASCADE"), nullable=False),
        sa.Column("task_instance_id", sa.Integer(), sa.ForeignKey("client_service_task_instances.id", ondelete="CASCADE"), nullable=False),
        sa.Column("client_id", sa.Integer(), sa.ForeignKey("clients.id", ondelete="CASCADE"), nullable=False),
        sa.Column("request_type", sa.String(30), nullable=False, server_default="document"),
        sa.Column("title", sa.String(200), nullable=False), sa.Column("description", sa.Text(), nullable=True),
        sa.Column("requested_from", sa.String(30), nullable=False, server_default="client_and_consultant"),
        sa.Column("due_date", sa.Date(), nullable=True), sa.Column("status", sa.String(30), nullable=False, server_default="pending"),
        sa.Column("response_note", sa.Text(), nullable=True), sa.Column("received_at_utc", sa.DateTime(timezone=True), nullable=True), sa.Column("verified_at_utc", sa.DateTime(timezone=True), nullable=True),
        sa.Column("requested_by_user_id", sa.Integer(), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True),
        sa.Column("responded_by_user_id", sa.Integer(), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True),
        sa.Column("verified_by_user_id", sa.Integer(), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True),
        sa.Column("created_at_utc", sa.DateTime(timezone=True), nullable=False), sa.Column("updated_at_utc", sa.DateTime(timezone=True), nullable=False), sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.true()),
    )
    for name, cols in [("ix_task_doc_requests_tenant",["tenant_id"]),("ix_task_doc_requests_subscription",["subscription_id"]),("ix_task_doc_requests_task",["task_instance_id"]),("ix_task_doc_requests_client",["client_id"]),("ix_task_doc_requests_status",["status"]),("ix_task_doc_requests_due",["due_date"])]: op.create_index(name,"service_task_document_requests",cols)

def downgrade():
    op.drop_table("service_task_document_requests")
