<?php

namespace App\Models\Tenants;

use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;

/**
 * 출퇴근 설정 모델
 *
 * @property int $id
 * @property int $tenant_id
 * @property bool $use_gps
 * @property bool $use_auto
 * @property int $allowed_radius
 * @property string|null $hq_address
 * @property float|null $hq_latitude
 * @property float|null $hq_longitude
 */
class AttendanceSetting extends Model
{
    use BelongsToTenant;

    protected $table = 'attendance_settings';

    protected $fillable = [
        'tenant_id',
        'use_gps',
        'use_auto',
        'allowed_radius',
        'hq_address',
        'hq_latitude',
        'hq_longitude',
    ];

    protected $casts = [
        'use_gps' => 'boolean',
        'use_auto' => 'boolean',
        'allowed_radius' => 'integer',
        'hq_latitude' => 'decimal:8',
        'hq_longitude' => 'decimal:8',
    ];

    protected $attributes = [
        'use_gps' => false,
        'use_auto' => false,
        'allowed_radius' => 100,
    ];

    // =========================================================================
    // 헬퍼 메서드
    // =========================================================================

    /**
     * GPS 설정 완료 여부
     */
    public function isGpsConfigured(): bool
    {
        return $this->use_gps
            && $this->hq_latitude !== null
            && $this->hq_longitude !== null;
    }

    /**
     * 좌표가 허용 범위 내인지 확인
     */
    public function isWithinRadius(float $latitude, float $longitude): bool
    {
        if (! $this->isGpsConfigured()) {
            return true; // GPS 미설정 시 항상 허용
        }

        $distance = $this->calculateDistance(
            $this->hq_latitude,
            $this->hq_longitude,
            $latitude,
            $longitude
        );

        return $distance <= $this->allowed_radius;
    }

    /**
     * 두 좌표 간 거리 계산 (미터)
     * Haversine 공식 사용
     */
    private function calculateDistance(float $lat1, float $lon1, float $lat2, float $lon2): float
    {
        $earthRadius = 6371000; // 지구 반경 (미터)

        $lat1Rad = deg2rad($lat1);
        $lat2Rad = deg2rad($lat2);
        $deltaLat = deg2rad($lat2 - $lat1);
        $deltaLon = deg2rad($lon2 - $lon1);

        $a = sin($deltaLat / 2) * sin($deltaLat / 2) +
            cos($lat1Rad) * cos($lat2Rad) *
            sin($deltaLon / 2) * sin($deltaLon / 2);

        $c = 2 * atan2(sqrt($a), sqrt(1 - $a));

        return $earthRadius * $c;
    }
}
