<?php

namespace App\Models;

use App\Models\Concerns\BelongsToCompany;

class AttendancePolicy extends Model
{
    use BelongsToCompany;

    public const MODE_NORMAL = 'normal';

    public const MODE_OFFICE_GEOFENCE = 'office_geofence';

    public const MODE_REMOTE = 'remote';

    public const MODE_HYBRID = 'hybrid';

    public const MODES = [
        self::MODE_NORMAL,
        self::MODE_OFFICE_GEOFENCE,
        self::MODE_REMOTE,
        self::MODE_HYBRID,
    ];

    public const BIOMETRIC_MODE_FIRST_IN_LAST_OUT = 'FIRST_IN_LAST_OUT';

    public const BIOMETRIC_MODE_PAIRWISE = 'PAIRWISE_IN_OUT';

    public const DEFAULT_RADIUS_METERS = 250;

    protected $table = 'attendance_policies';

    protected $fillable = [
        'company_id',
        'mode',
        'geo_fence_enabled',
        'remote_attendance_enabled',
        'remote_approval_required',
        'default_radius_meters',
        'gps_accuracy_requirement',
        'require_location_for_office',
        'allow_web_checkin',
        'allow_mobile_checkin',
        'biometric_attendance_mode',
        'biometric_min_punch_gap_minutes',
    ];

    protected $casts = [
        'geo_fence_enabled' => 'boolean',
        'remote_attendance_enabled' => 'boolean',
        'remote_approval_required' => 'boolean',
        'default_radius_meters' => 'integer',
        'gps_accuracy_requirement' => 'integer',
        'require_location_for_office' => 'boolean',
        'allow_web_checkin' => 'boolean',
        'allow_mobile_checkin' => 'boolean',
        'biometric_min_punch_gap_minutes' => 'integer',
    ];

    /**
     * @return array<string, mixed>
     */
    public static function systemDefaults(): array
    {
        return [
            'mode' => self::MODE_NORMAL,
            'geo_fence_enabled' => false,
            'remote_attendance_enabled' => false,
            'remote_approval_required' => false,
            'default_radius_meters' => self::DEFAULT_RADIUS_METERS,
            'gps_accuracy_requirement' => null,
            'require_location_for_office' => true,
            'allow_web_checkin' => true,
            'allow_mobile_checkin' => true,
            'biometric_attendance_mode' => self::BIOMETRIC_MODE_FIRST_IN_LAST_OUT,
            'biometric_min_punch_gap_minutes' => 1,
        ];
    }

    public static function syncDerivedFlags(string $mode): array
    {
        return [
            'geo_fence_enabled' => in_array($mode, [self::MODE_OFFICE_GEOFENCE, self::MODE_HYBRID], true),
            'remote_attendance_enabled' => in_array($mode, [self::MODE_REMOTE, self::MODE_HYBRID], true),
        ];
    }
}
