<?php

namespace App\Models\HR;

use App\Models\Approvals\Approval;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class Leave extends Model
{
    use SoftDeletes;

    protected $table = 'leaves';

    protected $fillable = [
        'tenant_id',
        'user_id',
        'leave_type',
        'start_date',
        'end_date',
        'days',
        'reason',
        'status',
        'approved_by',
        'approved_at',
        'approval_id',
        'reject_reason',
        'created_by',
        'updated_by',
        'deleted_by',
    ];

    protected $casts = [
        'tenant_id' => 'int',
        'user_id' => 'int',
        'approval_id' => 'int',
        'approved_by' => 'int',
        'created_by' => 'int',
        'updated_by' => 'int',
        'deleted_by' => 'int',
        'start_date' => 'date',
        'end_date' => 'date',
        'days' => 'float',
        'approved_at' => 'datetime',
    ];

    // =========================================================================
    // 상수
    // =========================================================================

    public const TYPE_MAP = [
        'annual' => '연차',
        'half_am' => '오전반차',
        'half_pm' => '오후반차',
        'sick' => '병가',
        'family' => '경조사',
        'maternity' => '출산',
        'parental' => '육아',
        'business_trip' => '출장',
        'remote' => '재택근무',
        'field_work' => '외근',
        'early_leave' => '조퇴',
        'late_reason' => '지각사유서',
        'absent_reason' => '결근사유서',
    ];

    // 그룹 상수
    public const VACATION_TYPES = ['annual', 'half_am', 'half_pm', 'sick', 'family', 'maternity', 'parental'];

    public const ATTENDANCE_REQUEST_TYPES = ['business_trip', 'remote', 'field_work', 'early_leave'];

    public const REASON_REPORT_TYPES = ['late_reason', 'absent_reason'];

    // 유형 → 결재양식코드 매핑
    public const FORM_CODE_MAP = [
        'annual' => 'leave', 'half_am' => 'leave', 'half_pm' => 'leave',
        'sick' => 'leave', 'family' => 'leave', 'maternity' => 'leave', 'parental' => 'leave',
        'business_trip' => 'attendance_request', 'remote' => 'attendance_request',
        'field_work' => 'attendance_request', 'early_leave' => 'attendance_request',
        'late_reason' => 'reason_report', 'absent_reason' => 'reason_report',
    ];

    // 유형 → 근태상태 매핑 (승인 시 Attendance에 반영할 상태)
    public const ATTENDANCE_STATUS_MAP = [
        'annual' => 'vacation', 'half_am' => 'vacation', 'half_pm' => 'vacation',
        'sick' => 'vacation', 'family' => 'vacation', 'maternity' => 'vacation', 'parental' => 'vacation',
        'business_trip' => 'businessTrip', 'remote' => 'remote', 'field_work' => 'fieldWork',
        'early_leave' => null,
        'late_reason' => null,
        'absent_reason' => null,
    ];

    // 그룹별 라벨
    public const GROUP_LABELS = [
        'vacation' => '휴가',
        'attendance_request' => '근태신청',
        'reason_report' => '사유서',
    ];

    public const STATUS_MAP = [
        'pending' => '대기',
        'approved' => '승인',
        'rejected' => '반려',
        'cancelled' => '취소',
    ];

    public const STATUS_COLORS = [
        'pending' => 'amber',
        'approved' => 'emerald',
        'rejected' => 'red',
        'cancelled' => 'gray',
    ];

    public const DEDUCTIBLE_TYPES = ['annual', 'half_am', 'half_pm'];

    // =========================================================================
    // 관계
    // =========================================================================

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

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

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

    public function approval(): BelongsTo
    {
        return $this->belongsTo(Approval::class, 'approval_id');
    }

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

    public function getTypeLabelAttribute(): string
    {
        return self::TYPE_MAP[$this->leave_type] ?? $this->leave_type;
    }

    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 getIsDeductibleAttribute(): bool
    {
        return in_array($this->leave_type, self::DEDUCTIBLE_TYPES);
    }

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

    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 scopeBetweenDates($query, string $startDate, string $endDate)
    {
        return $query->where(function ($q) use ($startDate, $endDate) {
            $q->where('start_date', '<=', $endDate)
                ->where('end_date', '>=', $startDate);
        });
    }

    public function scopeForUser($query, int $userId)
    {
        return $query->where('user_id', $userId);
    }

    public function scopeForYear($query, int $year)
    {
        return $query->whereYear('start_date', $year);
    }

    public function scopeWithStatus($query, string $status)
    {
        return $query->where('status', $status);
    }
}
