<?php

namespace App\Models;

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

class BusinessTripAttendance extends Model
{
    use HasFactory;

    protected $fillable = [
        'business_trip_id',
        'user_id',
        'date',
        'type',
        'check_in_time',
        'check_in_latitude',
        'check_in_longitude',
        'check_in_address',
        'check_in_city',
        'check_in_province',
        'check_in_is_editable',
        'check_in_notes',
        'check_out_time',
        'check_out_latitude',
        'check_out_longitude',
        'check_out_address',
        'check_out_city',
        'check_out_province',
        'check_out_is_editable',
        'check_out_notes',
        'reference_office_id',
        'distance_from_reference',
        'claim_status',
        'claimed_at',
        'approved_by',
        'approved_at',
        'rejection_reason',
    ];

    protected function casts(): array
    {
        return [
            'date' => 'date',
            'check_in_time' => 'datetime',
            'check_out_time' => 'datetime',
            'check_in_latitude' => 'decimal:8',
            'check_in_longitude' => 'decimal:8',
            'check_out_latitude' => 'decimal:8',
            'check_out_longitude' => 'decimal:8',
            'check_in_is_editable' => 'boolean',
            'check_out_is_editable' => 'boolean',
            'distance_from_reference' => 'integer',
            'claimed_at' => 'datetime',
            'approved_at' => 'datetime',
        ];
    }

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

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

    public function referenceOffice(): BelongsTo
    {
        return $this->belongsTo(Office::class, 'reference_office_id');
    }

    public function approver(): BelongsTo
    {
        return $this->belongsTo(User::class, 'approved_by');
    }

    public function scopeForDate($query, $date)
    {
        return $query->whereDate('date', $date);
    }

    public function scopeDeparture($query)
    {
        return $query->where('type', 'departure');
    }

    public function scopeArrival($query)
    {
        return $query->where('type', 'arrival');
    }

    public function scopeDaily($query)
    {
        return $query->where('type', 'daily');
    }

    public function scopeReturnDeparture($query)
    {
        return $query->where('type', 'return_departure');
    }

    public function scopeReturnArrival($query)
    {
        return $query->where('type', 'return_arrival');
    }

    public function scopeClaimed($query)
    {
        return $query->where('claim_status', 'claimed');
    }

    public function scopePendingApproval($query)
    {
        return $query->where('claim_status', 'claimed');
    }

    public function scopeApproved($query)
    {
        return $query->whereIn('claim_status', ['approved', 'auto_approved']);
    }

    public function hasCheckedIn(): bool
    {
        return $this->check_in_time !== null;
    }

    public function hasCheckedOut(): bool
    {
        return $this->check_out_time !== null;
    }

    public function isClaimed(): bool
    {
        return in_array($this->claim_status, ['claimed', 'approved', 'auto_approved']);
    }

    public function isApproved(): bool
    {
        return in_array($this->claim_status, ['approved', 'auto_approved']);
    }

    public function canBeClaimed(): bool
    {
        return $this->claim_status === 'draft' && $this->hasCheckedIn();
    }
}
