<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Attendance extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'clock_in',
        'clock_out',
        'status',
        'note',
        'selfie_photo',
        'clock_in_latitude',
        'clock_in_longitude',
        'clock_in_accuracy',
        'clock_in_location_name',
        'jobdesk',
        'admin_note',
    ];

    /**
     * Cast attributes to proper types.
     */
    protected $casts = [
        'clock_in' => 'datetime',
        'clock_out' => 'datetime',
        'clock_in_latitude' => 'float',
        'clock_in_longitude' => 'float',
        'clock_in_accuracy' => 'float',
    ];

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

    public function getIsVerifiedAttribute(): bool
    {
        return ! is_null($this->clock_in) && ! is_null($this->clock_out);
    }

    public function getVerificationLabelAttribute(): string
    {
        if (strtolower($this->status ?? '') === 'sakit') {
            return 'Sakit';
        }

        if (strtolower($this->status ?? '') === 'izin') {
            return 'Izin';
        }

        return $this->is_verified ? 'Benar' : 'Tidak Diterima';
    }
}
