"""INDL-56: add contract signed_document upload columns.

Three additive nullable columns on ``contracts`` for the staff-uploaded,
wet-signed/scanned copy of the contract (distinct from ``pdf_s3_key``, which
is the system-generated contract PDF). NULL on all existing rows means "no
signed document uploaded yet" — the frontend already treats that as the
default state, so no backfill is required.

Revision ID: 0068
Revises: 0067
"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

revision = "0068"
down_revision = "0067"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.add_column(
        "contracts",
        sa.Column("signed_document_s3_key", sa.Text(), nullable=True),
    )
    op.add_column(
        "contracts",
        sa.Column(
            "signed_document_uploaded_at", sa.DateTime(timezone=True), nullable=True
        ),
    )
    op.add_column(
        "contracts",
        sa.Column(
            "signed_document_uploaded_by",
            postgresql.UUID(as_uuid=True),
            nullable=True,
        ),
    )
    op.create_foreign_key(
        "fk_contracts_signed_document_uploaded_by",
        "contracts",
        "users",
        ["signed_document_uploaded_by"],
        ["id"],
        ondelete="SET NULL",
    )


def downgrade() -> None:
    op.drop_constraint(
        "fk_contracts_signed_document_uploaded_by",
        "contracts",
        type_="foreignkey",
    )
    op.drop_column("contracts", "signed_document_uploaded_by")
    op.drop_column("contracts", "signed_document_uploaded_at")
    op.drop_column("contracts", "signed_document_s3_key")
