<?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\AttendanceStatus;

/**
 * 考勤记录模型
 *
 * 管理员工每日考勤打卡记录。
 *
 * @property string $check_in_time  签到时间
 * @property string $check_out_time 签退时间
 * @property string $status         考勤状态
 * @property string $date           考勤日期
 */
class AttendanceRecord extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'employee_id',
        'date',
        'check_in_time',
        'check_out_time',
        'status',
        'late_minutes',
        'early_minutes',
        'work_hours',
    ];

    protected function casts(): array
    {
        return [
            'date' => 'date',
            'check_in_time' => 'datetime',
            'check_out_time' => 'datetime',
            'status' => AttendanceStatus::class,
            'late_minutes' => 'integer',
            'early_minutes' => 'integer',
            'work_hours' => 'decimal:1',
        ];
    }

    public function employee(): BelongsTo
    {
        return $this->belongsTo(Employee::class);
    }

    /**
     * 查询作用域：按日期范围
     */
    public function scopeDateRange($query, string $startDate, string $endDate)
    {
        return $query->whereBetween('date', [$startDate, $endDate]);
    }
}
