<?php

namespace App\Models\HR;

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',
        'reject_reason',
        'created_by',
        'updated_by',
        'deleted_by',
    ];

    protected $casts = [
        'tenant_id' => 'int',
        'user_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' => '육아',
    ];

    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');
    }

    // =========================================================================
    // 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);
    }
}
