<?php

namespace App\Modules\HR\Attendance\Models;

use App\Modules\HR\Core\Models\Employee;
use App\Modules\HR\Core\Models\Shift;
use Illuminate\Database\Eloquent\Model;

class AttendanceDetail extends Model
{
    protected $table = 'attendance_details';

    protected $fillable = [
        'attendance_id',
        'employee_id',
        'shift_id',
        'attendance_status',
        'leave_code',
        'leave_affects_pay',
        'in_time',
        'out_time',
        'late_minutes',
        'early_minutes',
        'short_leave_minutes',
        'ot_minutes',
        'dot_minutes',
        'remove_ot',
        'lunch_taken',
        'dinner_taken',
        'is_manual',
        'remarks',
    ];

    protected $casts = [
        'leave_affects_pay' => 'boolean',
        'remove_ot' => 'boolean',
        'lunch_taken' => 'boolean',
        'dinner_taken' => 'boolean',
        'is_manual' => 'boolean',
        'in_time' => 'datetime',
        'out_time' => 'datetime',
    ];

    public function attendance()
    {
        return $this->belongsTo(Attendance::class);
    }

    public function punches()
    {
        return $this->hasMany(AttendancePunchDetail::class);
    }

    public function shift()
    {
        return $this->belongsTo(Shift::class);
    }

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

    public function getOtHrsAttribute()
    {
        return round($this->ot_minutes / 60, 2);
    }

    public function getDotHrsAttribute()
    {
        return round($this->dot_minutes / 60, 2);
    }
}
