<?php

namespace App\Models\Tenants;

use App\Traits\Auditable;
use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * 휴가 정책 모델
 *
 * @property int $id
 * @property int $tenant_id
 * @property string $standard_type
 * @property int $fiscal_start_month
 * @property int $fiscal_start_day
 * @property int $default_annual_leave
 * @property int $additional_leave_per_year
 * @property int $max_annual_leave
 * @property bool $carry_over_enabled
 * @property int $carry_over_max_days
 * @property int $carry_over_expiry_months
 * @property int|null $created_by
 * @property int|null $updated_by
 */
class LeavePolicy extends Model
{
    use Auditable, BelongsToTenant;

    protected $table = 'leave_policies';

    protected $fillable = [
        'tenant_id',
        'standard_type',
        'fiscal_start_month',
        'fiscal_start_day',
        'default_annual_leave',
        'additional_leave_per_year',
        'max_annual_leave',
        'carry_over_enabled',
        'carry_over_max_days',
        'carry_over_expiry_months',
        'created_by',
        'updated_by',
    ];

    protected $casts = [
        'fiscal_start_month' => 'integer',
        'fiscal_start_day' => 'integer',
        'default_annual_leave' => 'integer',
        'additional_leave_per_year' => 'integer',
        'max_annual_leave' => 'integer',
        'carry_over_enabled' => 'boolean',
        'carry_over_max_days' => 'integer',
        'carry_over_expiry_months' => 'integer',
    ];

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

    public const TYPE_FISCAL = 'fiscal';

    public const TYPE_HIRE = 'hire';

    public const STANDARD_TYPES = [
        self::TYPE_FISCAL,
        self::TYPE_HIRE,
    ];

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

    /**
     * 테넌트
     */
    public function tenant(): BelongsTo
    {
        return $this->belongsTo(Tenant::class);
    }

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

    /**
     * 기준 유형 라벨
     */
    public function getStandardTypeLabelAttribute(): string
    {
        return match ($this->standard_type) {
            self::TYPE_FISCAL => '회계연도',
            self::TYPE_HIRE => '입사일',
            default => $this->standard_type,
        };
    }

    /**
     * 기준일 문자열
     */
    public function getFiscalStartDateStringAttribute(): string
    {
        return "{$this->fiscal_start_month}월 {$this->fiscal_start_day}일";
    }

    /**
     * 기본 설정 생성
     */
    public static function createDefault(int $tenantId, ?int $userId = null): self
    {
        return self::create([
            'tenant_id' => $tenantId,
            'standard_type' => self::TYPE_FISCAL,
            'fiscal_start_month' => 1,
            'fiscal_start_day' => 1,
            'default_annual_leave' => 15,
            'additional_leave_per_year' => 1,
            'max_annual_leave' => 25,
            'carry_over_enabled' => true,
            'carry_over_max_days' => 10,
            'carry_over_expiry_months' => 3,
            'created_by' => $userId,
            'updated_by' => $userId,
        ]);
    }
}
