<?php

namespace App\Models;

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

class LateArrivalRequest extends Model
{
    use HasFactory, HasUuids;

    protected $fillable = [
        'user_id',
        'company_id',
        'attendance_id',
        'request_date',
        'planned_arrival_time',
        'reason_type',
        'reason_detail',
        'status',
        'approved_by',
        'approved_at',
        'rejection_reason',
        'actual_checkin_at',
        'simulated_request_at',
        'is_auto_approved',
    ];

    protected $casts = [
        'user_id' => 'integer',
        'company_id' => 'integer',
        'approved_by' => 'integer',
        'request_date' => 'date',
        'planned_arrival_time' => 'datetime:H:i',
        'approved_at' => 'datetime',
        'actual_checkin_at' => 'datetime',
        'simulated_request_at' => 'datetime',
        'is_auto_approved' => 'boolean',
    ];

    // Reason types
    public const REASON_TRAFFIC = 'traffic';
    public const REASON_SICK = 'sick';
    public const REASON_FAMILY = 'family';
    public const REASON_APPOINTMENT = 'appointment';
    public const REASON_EMERGENCY = 'emergency';
    public const REASON_OFFICIAL = 'official';
    public const REASON_OTHER = 'other';

    public const REASON_TYPES = [
        self::REASON_TRAFFIC => 'Macet/Transportasi',
        self::REASON_SICK => 'Sakit/Tidak Enak Badan',
        self::REASON_FAMILY => 'Urusan Keluarga',
        self::REASON_APPOINTMENT => 'Janji/Appointment',
        self::REASON_EMERGENCY => 'Keperluan Mendesak',
        self::REASON_OFFICIAL => 'Tugas Dinas/Kantor',
        self::REASON_OTHER => 'Lainnya',
    ];

    // Status constants
    public const STATUS_PENDING = 'pending';
    public const STATUS_APPROVED = 'approved';
    public const STATUS_REJECTED = 'rejected';
    public const STATUS_USED = 'used';
    public const STATUS_EXPIRED = 'expired';

    public const STATUSES = [
        self::STATUS_PENDING => 'Menunggu',
        self::STATUS_APPROVED => 'Disetujui',
        self::STATUS_REJECTED => 'Ditolak',
        self::STATUS_USED => 'Sudah Digunakan',
        self::STATUS_EXPIRED => 'Kadaluarsa',
    ];

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

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

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

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

    // Scopes
    public function scopePending($query)
    {
        return $query->where('status', self::STATUS_PENDING);
    }

    public function scopeApproved($query)
    {
        return $query->where('status', self::STATUS_APPROVED);
    }

    public function scopeForUser($query, $userId)
    {
        return $query->where('user_id', $userId);
    }

    public function scopeForCompany($query, $companyId)
    {
        return $query->where('company_id', $companyId);
    }

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

    public function scopeActive($query)
    {
        return $query->whereIn('status', [self::STATUS_PENDING, self::STATUS_APPROVED]);
    }

    // Helpers
    public function isPending(): bool
    {
        return $this->status === self::STATUS_PENDING;
    }

    public function isApproved(): bool
    {
        return $this->status === self::STATUS_APPROVED;
    }

    public function isUsed(): bool
    {
        return $this->status === self::STATUS_USED;
    }

    public function canBeUsed(): bool
    {
        return $this->isApproved() &&
               $this->request_date->isToday() &&
               is_null($this->actual_checkin_at);
    }

    public function getStatusLabelAttribute(): string
    {
        return self::STATUSES[$this->status] ?? $this->status;
    }

    public function getReasonTypeLabelAttribute(): string
    {
        return self::REASON_TYPES[$this->reason_type] ?? $this->reason_type;
    }

    public function getFullReasonAttribute(): string
    {
        $label = $this->reason_type_label;
        if ($this->reason_type === self::REASON_OTHER && $this->reason_detail) {
            return $label . ': ' . $this->reason_detail;
        }
        return $label;
    }
}
