"""Phase 3 consultant task execution.

Revision ID: 20260723_phase3_consultant_task_execution
Revises: 20260723_phase2_consultant_visibility_document_requests
"""
from alembic import op
import sqlalchemy as sa

revision = "20260723_phase3_consultant_task_execution"
down_revision = "20260723_phase2_consultant_visibility_document_requests"
branch_labels = None
depends_on = None


def upgrade():
    op.add_column("client_service_task_instances", sa.Column("execution_mode", sa.String(30), nullable=False, server_default="internal"))
    op.add_column("client_service_task_instances", sa.Column("assigned_consultant_id", sa.Integer(), nullable=True))
    op.add_column("client_service_task_instances", sa.Column("consultant_assignment_status", sa.String(30), nullable=False, server_default="not_applicable"))
    op.add_column("client_service_task_instances", sa.Column("consultant_due_date", sa.Date(), nullable=True))
    op.add_column("client_service_task_instances", sa.Column("consultant_offered_at_utc", sa.DateTime(timezone=True), nullable=True))
    op.add_column("client_service_task_instances", sa.Column("consultant_accepted_at_utc", sa.DateTime(timezone=True), nullable=True))
    op.add_column("client_service_task_instances", sa.Column("consultant_declined_at_utc", sa.DateTime(timezone=True), nullable=True))
    op.add_column("client_service_task_instances", sa.Column("consultant_decline_reason", sa.Text(), nullable=True))
    op.add_column("client_service_task_instances", sa.Column("consultant_started_at_utc", sa.DateTime(timezone=True), nullable=True))
    op.add_column("client_service_task_instances", sa.Column("consultant_submitted_at_utc", sa.DateTime(timezone=True), nullable=True))
    op.add_column("client_service_task_instances", sa.Column("consultant_submission_note", sa.Text(), nullable=True))
    op.add_column("client_service_task_instances", sa.Column("consultant_approved_at_utc", sa.DateTime(timezone=True), nullable=True))
    op.add_column("client_service_task_instances", sa.Column("consultant_approved_by_user_id", sa.Integer(), nullable=True))
    op.create_foreign_key("fk_task_instance_assigned_consultant", "client_service_task_instances", "consultant_profiles", ["assigned_consultant_id"], ["id"], ondelete="SET NULL")
    op.create_foreign_key("fk_task_instance_consultant_approved_by", "client_service_task_instances", "users", ["consultant_approved_by_user_id"], ["id"], ondelete="SET NULL")
    op.create_index("ix_task_instance_execution_mode", "client_service_task_instances", ["execution_mode"])
    op.create_index("ix_task_instance_assigned_consultant", "client_service_task_instances", ["assigned_consultant_id"])
    op.create_index("ix_task_instance_consultant_assignment_status", "client_service_task_instances", ["consultant_assignment_status"])
    op.create_index("ix_task_instance_consultant_due_date", "client_service_task_instances", ["consultant_due_date"])
    op.create_index("ix_task_instance_consultant_approved_by", "client_service_task_instances", ["consultant_approved_by_user_id"])


def downgrade():
    op.drop_index("ix_task_instance_consultant_approved_by", table_name="client_service_task_instances")
    op.drop_index("ix_task_instance_consultant_due_date", table_name="client_service_task_instances")
    op.drop_index("ix_task_instance_consultant_assignment_status", table_name="client_service_task_instances")
    op.drop_index("ix_task_instance_assigned_consultant", table_name="client_service_task_instances")
    op.drop_index("ix_task_instance_execution_mode", table_name="client_service_task_instances")
    op.drop_constraint("fk_task_instance_consultant_approved_by", "client_service_task_instances", type_="foreignkey")
    op.drop_constraint("fk_task_instance_assigned_consultant", "client_service_task_instances", type_="foreignkey")
    for column in [
        "consultant_approved_by_user_id", "consultant_approved_at_utc", "consultant_submission_note",
        "consultant_submitted_at_utc", "consultant_started_at_utc", "consultant_decline_reason",
        "consultant_declined_at_utc", "consultant_accepted_at_utc", "consultant_offered_at_utc",
        "consultant_due_date", "consultant_assignment_status", "assigned_consultant_id", "execution_mode",
    ]:
        op.drop_column("client_service_task_instances", column)
