"""Phase 6G employee payroll foundation

Revision ID: 20260519_phase_6g_employee_payroll
Revises: 20260518_phase_6f_employee_onboarding_offboarding
Create Date: 2026-05-19
"""
from __future__ import annotations

from alembic import op
import sqlalchemy as sa

revision = "20260519_phase_6g_employee_payroll"
down_revision = "20260518_phase_6f_employee_onboarding_offboarding"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "employee_salary_structures",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("tenant_id", sa.Integer(), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False),
        sa.Column("branch_id", sa.Integer(), sa.ForeignKey("branches.id"), nullable=False),
        sa.Column("employee_id", sa.Integer(), sa.ForeignKey("employees.id", ondelete="CASCADE"), nullable=False),
        sa.Column("effective_from", sa.Date(), nullable=False),
        sa.Column("effective_to", sa.Date(), nullable=True),
        sa.Column("pay_cycle", sa.String(length=30), nullable=False, server_default="monthly"),
        sa.Column("monthly_ctc_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("basic_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("hra_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("allowance_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("employee_pf_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("employee_esi_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("professional_tax_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("tds_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("other_deduction_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.true()),
        sa.Column("remarks", sa.Text(), nullable=True),
        sa.Column("created_by_user_id", sa.Integer(), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True),
        sa.Column("updated_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),
        sa.Column("updated_at_utc", sa.DateTime(timezone=True), nullable=False),
        sa.UniqueConstraint("tenant_id", "employee_id", "effective_from", name="uq_employee_salary_structure_effective"),
    )
    op.create_index("ix_employee_salary_structures_tenant_id", "employee_salary_structures", ["tenant_id"])
    op.create_index("ix_employee_salary_structures_branch_id", "employee_salary_structures", ["branch_id"])
    op.create_index("ix_employee_salary_structures_employee_id", "employee_salary_structures", ["employee_id"])

    op.create_table(
        "employee_payroll_runs",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("tenant_id", sa.Integer(), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False),
        sa.Column("branch_id", sa.Integer(), sa.ForeignKey("branches.id"), nullable=False),
        sa.Column("pay_year", sa.Integer(), nullable=False),
        sa.Column("pay_month", sa.Integer(), nullable=False),
        sa.Column("run_name", sa.String(length=150), nullable=False),
        sa.Column("status", sa.String(length=30), nullable=False, server_default="draft"),
        sa.Column("total_employees", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("gross_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("deduction_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("net_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("notes", sa.Text(), nullable=True),
        sa.Column("processed_by_user_id", sa.Integer(), sa.ForeignKey("users.id", ondelete="SET NULL"), 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("paid_by_user_id", sa.Integer(), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True),
        sa.Column("paid_at_utc", sa.DateTime(timezone=True), 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.UniqueConstraint("tenant_id", "branch_id", "pay_year", "pay_month", name="uq_employee_payroll_run_period"),
    )
    op.create_index("ix_employee_payroll_runs_tenant_id", "employee_payroll_runs", ["tenant_id"])
    op.create_index("ix_employee_payroll_runs_branch_id", "employee_payroll_runs", ["branch_id"])
    op.create_index("ix_employee_payroll_runs_period", "employee_payroll_runs", ["pay_year", "pay_month"])

    op.create_table(
        "employee_payslips",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("tenant_id", sa.Integer(), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False),
        sa.Column("branch_id", sa.Integer(), sa.ForeignKey("branches.id"), nullable=False),
        sa.Column("payroll_run_id", sa.Integer(), sa.ForeignKey("employee_payroll_runs.id", ondelete="CASCADE"), nullable=False),
        sa.Column("employee_id", sa.Integer(), sa.ForeignKey("employees.id", ondelete="CASCADE"), nullable=False),
        sa.Column("salary_structure_id", sa.Integer(), sa.ForeignKey("employee_salary_structures.id", ondelete="SET NULL"), nullable=True),
        sa.Column("pay_year", sa.Integer(), nullable=False),
        sa.Column("pay_month", sa.Integer(), nullable=False),
        sa.Column("basic_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("hra_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("allowance_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("gross_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("employee_pf_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("employee_esi_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("professional_tax_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("tds_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("other_deduction_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("deduction_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("net_amount", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("status", sa.String(length=30), nullable=False, server_default="generated"),
        sa.Column("remarks", sa.Text(), nullable=True),
        sa.Column("generated_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),
        sa.Column("updated_at_utc", sa.DateTime(timezone=True), nullable=False),
        sa.UniqueConstraint("tenant_id", "payroll_run_id", "employee_id", name="uq_employee_payslip_run_employee"),
    )
    op.create_index("ix_employee_payslips_tenant_id", "employee_payslips", ["tenant_id"])
    op.create_index("ix_employee_payslips_branch_id", "employee_payslips", ["branch_id"])
    op.create_index("ix_employee_payslips_payroll_run_id", "employee_payslips", ["payroll_run_id"])
    op.create_index("ix_employee_payslips_employee_id", "employee_payslips", ["employee_id"])


def downgrade() -> None:
    op.drop_table("employee_payslips")
    op.drop_table("employee_payroll_runs")
    op.drop_table("employee_salary_structures")
