"""Phase 6L branch timezone and attendance timing rules

Revision ID: 20260521_phase_6l_branch_timezone_attendance_rules
Revises: 20260520_phase_6k_geo_attendance
Create Date: 2026-05-21
"""
from __future__ import annotations

from alembic import op
import sqlalchemy as sa

revision = "20260521_phase_6l_branch_timezone_attendance_rules"
down_revision = "20260520_phase_6k_geo_attendance"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.add_column("branch_settings", sa.Column("attendance_grace_minutes", sa.Integer(), nullable=False, server_default="10"))
    op.add_column("branch_settings", sa.Column("attendance_half_day_after_time", sa.Time(), nullable=True))
    op.add_column("branch_settings", sa.Column("attendance_rule_enabled", sa.Boolean(), nullable=False, server_default=sa.true()))

    op.add_column("employee_attendance", sa.Column("punch_in_local_at", sa.DateTime(timezone=False), nullable=True))
    op.add_column("employee_attendance", sa.Column("punch_out_local_at", sa.DateTime(timezone=False), nullable=True))
    op.add_column("employee_attendance", sa.Column("branch_timezone", sa.String(length=64), nullable=False, server_default="Asia/Kolkata"))
    op.add_column("employee_attendance", sa.Column("scheduled_start_local", sa.Time(), nullable=True))
    op.add_column("employee_attendance", sa.Column("scheduled_end_local", sa.Time(), nullable=True))
    op.add_column("employee_attendance", sa.Column("late_by_minutes", sa.Integer(), nullable=True))
    op.add_column("employee_attendance", sa.Column("attendance_rule_status", sa.String(length=40), nullable=True))
    op.add_column("employee_attendance", sa.Column("is_weekly_off", sa.Boolean(), nullable=False, server_default=sa.false()))
    op.create_index("ix_employee_attendance_attendance_rule_status", "employee_attendance", ["attendance_rule_status"])


def downgrade() -> None:
    op.drop_index("ix_employee_attendance_attendance_rule_status", table_name="employee_attendance")
    for col in [
        "is_weekly_off",
        "attendance_rule_status",
        "late_by_minutes",
        "scheduled_end_local",
        "scheduled_start_local",
        "branch_timezone",
        "punch_out_local_at",
        "punch_in_local_at",
    ]:
        op.drop_column("employee_attendance", col)

    op.drop_column("branch_settings", "attendance_rule_enabled")
    op.drop_column("branch_settings", "attendance_half_day_after_time")
    op.drop_column("branch_settings", "attendance_grace_minutes")
