<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable, SoftDeletes;

    protected $fillable = [
        'name',
        'email',
        'phone',
        'national_code',
        'role',
        'password',
        'is_active',
        'address',
        'postal_code',
        'vehicle_type',
        'vehicle_capacity',
        'vehicle_plate',
        'smart_card_number',
        'license_number',
        'driver_status',
        'rating',
        'last_activity',
    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
        'password' => 'hashed',
        'is_active' => 'boolean',
        'rating' => 'decimal:2',
        'vehicle_capacity' => 'decimal:2',
        'last_activity' => 'datetime',
    ];

    public function requestedLoads()
    {
        return $this->hasMany(Load::class, 'requester_id');
    }

    public function driverLoads()
    {
        return $this->hasMany(Load::class, 'driver_id');
    }

    public function isAdmin()
    {
        return $this->role === 'admin';
    }

    public function isDriver()
    {
        return $this->role === 'driver';
    }

    public function isRequester()
    {
        return $this->role === 'requester';
    }

    public function location()
    {
        return $this->hasOne(DriverLocation::class, 'driver_id');
    }

    public function locationHistory()
    {
        return $this->hasMany(DriverLocation::class, 'driver_id');
    }
}
