<?php

namespace App\Models\Tenants;

use App\Models\Members\User;
use App\Traits\Auditable;
use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * 휴가 부여 이력 모델
 *
 * @property int $id
 * @property int $tenant_id
 * @property int $user_id
 * @property string $grant_type
 * @property string $grant_date
 * @property float $grant_days
 * @property string|null $reason
 * @property int|null $created_by
 * @property int|null $updated_by
 * @property int|null $deleted_by
 */
class LeaveGrant extends Model
{
    use Auditable, BelongsToTenant, SoftDeletes;

    protected $table = 'leave_grants';

    protected $casts = [
        'grant_date' => 'date',
        'grant_days' => 'decimal:1',
    ];

    protected $fillable = [
        'tenant_id',
        'user_id',
        'grant_type',
        'grant_date',
        'grant_days',
        'reason',
        'created_by',
        'updated_by',
        'deleted_by',
    ];

    // =========================================================================
    // 상수 정의
    // =========================================================================

    public const TYPE_ANNUAL = 'annual';         // 연차

    public const TYPE_MONTHLY = 'monthly';       // 월차

    public const TYPE_REWARD = 'reward';         // 포상휴가

    public const TYPE_CONDOLENCE = 'condolence'; // 경조사

    public const TYPE_OTHER = 'other';           // 기타

    public const GRANT_TYPES = [
        self::TYPE_ANNUAL,
        self::TYPE_MONTHLY,
        self::TYPE_REWARD,
        self::TYPE_CONDOLENCE,
        self::TYPE_OTHER,
    ];

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

    /**
     * 부여 대상자
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }

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

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

    /**
     * 특정 유형
     */
    public function scopeOfType($query, string $type)
    {
        return $query->where('grant_type', $type);
    }

    /**
     * 특정 사용자
     */
    public function scopeForUser($query, int $userId)
    {
        return $query->where('user_id', $userId);
    }

    /**
     * 특정 연도
     */
    public function scopeForYear($query, int $year)
    {
        return $query->whereYear('grant_date', $year);
    }

    /**
     * 날짜 범위
     */
    public function scopeBetweenDates($query, string $startDate, string $endDate)
    {
        return $query->whereBetween('grant_date', [$startDate, $endDate]);
    }

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

    /**
     * 부여 유형 라벨
     */
    public function getGrantTypeLabelAttribute(): string
    {
        return match ($this->grant_type) {
            self::TYPE_ANNUAL => '연차',
            self::TYPE_MONTHLY => '월차',
            self::TYPE_REWARD => '포상휴가',
            self::TYPE_CONDOLENCE => '경조사',
            self::TYPE_OTHER => '기타',
            default => $this->grant_type,
        };
    }
}
