<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class VehicleBooking extends Model
{
    protected $fillable = [
        'vehicle_id', 
        'user_id', 
        'purpose', 
        'destination', 
        'start_time', 
        'end_time', 
        'status', 
        'notes'
    ];

    protected $casts = [
        'start_time' => 'datetime',
        'end_time' => 'datetime',
    ];

    // Relasi
    public function user() { return $this->belongsTo(User::class); }
    public function vehicle() { return $this->belongsTo(Vehicle::class); }

    /**
     * Helper untuk izin aksi (Persetujuan)
     */
    public function canBeActedOnBy($user)
    {
        if (!$user) return false;

        if ($this->status === 'pending') {
            return in_array($user->role, ['staff_bidang', 'sekretariat'], true);
        }

        if (in_array($this->status, ['staff_approved', 'sekretariat_approved'], true)) {
            return $user->role === 'kepala_bidang';
        }

        return false;
    }
}
