<?php

namespace App\Models\Hrm\Attendance;

use App\Models\User;
use App\Models\Hrm\Shift\Shift;
use App\Models\coreApp\BaseModel;
use App\Models\Traits\BranchTrait;
use Spatie\Activitylog\LogOptions;
use App\Models\Traits\CompanyTrait;
use Modules\Break\Entities\UserBreak;
use Spatie\Activitylog\Traits\LogsActivity;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Modules\SpecialAttendance\Entities\DutyCalendar;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Attendance extends BaseModel
{
    use LogsActivity,BranchTrait,CompanyTrait;

    protected $guarded = ['id'];

    protected static $logAttributes = [
        'id',
        'company_id',
        'user_id',
        'date',
        'remote_mode_in',
        'remote_mode_out',
        'check_in',
        'check_out',
        'stay_time',
        'late_reason',
        'late_time',
        'status',
        'checkin_ip',
        'checkout_ip',
        'check_in_location',
        'check_out_location',
        'check_in_latitude',
        'check_in_longitude',
        'check_out_latitude',
        'check_out_longitude',
        'city',
        'country_code',
        'country',
        'status_id'
    ];

    protected $casts = [
        'remote_mode_in' => 'int',
        'remote_mode_out' => 'int',
    ]; 

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

    public function dutySchedule(): BelongsTo
    {
        return $this->belongsTo(DutySchedule::class, 'duty_schedule_id', 'id');
    }

    public function lateInOutReason(): HasMany
    {
        return $this->hasMany(LateInOutReason::class, 'attendance_id', 'id');
    }

    public function lateInReason(): HasOne
    {
        return $this->hasOne(LateInOutReason::class, 'attendance_id', 'id')->where('type', 'in');
    }

    public function breaks(): HasMany
    {
        return $this->hasMany(UserBreak::class, 'date', 'date');
    }

    public function earlyOutReason(): HasOne
    {
        return $this->hasOne(LateInOutReason::class, 'attendance_id', 'id')->where('type', 'out');
    }

    public function dutyCalendar(): BelongsTo
    {
        return $this->belongsTo(DutyCalendar::class, "duty_calendar_id", "id");
    }


    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults();
    }
}
