<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasApiTokens, HasRoles, Notifiable;

    protected $fillable = [
        'firebase_uid',
        'full_name',
        'email',
        'phone',
        'photo_url',
        'user_type', // 'rider', 'driver', 'admin'
        'rating',
        'total_rides',
        'is_verified',
        'is_active',
        'fcm_token',
        'last_latitude',
        'last_longitude',
        'last_location_at',
        'password',
    ];

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

    protected $casts = [
        'rating' => 'float',
        'total_rides' => 'integer',
        'is_verified' => 'boolean',
        'is_active' => 'boolean',
        'last_latitude' => 'float',
        'last_longitude' => 'float',
        'last_location_at' => 'datetime',
        'created_at' => 'datetime',
    ];

    public function rides()
    {
        return $this->hasMany(Ride::class, 'user_id');
    }

    public function driver()
    {
        return $this->hasOne(Driver::class, 'user_id');
    }

    public function parcels()
    {
        return $this->hasMany(Parcel::class, 'sender_id');
    }

    public function foodOrders()
    {
        return $this->hasMany(FoodOrder::class, 'user_id');
    }

    public function emergencyContacts()
    {
        return $this->hasMany(EmergencyContact::class, 'user_id');
    }

    public function sosAlerts()
    {
        return $this->hasMany(SOSAlert::class, 'user_id');
    }

    public function payments()
    {
        return $this->hasMany(Payment::class, 'user_id');
    }

    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    public function scopeRiders($query)
    {
        return $query->where('user_type', 'rider');
    }

    public function scopeDrivers($query)
    {
        return $query->where('user_type', 'driver');
    }
}
