<?php

namespace App\Models;

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

class Driver extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'license_number',
        'license_expiry',
        'national_id',
        'phone',
        'emergency_contact',
        'vehicle_type',
        'experience_years',
        'rating',
        'total_trips',
        'is_verified',
        'is_active',
        'is_available',
        'current_status',
        'last_seen',
    ];

    protected $casts = [
        'is_verified' => 'boolean',
        'is_active' => 'boolean',
        'is_available' => 'boolean',
        'license_expiry' => 'date',
        'last_seen' => 'datetime',
        'rating' => 'decimal:2',
        'total_trips' => 'integer',
        'experience_years' => 'integer',
    ];

    /**
     * Get the user that owns the driver profile.
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Get the vehicle for this driver.
     */
    public function vehicle()
    {
        return $this->hasOne(Vehicle::class);
    }

    /**
     * Get the trips for this driver.
     */
    public function trips()
    {
        return $this->hasMany(Trip::class, 'driver_id');
    }

    /**
     * Get the payments for this driver.
     */
    public function payments()
    {
        return $this->hasMany(Payment::class, 'driver_id');
    }

    /**
     * Scope to get only active drivers.
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * Scope to get only available drivers.
     */
    public function scopeAvailable($query)
    {
        return $query->where('is_available', true);
    }
}
