<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Booking extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'booking_number', 'group_id', 'company_id', 'customer_id', 'driver_id', 'payout_id', 'vehicle_type_id',
        'status_id', 'payment_method_id', 'account_id', 'currency_id',
        'pickup_address', 'pickup_lat', 'pickup_lng', 'pickup_note',
        'dropoff_address', 'dropoff_lat', 'dropoff_lng', 'dropoff_note',
        'pickup_datetime', 'passengers', 'luggage', 'flight_number',
        'customer_reference', 'notes',
        'distance', 'distance_unit', 'duration_minutes', 'fare_amount',
        'job_type', 'assigned_by', 'assigned_at', 'reminder_sent_at',
    ];

    protected function casts(): array
    {
        return [
            'pickup_datetime' => 'datetime',
            'assigned_at' => 'datetime',
            'reminder_sent_at' => 'datetime',
            'pickup_lat' => 'decimal:7',
            'pickup_lng' => 'decimal:7',
            'dropoff_lat' => 'decimal:7',
            'dropoff_lng' => 'decimal:7',
            'distance' => 'decimal:2',
            'fare_amount' => 'decimal:2',
        ];
    }

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

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

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

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

    public function status(): BelongsTo
    {
        return $this->belongsTo(BookingStatus::class, 'status_id');
    }

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

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

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

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

    public function stops(): HasMany
    {
        return $this->hasMany(BookingStop::class)->orderBy('sequence');
    }

    public function statusHistory(): HasMany
    {
        return $this->hasMany(BookingStatusHistory::class)->latest();
    }

    public function messages(): HasMany
    {
        return $this->hasMany(Message::class)->oldest();
    }

    public function ratings(): HasMany
    {
        return $this->hasMany(Rating::class);
    }

    public function payout(): BelongsTo
    {
        return $this->belongsTo(DriverPayout::class, 'payout_id');
    }

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