"""phase 5 engagement closure checklist

Revision ID: 20260627_phase_5_engagement_closure_checklist
Revises: 20260626_phase_4_aqmm_task_evidence_review_notes
Create Date: 2026-06-27
"""
from __future__ import annotations

from alembic import op
import sqlalchemy as sa

revision = "20260627_phase_5_engagement_closure_checklist"
down_revision = "20260626_phase_4_aqmm_task_evidence_review_notes"
branch_labels = None
depends_on = None


def _has_table(bind, table_name: str) -> bool:
    return sa.inspect(bind).has_table(table_name)


def upgrade() -> None:
    bind = op.get_bind()
    if not _has_table(bind, "engagement_closure_checklists"):
        op.create_table(
            "engagement_closure_checklists",
            sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
            sa.Column("tenant_id", sa.Integer(), nullable=False),
            sa.Column("branch_id", sa.Integer(), nullable=True),
            sa.Column("client_id", sa.Integer(), nullable=False),
            sa.Column("subscription_id", sa.Integer(), nullable=False),
            sa.Column("closure_status", sa.String(length=40), nullable=False, server_default="not_started"),
            sa.Column("aqmm_acceptance_completed", sa.Boolean(), nullable=False, server_default=sa.false()),
            sa.Column("aqmm_tasks_completed", sa.Boolean(), nullable=False, server_default=sa.false()),
            sa.Column("evidence_review_completed", sa.Boolean(), nullable=False, server_default=sa.false()),
            sa.Column("final_documents_released", sa.Boolean(), nullable=False, server_default=sa.false()),
            sa.Column("udin_completed", sa.Boolean(), nullable=False, server_default=sa.false()),
            sa.Column("normal_tasks_completed", sa.Boolean(), nullable=False, server_default=sa.false()),
            sa.Column("deliverables_sent_to_client", sa.Boolean(), nullable=False, server_default=sa.false()),
            sa.Column("billing_reviewed", sa.Boolean(), nullable=False, server_default=sa.false()),
            sa.Column("open_points_closed", sa.Boolean(), nullable=False, server_default=sa.false()),
            sa.Column("client_communication_completed", sa.Boolean(), nullable=False, server_default=sa.false()),
            sa.Column("closure_note", sa.Text(), nullable=True),
            sa.Column("closure_block_reason", sa.Text(), nullable=True),
            sa.Column("closure_approved_by_user_id", sa.Integer(), nullable=True),
            sa.Column("closure_approved_at_utc", sa.DateTime(timezone=True), nullable=True),
            sa.Column("reopened_by_user_id", sa.Integer(), nullable=True),
            sa.Column("reopened_at_utc", sa.DateTime(timezone=True), nullable=True),
            sa.Column("reopen_reason", sa.Text(), nullable=True),
            sa.Column("created_by_user_id", sa.Integer(), 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.ForeignKeyConstraint(["tenant_id"], ["tenants.id"], ondelete="CASCADE"),
            sa.ForeignKeyConstraint(["branch_id"], ["branches.id"], ondelete="SET NULL"),
            sa.ForeignKeyConstraint(["client_id"], ["clients.id"], ondelete="CASCADE"),
            sa.ForeignKeyConstraint(["subscription_id"], ["client_service_subscriptions.id"], ondelete="CASCADE"),
            sa.ForeignKeyConstraint(["closure_approved_by_user_id"], ["users.id"], ondelete="SET NULL"),
            sa.ForeignKeyConstraint(["reopened_by_user_id"], ["users.id"], ondelete="SET NULL"),
            sa.ForeignKeyConstraint(["created_by_user_id"], ["users.id"], ondelete="SET NULL"),
            sa.UniqueConstraint("subscription_id", name="uq_engagement_closure_subscription"),
        )
        op.create_index("ix_engagement_closure_tenant", "engagement_closure_checklists", ["tenant_id"])
        op.create_index("ix_engagement_closure_branch", "engagement_closure_checklists", ["branch_id"])
        op.create_index("ix_engagement_closure_client", "engagement_closure_checklists", ["client_id"])
        op.create_index("ix_engagement_closure_subscription", "engagement_closure_checklists", ["subscription_id"])
        op.create_index("ix_engagement_closure_status", "engagement_closure_checklists", ["closure_status"])


def downgrade() -> None:
    bind = op.get_bind()
    if _has_table(bind, "engagement_closure_checklists"):
        op.drop_index("ix_engagement_closure_status", table_name="engagement_closure_checklists")
        op.drop_index("ix_engagement_closure_subscription", table_name="engagement_closure_checklists")
        op.drop_index("ix_engagement_closure_client", table_name="engagement_closure_checklists")
        op.drop_index("ix_engagement_closure_branch", table_name="engagement_closure_checklists")
        op.drop_index("ix_engagement_closure_tenant", table_name="engagement_closure_checklists")
        op.drop_table("engagement_closure_checklists")
