"""Phase 2.1 digital client acceptance workflow

Revision ID: 20260623_phase_2_1_digital_client_acceptance_workflow
Revises: 20260622_phase_2_client_acceptance_controls
Create Date: 2026-06-23
"""
from __future__ import annotations

from alembic import op
import sqlalchemy as sa

revision = "20260623_phase_2_1_digital_client_acceptance_workflow"
down_revision = "20260622_phase_2_client_acceptance_controls"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "client_acceptance_declarations",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("client_id", sa.Integer(), sa.ForeignKey("clients.id", ondelete="CASCADE"), nullable=False),
        sa.Column("tenant_id", sa.Integer(), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False),
        sa.Column("branch_id", sa.Integer(), sa.ForeignKey("branches.id", ondelete="SET NULL"), nullable=True),
        sa.Column("declaration_type", sa.String(length=30), nullable=False),
        sa.Column("assigned_user_id", sa.Integer(), sa.ForeignKey("users.id", ondelete="CASCADE"), nullable=False),
        sa.Column("assigned_role_snapshot", sa.String(length=120), nullable=True),
        sa.Column("declaration_text", sa.Text(), nullable=False),
        sa.Column("status", sa.String(length=30), nullable=False, server_default="pending"),
        sa.Column("response_notes", sa.Text(), nullable=True),
        sa.Column("issue_details", sa.Text(), nullable=True),
        sa.Column("responded_at_utc", sa.DateTime(timezone=True), nullable=True),
        sa.Column("response_ip", sa.String(length=80), nullable=True),
        sa.Column("response_user_agent", sa.String(length=500), nullable=True),
        sa.Column("requested_by_user_id", sa.Integer(), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True),
        sa.Column("requested_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.UniqueConstraint("client_id", "declaration_type", "assigned_user_id", name="uq_client_acceptance_declaration_user"),
    )
    op.create_index("ix_client_acceptance_declarations_client_id", "client_acceptance_declarations", ["client_id"])
    op.create_index("ix_client_acceptance_declarations_tenant_id", "client_acceptance_declarations", ["tenant_id"])
    op.create_index("ix_client_acceptance_declarations_branch_id", "client_acceptance_declarations", ["branch_id"])
    op.create_index("ix_client_acceptance_declarations_assigned_user_id", "client_acceptance_declarations", ["assigned_user_id"])
    op.create_index("ix_client_acceptance_declarations_status", "client_acceptance_declarations", ["status"])
    op.create_index("ix_client_acceptance_declarations_declaration_type", "client_acceptance_declarations", ["declaration_type"])

    op.create_table(
        "client_kyc_verifications",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("client_id", sa.Integer(), sa.ForeignKey("clients.id", ondelete="CASCADE"), nullable=False),
        sa.Column("tenant_id", sa.Integer(), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False),
        sa.Column("branch_id", sa.Integer(), sa.ForeignKey("branches.id", ondelete="SET NULL"), nullable=True),
        sa.Column("status", sa.String(length=30), nullable=False, server_default="pending_documents"),
        sa.Column("required_document_summary", sa.Text(), nullable=True),
        sa.Column("available_document_summary", sa.Text(), nullable=True),
        sa.Column("verification_notes", sa.Text(), nullable=True),
        sa.Column("verified_by_user_id", sa.Integer(), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True),
        sa.Column("verified_at_utc", sa.DateTime(timezone=True), nullable=True),
        sa.Column("rejected_by_user_id", sa.Integer(), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True),
        sa.Column("rejected_at_utc", sa.DateTime(timezone=True), 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.UniqueConstraint("client_id", name="uq_client_kyc_verification_client"),
    )
    op.create_index("ix_client_kyc_verifications_client_id", "client_kyc_verifications", ["client_id"])
    op.create_index("ix_client_kyc_verifications_tenant_id", "client_kyc_verifications", ["tenant_id"])
    op.create_index("ix_client_kyc_verifications_branch_id", "client_kyc_verifications", ["branch_id"])
    op.create_index("ix_client_kyc_verifications_status", "client_kyc_verifications", ["status"])

    op.create_table(
        "client_engagement_letters",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("client_id", sa.Integer(), sa.ForeignKey("clients.id", ondelete="CASCADE"), nullable=False),
        sa.Column("tenant_id", sa.Integer(), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False),
        sa.Column("branch_id", sa.Integer(), sa.ForeignKey("branches.id", ondelete="SET NULL"), nullable=True),
        sa.Column("letter_code", sa.String(length=80), nullable=False, server_default="STANDARD"),
        sa.Column("title", sa.String(length=255), nullable=False),
        sa.Column("body_text", sa.Text(), nullable=False),
        sa.Column("version_no", sa.Integer(), nullable=False, server_default="1"),
        sa.Column("status", sa.String(length=40), nullable=False, server_default="draft"),
        sa.Column("sent_to_client_at_utc", sa.DateTime(timezone=True), nullable=True),
        sa.Column("approved_by_user_id", sa.Integer(), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True),
        sa.Column("approved_at_utc", sa.DateTime(timezone=True), nullable=True),
        sa.Column("accepted_mode", sa.String(length=40), nullable=True),
        sa.Column("accepted_by_user_id", sa.Integer(), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True),
        sa.Column("accepted_at_utc", sa.DateTime(timezone=True), nullable=True),
        sa.Column("accepted_ip", sa.String(length=80), nullable=True),
        sa.Column("accepted_user_agent", sa.String(length=500), nullable=True),
        sa.Column("acceptance_declaration_text", sa.Text(), nullable=True),
        sa.Column("otp_verified", sa.Boolean(), nullable=False, server_default=sa.false()),
        sa.Column("pdf_hash_sha256", sa.String(length=64), nullable=True),
        sa.Column("manual_signed_file_path", sa.String(length=1000), nullable=True),
        sa.Column("manual_signed_file_hash_sha256", sa.String(length=64), nullable=True),
        sa.Column("manual_verified_by_user_id", sa.Integer(), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True),
        sa.Column("manual_verified_at_utc", sa.DateTime(timezone=True), nullable=True),
        sa.Column("rejection_reason", sa.Text(), nullable=True),
        sa.Column("created_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, server_default=sa.func.now()),
        sa.Column("updated_at_utc", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.UniqueConstraint("client_id", "letter_code", "version_no", name="uq_client_engagement_letter_code_version"),
    )
    op.create_index("ix_client_engagement_letters_client_id", "client_engagement_letters", ["client_id"])
    op.create_index("ix_client_engagement_letters_tenant_id", "client_engagement_letters", ["tenant_id"])
    op.create_index("ix_client_engagement_letters_branch_id", "client_engagement_letters", ["branch_id"])
    op.create_index("ix_client_engagement_letters_status", "client_engagement_letters", ["status"])
    op.create_index("ix_client_engagement_letters_accepted_mode", "client_engagement_letters", ["accepted_mode"])


def downgrade() -> None:
    op.drop_table("client_engagement_letters")
    op.drop_table("client_kyc_verifications")
    op.drop_table("client_acceptance_declarations")
