<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class VehicleType extends Model
{
    use HasFactory;

    protected $table = 'vehicle_types';

    protected $fillable = [
        'tenant_id',
        'name',
        'category',
        'description',
        'icon_url',
        'capacity_passengers',
        'capacity_luggage',
        'features',
        'description_features',
        'display_label',
        'is_active',
        'sort_order',
    ];

    protected $casts = [
        'features' => 'json',
        'is_active' => 'boolean',
        'capacity_passengers' => 'integer',
        'capacity_luggage' => 'integer',
        'sort_order' => 'integer',
    ];

    /**
     * Relationship with Tenant
     */
    public function tenant(): BelongsTo
    {
        return $this->belongsTo(Tenant::class);
    }

    /**
     * Relationship with pricing
     */
    public function pricing(): HasOne
    {
        return $this->hasOne(VehicleTypePricing::class, 'vehicle_type_id')
            ->where('is_active', true)
            ->orderBy('created_at', 'desc');
    }

    /**
     * All vehicle instances of this type
     */
    public function vehicles(): HasMany
    {
        return $this->hasMany(Vehicle::class, 'vehicle_type_id');
    }

    /**
     * Get all active pricing records
     */
    public function allPricing()
    {
        return $this->hasMany(VehicleTypePricing::class, 'vehicle_type_id')
            ->where('is_active', true)
            ->orderBy('created_at', 'desc');
    }

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

    /**
     * Scope for tenant
     */
    public function scopeForTenant($query, $tenantId)
    {
        return $query->where('tenant_id', $tenantId);
    }

    /**
     * Get formatted vehicle type as array for API response
     */
    public function toApiArray()
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'category' => $this->category,
            'description' => $this->description,
            'icon_url' => $this->icon_url,
            'capacity_passengers' => $this->capacity_passengers,
            'capacity_luggage' => $this->capacity_luggage,
            'features' => $this->features ?? [],
            'description_features' => $this->description_features,
            'display_label' => $this->display_label,
            'is_active' => $this->is_active,
            'pricing' => $this->pricing ? [
                'id'               => $this->pricing->id,
                'base_fare'        => (float) $this->pricing->base_fare,
                'per_km_rate'      => (float) $this->pricing->per_km_rate,
                'per_minute_rate'  => (float) $this->pricing->per_minute_rate,
                'per_hour_rate'    => (float) $this->pricing->per_hour_rate,
                'ot_wait_rate'     => (float) $this->pricing->ot_wait_rate,
                'extra_stop_rate'  => (float) $this->pricing->extra_stop_rate,
                'per_mile_rate'    => (float) $this->pricing->per_mile_rate,
                'std_gear_rate'    => (float) $this->pricing->std_gear_rate,
                'minimum_fare'     => (float) $this->pricing->minimum_fare,
                'currency'         => $this->pricing->currency,
                'pricing_type'     => $this->pricing->pricing_type,
            ] : null,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}
