<?php

declare(strict_types=1);

namespace Kylin\Hr\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Kylin\Hr\Enums\ContractType;

/**
 * 合同模型
 *
 * 管理员工劳动合同，包括合同类型、起止日期、签订次数等。
 *
 * @property string $contract_type 合同类型
 * @property string $start_date    开始日期
 * @property string $end_date      结束日期
 * @property int    $sign_count    签订次数
 */
class Contract extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'employee_id',
        'type',
        'start_date',
        'end_date',
        'sign_date',
        'term_months',
        'renewal_count',
        'status',
        'file_path',
    ];

    protected function casts(): array
    {
        return [
            'type' => ContractType::class,
            'start_date' => 'date',
            'end_date' => 'date',
            'sign_date' => 'date',
            'term_months' => 'integer',
            'renewal_count' => 'integer',
            'status' => 'integer',
        ];
    }

    /**
     * 关联员工
     */
    public function employee(): BelongsTo
    {
        return $this->belongsTo(Employee::class);
    }

    /**
     * 是否即将到期
     */
    public function isExpiringSoon(int $days = 30): bool
    {
        if ($this->end_date === null) {
            return false;
        }
        return $this->end_date->diffInDays(now(), false) >= -$days && $this->end_date->isFuture();
    }
}
