"""Phase 6E employee document management

Revision ID: 20260517_phase_6e_employee_documents
Revises: 20260516_phase_6d_employee_leave
Create Date: 2026-05-10
"""
from __future__ import annotations

from alembic import op
import sqlalchemy as sa

revision = "20260517_phase_6e_employee_documents"
down_revision = "20260516_phase_6d_employee_leave"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "employee_document_types",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("tenant_id", sa.Integer(), nullable=False),
        sa.Column("branch_id", sa.Integer(), nullable=False),
        sa.Column("code", sa.String(length=50), nullable=False),
        sa.Column("name", sa.String(length=150), nullable=False),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("is_mandatory", sa.Boolean(), nullable=False, server_default=sa.false()),
        sa.Column("allow_employee_upload", sa.Boolean(), nullable=False, server_default=sa.true()),
        sa.Column("requires_verification", sa.Boolean(), nullable=False, server_default=sa.true()),
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.true()),
        sa.Column("created_by_user_id", sa.Integer(), nullable=True),
        sa.Column("updated_by_user_id", sa.Integer(), nullable=True),
        sa.Column("created_at_utc", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at_utc", sa.DateTime(timezone=True), nullable=False),
        sa.ForeignKeyConstraint(["tenant_id"], ["tenants.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["branch_id"], ["branches.id"]),
        sa.ForeignKeyConstraint(["created_by_user_id"], ["users.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["updated_by_user_id"], ["users.id"], ondelete="SET NULL"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("tenant_id", "branch_id", "code", name="uq_employee_document_types_tenant_branch_code"),
    )
    op.create_index("ix_employee_document_types_tenant_id", "employee_document_types", ["tenant_id"])
    op.create_index("ix_employee_document_types_branch_id", "employee_document_types", ["branch_id"])
    op.create_index("ix_employee_document_types_code", "employee_document_types", ["code"])
    op.create_index("ix_employee_document_types_is_active", "employee_document_types", ["is_active"])

    op.create_table(
        "employee_documents",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("tenant_id", sa.Integer(), nullable=False),
        sa.Column("branch_id", sa.Integer(), nullable=False),
        sa.Column("employee_id", sa.Integer(), nullable=False),
        sa.Column("document_type_id", sa.Integer(), nullable=True),
        sa.Column("title", sa.String(length=200), nullable=False),
        sa.Column("document_no", sa.String(length=100), nullable=True),
        sa.Column("issue_date", sa.Date(), nullable=True),
        sa.Column("expiry_date", sa.Date(), nullable=True),
        sa.Column("original_filename", sa.String(length=255), nullable=False),
        sa.Column("stored_filename", sa.String(length=255), nullable=False),
        sa.Column("storage_path", sa.String(length=500), nullable=False),
        sa.Column("content_type", sa.String(length=150), nullable=True),
        sa.Column("file_size_bytes", sa.Integer(), nullable=True),
        sa.Column("status", sa.String(length=30), nullable=False, server_default="uploaded"),
        sa.Column("visibility", sa.String(length=30), nullable=False, server_default="employee_and_hr"),
        sa.Column("remarks", sa.Text(), nullable=True),
        sa.Column("verified_by_user_id", sa.Integer(), nullable=True),
        sa.Column("verified_at_utc", sa.DateTime(timezone=True), nullable=True),
        sa.Column("verification_notes", sa.Text(), nullable=True),
        sa.Column("uploaded_by_user_id", sa.Integer(), nullable=True),
        sa.Column("updated_by_user_id", sa.Integer(), nullable=True),
        sa.Column("created_at_utc", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at_utc", sa.DateTime(timezone=True), nullable=False),
        sa.ForeignKeyConstraint(["tenant_id"], ["tenants.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["branch_id"], ["branches.id"]),
        sa.ForeignKeyConstraint(["employee_id"], ["employees.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["document_type_id"], ["employee_document_types.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["verified_by_user_id"], ["users.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["uploaded_by_user_id"], ["users.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["updated_by_user_id"], ["users.id"], ondelete="SET NULL"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index("ix_employee_documents_tenant_id", "employee_documents", ["tenant_id"])
    op.create_index("ix_employee_documents_branch_id", "employee_documents", ["branch_id"])
    op.create_index("ix_employee_documents_employee_id", "employee_documents", ["employee_id"])
    op.create_index("ix_employee_documents_document_type_id", "employee_documents", ["document_type_id"])
    op.create_index("ix_employee_documents_title", "employee_documents", ["title"])
    op.create_index("ix_employee_documents_document_no", "employee_documents", ["document_no"])
    op.create_index("ix_employee_documents_expiry_date", "employee_documents", ["expiry_date"])
    op.create_index("ix_employee_documents_status", "employee_documents", ["status"])
    op.create_index("ix_employee_documents_visibility", "employee_documents", ["visibility"])


def downgrade() -> None:
    op.drop_index("ix_employee_documents_visibility", table_name="employee_documents")
    op.drop_index("ix_employee_documents_status", table_name="employee_documents")
    op.drop_index("ix_employee_documents_expiry_date", table_name="employee_documents")
    op.drop_index("ix_employee_documents_document_no", table_name="employee_documents")
    op.drop_index("ix_employee_documents_title", table_name="employee_documents")
    op.drop_index("ix_employee_documents_document_type_id", table_name="employee_documents")
    op.drop_index("ix_employee_documents_employee_id", table_name="employee_documents")
    op.drop_index("ix_employee_documents_branch_id", table_name="employee_documents")
    op.drop_index("ix_employee_documents_tenant_id", table_name="employee_documents")
    op.drop_table("employee_documents")
    op.drop_index("ix_employee_document_types_is_active", table_name="employee_document_types")
    op.drop_index("ix_employee_document_types_code", table_name="employee_document_types")
    op.drop_index("ix_employee_document_types_branch_id", table_name="employee_document_types")
    op.drop_index("ix_employee_document_types_tenant_id", table_name="employee_document_types")
    op.drop_table("employee_document_types")
