"""add structured checklist fields to service tasks

Revision ID: 20260719_task_checklist_fields
Revises: 20260713_bank_stmt_queue
"""
from alembic import op
import sqlalchemy as sa

revision = "20260719_task_checklist_fields"
down_revision = "20260713_bank_stmt_queue"
branch_labels = None
depends_on = None

_TEMPLATE_TABLES = ("service_default_task_templates", "firm_service_task_templates")

def upgrade():
    for table in _TEMPLATE_TABLES:
        op.add_column(table, sa.Column("task_category", sa.String(length=100), nullable=True))
        op.add_column(table, sa.Column("response_required", sa.Boolean(), nullable=False, server_default=sa.false()))
        op.add_column(table, sa.Column("response_type", sa.String(length=20), nullable=False, server_default="NONE"))
        op.add_column(table, sa.Column("evidence_required", sa.Boolean(), nullable=False, server_default=sa.false()))
        op.add_column(table, sa.Column("remarks_required_if_no", sa.Boolean(), nullable=False, server_default=sa.false()))
        op.create_index(f"ix_{table}_task_category", table, ["task_category"], unique=False)

    table = "client_service_task_instances"
    op.add_column(table, sa.Column("task_category", sa.String(length=100), nullable=True))
    op.add_column(table, sa.Column("response_required", sa.Boolean(), nullable=False, server_default=sa.false()))
    op.add_column(table, sa.Column("response_type", sa.String(length=20), nullable=False, server_default="NONE"))
    op.add_column(table, sa.Column("evidence_required", sa.Boolean(), nullable=False, server_default=sa.false()))
    op.add_column(table, sa.Column("remarks_required_if_no", sa.Boolean(), nullable=False, server_default=sa.false()))
    op.add_column(table, sa.Column("checklist_response", sa.String(length=10), nullable=True))
    op.add_column(table, sa.Column("checklist_text_response", sa.Text(), nullable=True))
    op.add_column(table, sa.Column("checklist_number_response", sa.Numeric(18, 2), nullable=True))
    op.add_column(table, sa.Column("checklist_date_response", sa.Date(), nullable=True))
    op.add_column(table, sa.Column("checklist_remarks", sa.Text(), nullable=True))
    op.create_index("ix_client_service_task_instances_task_category", table, ["task_category"], unique=False)

def downgrade():
    table = "client_service_task_instances"
    op.drop_index("ix_client_service_task_instances_task_category", table_name=table)
    for column in ("checklist_remarks", "checklist_date_response", "checklist_number_response", "checklist_text_response", "checklist_response", "remarks_required_if_no", "evidence_required", "response_type", "response_required", "task_category"):
        op.drop_column(table, column)

    for table in reversed(_TEMPLATE_TABLES):
        op.drop_index(f"ix_{table}_task_category", table_name=table)
        for column in ("remarks_required_if_no", "evidence_required", "response_type", "response_required", "task_category"):
            op.drop_column(table, column)
