# ============================================================
# ADDITIVE: Time, Biometrics & Perimeter Security - shared logic
# ------------------------------------------------------------
# Used by People/views_att.py (Admin / HR), People/views_att_device.py (device
# ingestion) and People/views_att_self.py (employee / manager).
# Every path that creates a punch writes the EXISTING EMP_check_in_check_out
# model (the same record the existing /employee/check-in/ creates) plus a
# 1:1 AttendancePunchMeta row that remembers HOW the punch happened.
# ============================================================

import hashlib
import ipaddress
import math
import secrets
from collections import Counter, defaultdict
from datetime import datetime, time, timedelta
from decimal import Decimal

from django.db.models import Count, Q
from django.utils import timezone

from AdminCenter.models import IntegrationConnector
from AdminCenter.services import ping_endpoint
from Employee.leave_models import ReportingHierarchy, WFHPolicy, WFHRequest
from Employee.models import AttendanceEmp, EMP_check_in_check_out, EmployeeDT

from .common import dept_name, emp_brief, iso
from .att_models import (AttendanceGeofence, AttendancePolicy, AttendancePunchMeta, AttendanceRegularization,
                         BiometricDevice, BiometricEnrollment, BiometricPunch, EsslSource, FieldForceAgent, FieldTrackPoint,
                         PerimeterEvent)
from .reports_engine import parse_minutes

COMPLIANT = ("In-Perimeter Verified", "Authorized WFH", "Field Force", "Device Verified", "Regularized", "Manual Entry")
INSIDE = ("In-Perimeter Verified", "Device Verified", "Field Force")
VIOLATIONS = ("Blocked by Perimeter Lock", "Redirected to WFH Portal")
GATEWAY_NAME = "Biometric Hardware Gateway"


class PunchConflict(Exception):
    """A punch cannot be applied (already checked in / nothing to check out)."""


# ------------------------------------------------------------
# small helpers
# ------------------------------------------------------------

def fnum(v):
    return None if v is None else float(v)


def day_range(day):
    a = timezone.make_aware(datetime.combine(day, time.min))
    return a, a + timedelta(days=1)


def fmt12(t):
    """'09:28:14' | time -> '09:28 AM'"""
    if not t:
        return None
    if isinstance(t, str):
        m = parse_minutes(t)
        if m is None:
            return t
        t = time(m // 60, m % 60)
    return t.strftime("%I:%M %p")


def fmt_time24(t):
    return t.strftime("%H:%M:%S")


def ago(dt, now=None):
    if not dt:
        return None
    s = int(((now or timezone.now()) - dt).total_seconds())
    if s < 60:
        return "Just now"
    if s < 3600:
        return f"{s // 60} mins ago"
    if s < 86400:
        return f"{s // 3600} hrs ago"
    return f"{s // 86400} days ago"


def gps_label(lat, lng):
    if lat is None or lng is None:
        return None
    return f"{abs(lat):.4f}° {'N' if lat >= 0 else 'S'}, {abs(lng):.4f}° {'E' if lng >= 0 else 'W'}"


def haversine_m(lat1, lon1, lat2, lon2):
    r = 6371000.0
    p1, p2 = math.radians(lat1), math.radians(lat2)
    dp, dl = p2 - p1, math.radians(lon2 - lon1)
    a = math.sin(dp / 2) ** 2 + math.cos(p1) * math.cos(p2) * math.sin(dl / 2) ** 2
    return 2 * r * math.asin(math.sqrt(a))


def ip_in_ranges(ip, ranges):
    try:
        addr = ipaddress.ip_address(str(ip))
    except ValueError:
        return False
    for r in ranges or []:
        try:
            if addr in ipaddress.ip_network(str(r), strict=False):
                return True
        except ValueError:
            continue
    return False


def client_ip(request):
    fwd = request.META.get("HTTP_X_FORWARDED_FOR")
    ip = (fwd.split(",")[0].strip() if fwd else request.META.get("REMOTE_ADDR")) or None
    try:
        ipaddress.ip_address(ip)
        return ip
    except (ValueError, TypeError):
        return None


# ------------------------------------------------------------
# policy / geofences
# ------------------------------------------------------------

def get_policy(company):
    """Saved policy or the (unsaved) defaults."""
    p = AttendancePolicy.objects.filter(company=company, is_active=True).first()
    return p or AttendancePolicy(company=company)


def policy_row(p):
    return {"id": p.pk, "configured": p.pk is not None, "shift_start": p.shift_start.strftime("%H:%M"),
            "shift_end": p.shift_end.strftime("%H:%M"), "grace_minutes": p.grace_minutes,
            "early_leave_grace_minutes": p.early_leave_grace_minutes, "perimeter_enforced": p.perimeter_enforced,
            "allow_web_punch": p.allow_web_punch, "allow_mobile_punch": p.allow_mobile_punch,
            "wfh_redirect_enabled": p.wfh_redirect_enabled, "field_ping_interval_minutes": p.field_ping_interval_minutes,
            "device_online_minutes": p.device_online_minutes, "ingestion_mode": p.ingestion_mode,
            "reconcile_interval_seconds": p.reconcile_interval_seconds,
            "late_cutoff": (datetime.combine(timezone.localdate(), p.shift_start) + timedelta(minutes=p.grace_minutes)).strftime("%H:%M")}


def late_cutoff_minutes(policy):
    return policy.shift_start.hour * 60 + policy.shift_start.minute + policy.grace_minutes


def early_cutoff_minutes(policy):
    return policy.shift_end.hour * 60 + policy.shift_end.minute - policy.early_leave_grace_minutes


def geofence_dict(g):
    return {"id": g.id, "name": g.name, "branch": g.branch.name if g.branch_id else None, "latitude": g.latitude,
            "longitude": g.longitude, "radius_m": g.radius_m, "ip_ranges": list(g.ip_ranges or []),
            "is_primary": g.is_primary, "source": "saved"}


def geofences(company):
    """Saved geofences; else the company-profile office location (existing company_details fields)."""
    saved = list(AttendanceGeofence.objects.filter(company=company, is_active=True).select_related("branch"))
    if saved:
        return [geofence_dict(g) for g in saved]
    if company.office_latitude is not None and company.office_longitude is not None:
        return [{"id": None, "name": "Company office (from company profile)", "branch": None,
                 "latitude": company.office_latitude, "longitude": company.office_longitude,
                 "radius_m": company.allowed_radius or 200, "ip_ranges": [], "is_primary": True, "source": "company_profile"}]
    return []


def evaluate_perimeter(company, lat, lng, ip=None):
    """Dual verification: GPS inside the radius AND (when the hub lists Wi-Fi subnets) a whitelisted IP."""
    fences = geofences(company)
    out = {"configured": bool(fences), "latitude": lat, "longitude": lng, "network_ip": ip, "inside": False,
           "within_radius": None, "ip_whitelisted": None, "distance_m": None, "geofence": None, "reason": None}
    if not fences:
        out["reason"] = "No office perimeter is configured."
        return out
    if lat is None or lng is None:
        out["reason"] = "GPS coordinates were not provided."
        out["geofence"] = fences[0]
        return out
    best = min(fences, key=lambda f: haversine_m(lat, lng, f["latitude"], f["longitude"]))
    d = haversine_m(lat, lng, best["latitude"], best["longitude"])
    gps_ok = d <= best["radius_m"]
    ranges = best["ip_ranges"]
    ip_ok = ip_in_ranges(ip, ranges) if ranges else None
    inside = gps_ok and (ip_ok is not False)
    out.update(geofence=best, distance_m=round(d, 1), within_radius=gps_ok, ip_whitelisted=ip_ok, inside=inside)
    if not gps_ok:
        far = f"{d / 1000:.1f} km" if d >= 1000 else f"{d:.0f} m"
        out["reason"] = f"Outside authorized Wi-Fi & {best['radius_m']}m GPS radius ({far})"
    elif ip_ok is False:
        out["reason"] = "Inside the GPS radius but the network IP is not in the authorized Wi-Fi subnets"
    return out


# ------------------------------------------------------------
# WFH (existing WFHPolicy / WFHRequest)
# ------------------------------------------------------------

def wfh_policy(company):
    return WFHPolicy.objects.filter(company=company).first()


def approved_wfh_today(company, code, day):
    return WFHRequest.objects.filter(company=company, Employee_ID=code, wfh_date=day, status="Approved").exists()


def wfh_week_used(company, code, day):
    monday = day - timedelta(days=day.weekday())
    return WFHRequest.objects.filter(company=company, Employee_ID=code, status="Approved",
                                     wfh_date__gte=monday, wfh_date__lte=monday + timedelta(days=6)).count()


def wfh_remaining(company, code, day):
    p = wfh_policy(company)
    if p is None or not p.is_enabled:
        return 0
    return max(0, p.weekly_quota_days - wfh_week_used(company, code, day))


# ------------------------------------------------------------
# punch decision (web / mobile)
# ------------------------------------------------------------

def decide_punch(company, employee, lat, lng, ip, day):
    pol = get_policy(company)
    ev = evaluate_perimeter(company, lat, lng, ip)
    res = {"allowed": False, "perimeter_status": None, "event_status": None, "reason": ev["reason"], "evaluation": ev,
           "wfh_today": approved_wfh_today(company, employee.employee_code, day),
           "wfh_remaining_this_week": wfh_remaining(company, employee.employee_code, day),
           "policy_action": None}
    if not pol.perimeter_enforced:
        res.update(allowed=True, perimeter_status="Unverified", policy_action="Allow Web / Mobile Attendance Punch (perimeter not enforced)")
    elif ev["inside"]:
        res.update(allowed=True, perimeter_status="In-Perimeter Verified", policy_action="Allow Web / Mobile Attendance Punch")
    elif res["wfh_today"]:
        res.update(allowed=True, perimeter_status="Authorized WFH", policy_action="Allow punch - approved Work From Home today")
    else:
        redirect = pol.wfh_redirect_enabled and res["wfh_remaining_this_week"] > 0
        res["event_status"] = "Redirected to WFH Portal" if redirect else "Blocked by Perimeter Lock"
        res["policy_action"] = ("Redirect to WFH portal - request Work From Home" if redirect else "Block punch - Perimeter Lock")
        res["reason"] = (res["reason"] or "Outside the office perimeter") + ("" if res["wfh_today"] else " without approved WFH")
    return res


def log_event(company, employee, action, decision, ip, lat, lng):
    ev = decision["evaluation"]
    return PerimeterEvent.objects.create(
        company=company, employee=employee, employee_code=employee.employee_code if employee else None,
        event_time=timezone.now(), attempted_action=action, detected_ip=ip, latitude=lat, longitude=lng,
        distance_m=ev.get("distance_m"), geofence_id=(ev.get("geofence") or {}).get("id"),
        breach_reason=decision["reason"], status=decision["event_status"], created_by=f"employee:{employee.employee_code}" if employee else None)


# ------------------------------------------------------------
# punches -> EMP_check_in_check_out (+ meta)
# ------------------------------------------------------------

def _hours(login_date, login_time, logout_date, logout_time):
    from Employee.views import _working_hours_str       # the existing helper (same format as /employee/check-out/)
    return _working_hours_str(login_date, login_time, logout_date, logout_time)


def open_record(company, code, day):
    return (EMP_check_in_check_out.objects.filter(company=company, Employee_ID=code, logout_time__isnull=True,
                                                  is_active=True, login_date__gte=day - timedelta(days=1))
            .order_by("-id").first())


def create_punch_in(company, employee, when, *, source, verdict, lat=None, lng=None, device=None, geofence_id=None,
                    distance=None, ip=None, comment=None, actor=None, regularization_id=None):
    when = timezone.localtime(when)
    if EMP_check_in_check_out.objects.filter(company=company, Employee_ID=employee.employee_code, login_date=when.date(),
                                             logout_time__isnull=True, is_active=True).exists():
        raise PunchConflict("Already checked in - check out first.")
    inside = verdict in INSIDE
    rec = EMP_check_in_check_out.objects.create(
        Employee_ID=employee.employee_code, company=company, Name=employee.name, login_date=when.date(),
        login_time=fmt_time24(when.time()), checkin_latitude=lat, checkin_longitude=lng,
        checkin_location_type="office" if inside else "outside", comment=comment, created_by=actor, updated_by=actor)
    AttendancePunchMeta.objects.create(company=company, checkin=rec, source=source, perimeter_status=verdict, device=device,
                                       geofence_id=geofence_id, distance_m=distance, ip_address=ip,
                                       regularization_id=regularization_id, created_by=actor, updated_by=actor)
    employee.check_st, employee.Employ_check_id = "check_in", str(rec.id)
    employee.save(update_fields=["check_st", "Employ_check_id"])
    return rec


def close_punch(company, employee, when, *, source, lat=None, lng=None, inside=None, comment=None, actor=None):
    when = timezone.localtime(when)
    rec = open_record(company, employee.employee_code, when.date())
    if rec is None:
        raise PunchConflict("No open check-in found.")
    rec.logout_date, rec.logout_time = when.date(), fmt_time24(when.time())
    rec.total_working_hours = _hours(rec.login_date, rec.login_time, rec.logout_date, rec.logout_time)
    rec.checkout_latitude, rec.checkout_longitude = lat, lng
    rec.checkout_location_type = None if inside is None else ("office" if inside else "outside")
    rec.comment = comment or rec.comment
    rec.updated_by = actor
    rec.id_version = (rec.id_version or 1) + 1
    rec.save()
    AttendancePunchMeta.objects.filter(checkin=rec).update(checkout_source=source)
    employee.check_st, employee.Employ_check_id = None, None
    employee.save(update_fields=["check_st", "Employ_check_id"])
    return rec


# ------------------------------------------------------------
# biometric
# ------------------------------------------------------------

def hash_key(key):
    return hashlib.sha256(str(key).encode()).hexdigest()


def new_device_key(device_code):
    return f"bdk_{device_code.lower().replace('-', '')}_{secrets.token_urlsafe(24)}"


def device_for_key(key):
    if not key:
        return None
    return BiometricDevice.objects.filter(api_key_hash=hash_key(key), is_active=True).select_related("company").first()


def resolve_employee(company, biometric_id):
    e = (BiometricEnrollment.objects.filter(company=company, biometric_id=biometric_id, is_active=True)
         .select_related("employee").first())
    return e.employee if e else None


def reconcile(p, actor="device"):
    """Turn one raw BiometricPunch into a check-in / check-out record."""
    company = p.company
    emp = p.employee or resolve_employee(company, p.biometric_id)
    if emp is None:
        p.sync_status, p.sync_message = "Unmatched", f"No enrollment for biometric id '{p.biometric_id}'."
        p.save()
        return p
    p.employee = emp
    if not emp.is_active:
        p.sync_status, p.sync_message = "Failed", "Employee is not active."
        p.save()
        return p
    if (p.raw_payload or {}).get("auto_direction"):
        # the source (eSSL) did not say IN / OUT: ignore double scans, then toggle by the open check-in
        gap = int((p.raw_payload or {}).get("min_gap") or 0)
        if gap:
            prev = (BiometricPunch.objects.filter(company=company, employee=emp, sync_status="Reconciled",
                                                  punch_time__lte=p.punch_time).exclude(pk=p.pk).order_by("-punch_time").first())
            if prev and (p.punch_time - prev.punch_time) < timedelta(minutes=gap):
                p.sync_status, p.sync_message = "Duplicate", f"Double scan within {gap} minute(s) of the previous punch."
                p.save()
                return p
        p.direction = "OUT" if open_record(company, emp.employee_code, timezone.localtime(p.punch_time).date()) else "IN"
    try:
        if p.direction == "IN":
            rec = create_punch_in(company, emp, p.punch_time, source="Biometric Device", verdict="Device Verified",
                                  device=p.device, actor=actor)
        else:
            rec = close_punch(company, emp, p.punch_time, source="Biometric Device", inside=True, actor=actor)
        p.sync_status, p.sync_message, p.checkin = "Reconciled", None, rec
    except PunchConflict as e:
        p.sync_status = "Duplicate" if p.direction == "IN" else "Failed"
        p.sync_message = str(e)
    p.save()
    return p


def device_state(dev, policy, now=None):
    now = now or timezone.now()
    if dev.status != "Enabled":
        return dev.status
    if dev.last_seen_at and now - dev.last_seen_at <= timedelta(minutes=policy.device_online_minutes):
        return "Online"
    return "Offline"


def device_row(dev, policy, stats=None, now=None, with_state=True):
    now = now or timezone.now()
    s = (stats or {}).get(dev.id, {"total": 0, "ok": 0})
    return {
        "id": dev.id, "device_code": dev.device_code, "name": dev.name, "vendor": dev.vendor, "model_line": dev.name,
        "location_label": dev.location_label, "branch": dev.branch.name if dev.branch_id else None,
        "ip_address": dev.ip_address, "port": dev.port,
        "ip_port": f"{dev.ip_address}:{dev.port}" if dev.ip_address and dev.port else dev.ip_address,
        "sync_protocol": dev.sync_protocol, "firmware": dev.firmware, "status": dev.status,
        "connection": device_state(dev, policy, now), "last_seen_at": iso(dev.last_seen_at), "last_seen_label": ago(dev.last_seen_at, now),
        "last_sync_at": iso(dev.last_sync_at), "today_punches": s["total"],
        "operational_percent": round(s["ok"] * 100 / s["total"], 1) if s["total"] else 100.0,
        "api_key_hint": dev.api_key_hint, "last_ping": {"ok": dev.last_ping_ok, "ms": dev.last_ping_ms, "message": dev.last_ping_message},
        "created_at": iso(dev.created_at), "updated_at": iso(dev.updated_at)}


def device_stats(company, day):
    a, b = day_range(day)
    st = defaultdict(lambda: {"total": 0, "ok": 0})
    for did, status in BiometricPunch.objects.filter(company=company, punch_time__gte=a, punch_time__lt=b,
                                                     device__isnull=False).values_list("device_id", "sync_status"):
        st[did]["total"] += 1
        st[did]["ok"] += status in ("Reconciled", "Duplicate")
    return st


def gateway(company):
    return IntegrationConnector.objects.filter(company=company, connector_type="Biometric", is_active=True).order_by("id").first()


def gateway_row(company):
    c, p = gateway(company), get_policy(company)
    return {"connector_id": c.id if c else None, "name": c.name if c else GATEWAY_NAME,
            "central_gateway_url": c.endpoint if c else None, "connector_status": c.status if c else "Not Configured",
            "last_ping_at": iso(c.last_ping_at) if c else None, "last_ping_ms": c.last_ping_ms if c else None,
            "last_ping_message": c.last_ping_message if c else None, "ingestion_mode": p.ingestion_mode,
            "reconcile_interval_seconds": p.reconcile_interval_seconds,
            "auto_reconcile_schedule": ("Every 60 Seconds (Live Real-Time)" if p.reconcile_interval_seconds == 60
                                        else f"Every {p.reconcile_interval_seconds} Seconds")}


def ping_gateway(company):
    c = gateway(company)
    if c is None or not c.endpoint:
        raise ValueError("Set the central gateway URL first.")
    ok, ms, msg = ping_endpoint(c.endpoint)
    c.status = "Connected" if ok else "Error"
    c.last_ping_at, c.last_ping_ms, c.last_ping_message = timezone.now(), ms, msg[:300]
    c.save()
    return ok, ms, msg


def ping_device(dev):
    if not dev.ip_address:
        raise ValueError("The device has no IP address.")
    ok, ms, msg = ping_endpoint(f"{dev.ip_address}:{dev.port or 80}")
    dev.last_ping_ok, dev.last_ping_ms, dev.last_ping_message = ok, ms, msg[:300]
    if ok:
        dev.last_seen_at = timezone.now()
    dev.save()
    return ok, ms, msg


# ------------------------------------------------------------
# daily log rows (Daily Attendance tab)
# ------------------------------------------------------------

def infer_source(rec):
    if rec.checkin_latitude is not None and rec.checkin_longitude is not None:
        return "GPS Mobile Punch"
    return "Web Portal Punch"


def employee_map(company, codes=None):
    qs = EmployeeDT.objects.filter(company=company).select_related("department")
    if codes is not None:
        qs = qs.filter(employee_code__in=codes)
    return {e.employee_code: e for e in qs}


def punch_row(rec, meta, emp, policy, request=None):
    cut = late_cutoff_minutes(policy)
    arrive = parse_minutes(rec.login_time)
    night = emp is not None and emp.Work_sift == "Night Shift"
    late = (not night) and arrive is not None and arrive > cut
    source = meta.source if meta else infer_source(rec)
    dev = meta.device if meta and meta.device_id else None
    mode = f"{source} ({dev.branch.name if dev and dev.branch_id else dev.name})" if dev else source
    return {
        "id": rec.id, "date": iso(rec.login_date), "employee": emp_brief(emp, request) if emp else {"employee_code": rec.Employee_ID, "name": rec.Name},
        "punch_in": rec.login_time, "punch_in_label": fmt12(rec.login_time), "punch_out": rec.logout_time,
        "punch_out_label": fmt12(rec.logout_time) if rec.logout_time else "--", "total_working_hours": rec.total_working_hours,
        "source": source, "device_mode": mode, "device": dev.device_code if dev else None,
        "perimeter_status": meta.perimeter_status if meta else "Unverified", "meta_recorded": meta is not None,
        "distance_m": fnum(meta.distance_m) if meta else None,
        "gps": {"latitude": rec.checkin_latitude, "longitude": rec.checkin_longitude, "label": gps_label(rec.checkin_latitude, rec.checkin_longitude)},
        "location_type": rec.checkin_location_type, "status": "Late" if late else "Present", "late": late,
        "comment": rec.comment, "created_at": iso(rec.created_at) if hasattr(rec, "created_at") else None}


# ------------------------------------------------------------
# late / early
# ------------------------------------------------------------

def late_early_rows(company, start, end, codes=None, kind=None, policy=None):
    policy = policy or get_policy(company)
    lc, ec = late_cutoff_minutes(policy), early_cutoff_minutes(policy)
    emap = employee_map(company, codes)
    qs = EMP_check_in_check_out.objects.filter(company=company, is_active=True, login_date__gte=start, login_date__lte=end)
    if codes is not None:
        qs = qs.filter(Employee_ID__in=codes)
    per = defaultdict(list)
    for r in qs.order_by("login_date", "id"):
        per[(r.Employee_ID, r.login_date)].append(r)
    regs = {(r.employee.employee_code, r.attendance_date): r for r in AttendanceRegularization.objects.filter(
        company=company, is_active=True, attendance_date__gte=start, attendance_date__lte=end,
        status__in=["Pending Approval", "Approved"]).select_related("employee")}
    rows = []
    for (code, d), recs in sorted(per.items(), key=lambda kv: (kv[0][1], kv[0][0]), reverse=True):
        emp = emap.get(code)
        if emp is None or emp.Work_sift == "Night Shift":
            continue
        first = min((parse_minutes(r.login_time) for r in recs if parse_minutes(r.login_time) is not None), default=None)
        outs = [parse_minutes(r.logout_time) for r in recs if r.logout_time and parse_minutes(r.logout_time) is not None]
        reg = regs.get((code, d))
        base = {"employee": {"employee_code": code, "name": emp.name}, "employee_label": f"{emp.name} ({code})", "date": iso(d),
                "regularization": ({"request_id": reg.request_id, "status": reg.status} if reg else None),
                "action": "Request Regularization" if not reg else f"Regularization {reg.status}"}
        if first is not None and first > lc and kind in (None, "late"):
            by = first - (lc - policy.grace_minutes)
            rows.append({**base, "type": "Late Arrival", "recorded_arrival": fmt12(time(first // 60, first % 60)),
                         "scheduled_shift": fmt12(policy.shift_start), "grace_period": f"{policy.grace_minutes} Mins Exceeded",
                         "late_by_minutes": by, "grace_minutes": policy.grace_minutes})
        if outs and max(outs) < ec and kind in (None, "early"):
            m = max(outs)
            rows.append({**base, "type": "Early Departure", "recorded_departure": fmt12(time(m // 60, m % 60)),
                         "scheduled_shift_end": fmt12(policy.shift_end), "grace_period": f"{policy.early_leave_grace_minutes} Mins Exceeded",
                         "early_by_minutes": (ec + policy.early_leave_grace_minutes) - m, "grace_minutes": policy.early_leave_grace_minutes})
    return rows


# ------------------------------------------------------------
# regularization
# ------------------------------------------------------------

def reg_row(r):
    e = r.employee
    return {"id": r.id, "request_id": r.request_id, "employee": {"employee_code": e.employee_code, "name": e.name},
            "employee_label": f"{e.name} ({e.employee_code})", "date": iso(r.attendance_date),
            "punch_time": fmt12(r.punch_in_time), "punch_in_time": r.punch_in_time.strftime("%H:%M:%S"),
            "punch_out_time": r.punch_out_time.strftime("%H:%M:%S") if r.punch_out_time else None, "reason": r.reason,
            "status": r.status, "requested_by": r.requested_by, "reviewed_by": r.reviewed_by, "reviewed_at": iso(r.reviewed_at),
            "review_comment": r.review_comment, "applied_checkin_id": r.applied_checkin_id,
            "can_approve": r.status == "Pending Approval", "created_at": iso(r.created_at), "updated_at": iso(r.updated_at)}


def apply_regularization(reg, actor):
    """Approved request -> punch record (+ day status for past days)."""
    emp, d, company = reg.employee, reg.attendance_date, reg.company
    in_s = fmt_time24(reg.punch_in_time)
    out_s = fmt_time24(reg.punch_out_time) if reg.punch_out_time else None
    rec = (EMP_check_in_check_out.objects.filter(company=company, Employee_ID=emp.employee_code, login_date=d, is_active=True)
           .order_by("id").first())
    if rec is None:
        rec = EMP_check_in_check_out.objects.create(Employee_ID=emp.employee_code, company=company, Name=emp.name,
                                                    login_date=d, login_time=in_s, created_by=actor, updated_by=actor)
    else:
        rec.login_time = in_s
    if out_s:
        rec.logout_date, rec.logout_time = d, out_s
        rec.total_working_hours = _hours(d, in_s, d, out_s)
    rec.comment = f"Regularized {reg.request_id}: {reg.reason}"[:500]
    rec.updated_by = actor
    rec.save()
    AttendancePunchMeta.objects.update_or_create(
        checkin=rec, defaults={"company": company, "source": "Regularization", "perimeter_status": "Regularized",
                               "regularization_id": reg.id, "updated_by": actor, "created_by": actor})
    today = timezone.localdate()
    if d < today:                                         # the scheduler only fills yesterday - keep payroll consistent
        status = "Present"
        if rec.total_working_hours:
            try:
                from Company.utils_payroll_export import time_to_decimal
                if time_to_decimal(rec.total_working_hours) < (emp.Work_hours or 8) / 2:
                    status = "Half Day"
            except Exception:
                pass
        row = AttendanceEmp.objects.filter(company=company, Employee_ID=emp.employee_code, date=d).first()
        if row is None:
            AttendanceEmp.objects.create(Employee_ID=emp.employee_code, company=company, Name=emp.name, date=d, status=status,
                                         total_working_hours=rec.total_working_hours, comment=f"Regularized {reg.request_id}",
                                         check_in_id=rec, created_by=actor)
        else:
            row.status, row.total_working_hours, row.comment, row.check_in_id = status, rec.total_working_hours, f"Regularized {reg.request_id}", rec
            row.updated_by = actor
            row.save()
    return rec


# ------------------------------------------------------------
# field force
# ------------------------------------------------------------

def agent_row(a):
    e = a.employee
    return {"id": a.id, "employee": {"employee_code": e.employee_code, "name": e.name, "designation": e.Designation},
            "label": f"{e.name} ({e.Designation})", "territory": a.territory, "ping_interval_minutes": a.ping_interval_minutes,
            "per_km_rate": fnum(a.per_km_rate), "created_at": iso(a.created_at), "updated_at": iso(a.updated_at)}


def point_row(p):
    return {"id": p.id, "agent_id": p.agent_id, "time": timezone.localtime(p.recorded_at).strftime("%I:%M %p"),
            "recorded_at": iso(p.recorded_at), "type": p.point_type, "location_label": p.location_label,
            "client_name": p.client_name, "latitude": p.latitude, "longitude": p.longitude,
            "coordinates": gps_label(p.latitude, p.longitude), "speed_kmh": fnum(p.speed_kmh),
            "battery_percent": p.battery_percent, "notes": p.notes, "site_verified": p.site_verified}


def agent_day(agent, day):
    a, b = day_range(day)
    pts = list(FieldTrackPoint.objects.filter(agent=agent, is_active=True, recorded_at__gte=a, recorded_at__lt=b).order_by("recorded_at", "id"))
    dist, cum, trail = 0.0, 0.0, []
    for i, p in enumerate(pts):
        if i:
            cum += haversine_m(pts[i - 1].latitude, pts[i - 1].longitude, p.latitude, p.longitude)
        trail.append({**point_row(p), "cumulative_km": round(cum / 1000, 2)})
    dist = cum / 1000
    visits = [p for p in pts if p.point_type == "Client Check-In"]
    last = pts[-1] if pts else None
    claim = Decimal(str(round(dist, 2))) * agent.per_km_rate
    return {"agent": agent_row(agent), "date": str(day), "total_distance_km": round(dist, 1),
            "total_distance_label": f"{dist:.1f} km", "verified_client_visits": sum(1 for v in visits if v.site_verified),
            "client_visits": len(visits), "points": len(pts),
            "battery_percent": last.battery_percent if last else None,
            "ping_status": f"Ping Every {agent.ping_interval_minutes} Mins",
            "last_ping_at": iso(last.recorded_at) if last else None,
            "travel_claim_amount": float(claim.quantize(Decimal("0.01"))), "trail": trail}


# ------------------------------------------------------------
# dashboard
# ------------------------------------------------------------

def dashboard(company, day=None, request=None):
    now = timezone.now()
    day = day or timezone.localdate()
    pol = get_policy(company)
    a, b = day_range(day)
    recs = list(EMP_check_in_check_out.objects.filter(company=company, login_date=day, is_active=True))
    metas = {m.checkin_id: m for m in AttendancePunchMeta.objects.filter(checkin_id__in=[r.id for r in recs])}
    punches = len(recs) + sum(1 for r in recs if r.logout_time)

    bio = Counter(BiometricPunch.objects.filter(company=company, punch_time__gte=a, punch_time__lt=b)
                  .values_list("sync_status", flat=True))
    bio_total = sum(bio.values())
    synced = bio["Reconciled"] + bio["Duplicate"]

    src = Counter()
    for r in recs:
        m = metas.get(r.id)
        s = m.source if m else infer_source(r)
        src["biometric" if s == "Biometric Device" else "mobile_gps" if s == "GPS Mobile Punch"
            else "web_portal" if s == "Web Portal Punch" else "other"] += 1

    def verdict(r):
        m = metas.get(r.id)
        return m.perimeter_status if m else "Unverified"

    compliant = sum(1 for r in recs if verdict(r) in COMPLIANT)
    viol_today = PerimeterEvent.objects.filter(company=company, is_active=True, event_time__gte=a, event_time__lt=b,
                                               status__in=VIOLATIONS).count()
    compliance = round(compliant * 100 / (compliant + viol_today), 1) if (compliant + viol_today) else None
    radius = (geofences(company) or [{"radius_m": 200}])[0]["radius_m"]

    # field force
    agents = list(FieldForceAgent.objects.filter(company=company, is_active=True).select_related("employee"))
    active_field = 0
    for ag in agents:
        last = FieldTrackPoint.objects.filter(agent=ag, is_active=True, recorded_at__gte=a, recorded_at__lt=b).order_by("-recorded_at").first()
        if last and (day != timezone.localdate() or now - last.recorded_at <= timedelta(minutes=ag.ping_interval_minutes * 2)):
            active_field += 1

    # WFH
    wfh_today = list(WFHRequest.objects.filter(company=company, wfh_date=day, status="Approved").values_list("Employee_ID", flat=True))
    wp = wfh_policy(company)
    quota = wp.weekly_quota_days if wp else None
    over = [c for c in set(wfh_today) if quota is not None and wfh_week_used(company, c, day) > quota]

    # weekly chart (Mon..day)
    monday = day - timedelta(days=day.weekday())
    week = []
    for i in range((day - monday).days + 1):
        d = monday + timedelta(days=i)
        da, db = day_range(d)
        rr = list(EMP_check_in_check_out.objects.filter(company=company, login_date=d, is_active=True).values_list("id", flat=True))
        mm = Counter(AttendancePunchMeta.objects.filter(checkin_id__in=rr).values_list("perimeter_status", flat=True))
        week.append({"date": str(d), "weekday": d.strftime("%a"),
                     "inside_perimeter": sum(mm[k] for k in INSIDE) + mm["Regularized"] + mm["Manual Entry"],
                     "authorized_wfh": mm["Authorized WFH"],
                     "blocked_out_of_bounds": PerimeterEvent.objects.filter(company=company, is_active=True, event_time__gte=da,
                                                                           event_time__lt=db, status__in=VIOLATIONS).count(),
                     "unverified": (len(rr) - sum(mm.values()))})

    srcs = list(EsslSource.objects.filter(company=company, is_active=True, enabled=True))
    essl_rows = [{"id": x.id, "name": x.name, "last_status": x.last_status, "last_sync_at": iso(x.last_sync_at),
                  "last_sync_label": ago(x.last_sync_at, now)} for x in srcs]
    essl_live = any(x.last_status in ("Success", "Running") and x.last_sync_at and
                    now - x.last_sync_at <= timedelta(seconds=max(x.poll_interval_seconds * 5, 300)) for x in srcs)
    devs = list(BiometricDevice.objects.filter(company=company, is_active=True).select_related("branch"))
    stats = device_stats(company, day)
    cards = [device_row(d, pol, stats, now) for d in devs]
    online = sum(1 for c in cards if c["connection"] == "Online")
    vendors = sorted({d.vendor for d in devs})
    return {
        "title": "Time, Biometrics & Perimeter Security",
        "subtitle": "Real-time biometric device sync, Wi-Fi & GPS perimeter locking, and 30-min field workforce tracking",
        "date": str(day),
        "badges": {"perimeter_engine": "Perimeter Engine Active" if pol.perimeter_enforced else "Perimeter Engine Off",
                   "hardware_devices_synced": f"{online} Hardware Devices Synced", "devices_total": len(devs), "devices_online": online},
        "sync_engine": {"title": "Hardware Sync Engine: " + ("Live API/SQL Connected" if (online or essl_live) else "No device online"),
                        "vendors": vendors, "caption": " • ".join(vendors) + " • Real-time synchronization active without manual reconciliation"
                        if vendors else "No biometric devices registered", "connected": bool(online or essl_live),
                        "essl_sources": essl_rows},
        "cards": {
            "todays_verified_punches": {"title": "Today's Verified Punches", "value": punches, "label": f"{punches:,}",
                                        "caption": (f"{round(synced * 100 / bio_total)}% Synced via Hardware API" if bio_total else "No hardware punches yet"),
                                        "biometric_punches": bio_total, "synced_percent": round(synced * 100 / bio_total, 1) if bio_total else None},
            "perimeter_compliance": {"title": "Perimeter Compliance", "value": compliance, "label": f"{compliance:g}%" if compliance is not None else "-",
                                     "caption": f"Strict Wi-Fi & {radius}m GPS Enforced" if pol.perimeter_enforced else "Perimeter not enforced",
                                     "violations_today": viol_today},
            "active_field_force": {"title": "Active Field Force", "value": active_field, "label": f"{active_field} Active",
                                   "caption": f"{(agents[0].ping_interval_minutes if agents else pol.field_ping_interval_minutes)} min Continuous Interval Ping",
                                   "agents": len(agents)},
            "approved_wfh_today": {"title": "Approved WFH Today", "value": len(wfh_today), "label": f"{len(wfh_today)} Employees",
                                   "caption": (f"{quota} Day/Week Quota Compliant" if not over and quota is not None else
                                               f"{len(over)} over the weekly quota" if over else "WFH policy not configured")}},
        "punch_source_breakdown": {"title": "Multi-Device Punch Source Breakdown", "badge": "Real-Time", "total": len(recs),
                                   "series": [{"key": "biometric", "label": "Biometric Devices (Physical)", "count": src["biometric"]},
                                              {"key": "mobile_gps", "label": "Mobile GPS Geofenced Punch", "count": src["mobile_gps"]},
                                              {"key": "web_portal", "label": "Web Portal Office Wi-Fi", "count": src["web_portal"]},
                                              {"key": "other", "label": "Field / Regularization / Manual", "count": src["other"]}]},
        "perimeter_audit": {"title": "Perimeter Security & Geofence Audit",
                            "badge": "Zero Breaches Today" if not viol_today else f"{viol_today} Breaches Today", "weekly": week},
        "hardware_network": {"title": "Physical Biometric Hardware Network" + (f" ({' • '.join(vendors)})" if vendors else ""),
                             "caption": "Direct API / SQL connection eliminating end-of-month manual reconciliation errors", "devices": cards},
        "quick_links": [{"key": "geofence", "title": "Geofenced Check-In Terminal", "endpoint": "/api/attendance-hub/geofences/simulate/"},
                        {"key": "field", "title": "Field Force Geo-Tracking", "endpoint": "/api/attendance-hub/field/summary/"},
                        {"key": "wfh", "title": "WFH & Remote Policy Engine", "endpoint": "/api/leave-hub/"}],
    }


def decide_regularization(reg, status, comment, actor):
    """Approve / reject a request (approval writes the punch record)."""
    reg.status, reg.reviewed_by, reg.reviewed_at, reg.review_comment, reg.updated_by = status, actor, timezone.now(), comment, actor
    if status == "Approved":
        reg.applied_checkin = apply_regularization(reg, actor)
    reg.save()
    return reg
