"""Phase 6C employee attendance foundation

Revision ID: 20260515_phase_6c_employee_attendance
Revises: 20260514_phase_6b_employee_ess_registration
Create Date: 2026-05-10
"""
from __future__ import annotations

from alembic import op
import sqlalchemy as sa

revision = "20260515_phase_6c_employee_attendance"
down_revision = "20260514_phase_6b_employee_ess_registration"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "employee_attendance",
        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("user_id", sa.Integer(), nullable=True),
        sa.Column("attendance_date", sa.Date(), nullable=False),
        sa.Column("punch_in_utc", sa.DateTime(timezone=True), nullable=True),
        sa.Column("punch_out_utc", sa.DateTime(timezone=True), nullable=True),
        sa.Column("work_duration_minutes", sa.Integer(), nullable=True),
        sa.Column("status", sa.String(length=30), nullable=False, server_default="present"),
        sa.Column("approval_status", sa.String(length=30), nullable=False, server_default="approved"),
        sa.Column("source", sa.String(length=30), nullable=False, server_default="self_punch"),
        sa.Column("remarks", sa.Text(), nullable=True),
        sa.Column("reviewed_by_user_id", sa.Integer(), nullable=True),
        sa.Column("reviewed_at_utc", sa.DateTime(timezone=True), nullable=True),
        sa.Column("review_notes", sa.Text(), nullable=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(["branch_id"], ["branches.id"]),
        sa.ForeignKeyConstraint(["created_by_user_id"], ["users.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["employee_id"], ["employees.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["reviewed_by_user_id"], ["users.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["tenant_id"], ["tenants.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["updated_by_user_id"], ["users.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="SET NULL"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("tenant_id", "employee_id", "attendance_date", name="uq_employee_attendance_employee_date"),
    )
    for col in [
        "tenant_id",
        "branch_id",
        "employee_id",
        "user_id",
        "attendance_date",
        "status",
        "approval_status",
        "source",
    ]:
        op.create_index(f"ix_employee_attendance_{col}", "employee_attendance", [col], unique=False)


def downgrade() -> None:
    op.drop_table("employee_attendance")
