<?php

namespace App\Models\HR;

use App\Models\User;
use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class Attendance extends Model
{
    use ModelTrait, SoftDeletes;

    protected $table = 'attendances';

    protected $fillable = [
        'tenant_id',
        'user_id',
        'base_date',
        'status',
        'json_details',
        'remarks',
        'created_by',
        'updated_by',
        'deleted_by',
    ];

    protected $casts = [
        'json_details' => 'array',
        'base_date' => 'date',
        'tenant_id' => 'int',
        'user_id' => 'int',
    ];

    protected $attributes = [
        'status' => 'onTime',
    ];

    public const STATUS_MAP = [
        'onTime' => '정시출근',
        'late' => '지각',
        'absent' => '결근',
        'vacation' => '휴가',
        'businessTrip' => '출장',
        'fieldWork' => '외근',
        'overtime' => '야근',
        'remote' => '재택',
    ];

    public const STATUS_COLORS = [
        'onTime' => 'emerald',
        'late' => 'amber',
        'absent' => 'red',
        'vacation' => 'blue',
        'businessTrip' => 'purple',
        'fieldWork' => 'indigo',
        'overtime' => 'orange',
        'remote' => 'teal',
    ];

    // =========================================================================
    // 관계 정의
    // =========================================================================

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    // =========================================================================
    // Accessor
    // =========================================================================

    public function getCheckInAttribute(): ?string
    {
        $checkIns = $this->json_details['check_ins'] ?? [];

        if (! empty($checkIns)) {
            $times = array_filter(array_map(fn ($entry) => $entry['time'] ?? null, $checkIns));
            if (! empty($times)) {
                sort($times);

                return $times[0];
            }
        }

        return $this->json_details['check_in'] ?? null;
    }

    public function getCheckOutAttribute(): ?string
    {
        $checkOuts = $this->json_details['check_outs'] ?? [];

        if (! empty($checkOuts)) {
            $times = array_filter(array_map(fn ($entry) => $entry['time'] ?? null, $checkOuts));
            if (! empty($times)) {
                rsort($times);

                return $times[0];
            }
        }

        return $this->json_details['check_out'] ?? null;
    }

    public function getWorkMinutesAttribute(): ?int
    {
        return isset($this->json_details['work_minutes'])
            ? (int) $this->json_details['work_minutes']
            : null;
    }

    public function getStatusLabelAttribute(): string
    {
        return self::STATUS_MAP[$this->status] ?? $this->status;
    }

    public function getStatusColorAttribute(): string
    {
        return self::STATUS_COLORS[$this->status] ?? 'gray';
    }

    // =========================================================================
    // 스코프
    // =========================================================================

    public function scopeForTenant($query, ?int $tenantId = null)
    {
        $tenantId = $tenantId ?? session('selected_tenant_id');
        if ($tenantId) {
            return $query->where($this->table.'.tenant_id', $tenantId);
        }

        return $query;
    }

    public function scopeOnDate($query, string $date)
    {
        return $query->whereDate('base_date', $date);
    }

    public function scopeBetweenDates($query, string $startDate, string $endDate)
    {
        return $query->whereBetween('base_date', [$startDate, $endDate]);
    }
}
