"""Phase 6K geo-fenced attendance and OD approval

Revision ID: 20260520_phase_6k_geo_attendance
Revises: 20260519_phase_6g_employee_payroll
Create Date: 2026-05-20
"""
from __future__ import annotations

from alembic import op
import sqlalchemy as sa

revision = "20260520_phase_6k_geo_attendance"
down_revision = "20260519_phase_6g_employee_payroll"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.add_column("branch_settings", sa.Column("attendance_geo_enabled", sa.Boolean(), nullable=False, server_default=sa.false()))
    op.add_column("branch_settings", sa.Column("attendance_geo_radius_meters", sa.Integer(), nullable=False, server_default="100"))
    op.add_column("branch_settings", sa.Column("attendance_ip_enabled", sa.Boolean(), nullable=False, server_default=sa.false()))
    op.add_column("branch_settings", sa.Column("attendance_allowed_ip_csv", sa.Text(), nullable=True))

    op.add_column("employee_attendance", sa.Column("punch_in_latitude", sa.Float(), nullable=True))
    op.add_column("employee_attendance", sa.Column("punch_in_longitude", sa.Float(), nullable=True))
    op.add_column("employee_attendance", sa.Column("punch_in_accuracy_meters", sa.Float(), nullable=True))
    op.add_column("employee_attendance", sa.Column("punch_in_distance_meters", sa.Float(), nullable=True))
    op.add_column("employee_attendance", sa.Column("punch_in_ip", sa.String(length=80), nullable=True))
    op.add_column("employee_attendance", sa.Column("punch_in_geo_status", sa.String(length=40), nullable=True))
    op.add_column("employee_attendance", sa.Column("punch_in_ip_status", sa.String(length=40), nullable=True))

    op.add_column("employee_attendance", sa.Column("punch_out_latitude", sa.Float(), nullable=True))
    op.add_column("employee_attendance", sa.Column("punch_out_longitude", sa.Float(), nullable=True))
    op.add_column("employee_attendance", sa.Column("punch_out_accuracy_meters", sa.Float(), nullable=True))
    op.add_column("employee_attendance", sa.Column("punch_out_distance_meters", sa.Float(), nullable=True))
    op.add_column("employee_attendance", sa.Column("punch_out_ip", sa.String(length=80), nullable=True))
    op.add_column("employee_attendance", sa.Column("punch_out_geo_status", sa.String(length=40), nullable=True))
    op.add_column("employee_attendance", sa.Column("punch_out_ip_status", sa.String(length=40), nullable=True))

    op.create_index("ix_employee_attendance_punch_in_geo_status", "employee_attendance", ["punch_in_geo_status"])
    op.create_index("ix_employee_attendance_punch_in_ip_status", "employee_attendance", ["punch_in_ip_status"])
    op.create_index("ix_employee_attendance_punch_out_geo_status", "employee_attendance", ["punch_out_geo_status"])
    op.create_index("ix_employee_attendance_punch_out_ip_status", "employee_attendance", ["punch_out_ip_status"])


def downgrade() -> None:
    op.drop_index("ix_employee_attendance_punch_out_ip_status", table_name="employee_attendance")
    op.drop_index("ix_employee_attendance_punch_out_geo_status", table_name="employee_attendance")
    op.drop_index("ix_employee_attendance_punch_in_ip_status", table_name="employee_attendance")
    op.drop_index("ix_employee_attendance_punch_in_geo_status", table_name="employee_attendance")

    for col in [
        "punch_out_ip_status", "punch_out_geo_status", "punch_out_ip", "punch_out_distance_meters",
        "punch_out_accuracy_meters", "punch_out_longitude", "punch_out_latitude",
        "punch_in_ip_status", "punch_in_geo_status", "punch_in_ip", "punch_in_distance_meters",
        "punch_in_accuracy_meters", "punch_in_longitude", "punch_in_latitude",
    ]:
        op.drop_column("employee_attendance", col)

    op.drop_column("branch_settings", "attendance_allowed_ip_csv")
    op.drop_column("branch_settings", "attendance_ip_enabled")
    op.drop_column("branch_settings", "attendance_geo_radius_meters")
    op.drop_column("branch_settings", "attendance_geo_enabled")
