<?php

namespace App\Master\Models;

use App\VehicleType;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Mtc\Crm\Traits\ModelSortAndFilter;
use Stancl\Tenancy\Database\Concerns\CentralConnection;

class CapVehicle extends Model
{
    use CentralConnection;
    use ModelSortAndFilter;

    protected $table = 'cap_vehicles';

    protected $fillable = [
        'cap_code',
        'cap_id',
        'manufacturer',
        'short_model',
        'model',
        'model_introduced',
        'model_discontinued',
        'short_derivative',
        'derivative',
        'derivative_introduced_year',
        'derivative_discontinued_year',
        'available_new_vehicle',
        'type',
        'make_id',
        'model_id',
    ];

    protected $casts = [
        'cap_id' => 'integer',
        'model_introduced' => 'integer',
        'model_discontinued' => 'integer',
        'derivative_introduced_year' => 'integer',
        'derivative_discontinued_year' => 'integer',
        'available_new_vehicle' => 'boolean',
        'make_id' => 'integer',
        'model_id' => 'integer',
    ];

    public function make(): BelongsTo
    {
        return $this->belongsTo(VehicleMake::class, 'make_id');
    }

    public function autograbVehicleIds(): HasMany
    {
        return $this->hasMany(CapVehicleAutoGrabId::class, 'cap_vehicle_id');
    }

    public function vehicleModel(): BelongsTo
    {
        return $this->belongsTo(VehicleModel::class, 'model_id');
    }

    public function scopeOfType(Builder $query, string $type): Builder
    {
        return $query->where('type', $type);
    }

    public function scopeCars(Builder $query): Builder
    {
        return $query->ofType(VehicleType::CAR->value);
    }

    public function scopeLcvs(Builder $query): Builder
    {
        return $query->ofType(VehicleType::LCV->value);
    }

    public function scopeByManufacturer(Builder $query, string $manufacturer): Builder
    {
        return $query->where('manufacturer', 'like', "%{$manufacturer}%");
    }

    public function scopeByModel(Builder $query, string $model): Builder
    {
        return $query->where(function ($q) use ($model) {
            $q->where('short_model', 'like', "%{$model}%")
                ->orWhere('model', 'like', "%{$model}%");
        });
    }

    public function scopeByDerivative(Builder $query, string $derivative): Builder
    {
        return $query->where(function ($q) use ($derivative) {
            $q->where('short_derivative', 'like', "%{$derivative}%")
                ->orWhere('derivative', 'like', "%{$derivative}%");
        });
    }

    public function scopeActiveInYear(Builder $query, int $year): Builder
    {
        return $query->where(function ($q) use ($year) {
            $q->where(function ($inner) use ($year) {
                $inner->whereNull('model_introduced')
                    ->orWhere('model_introduced', '<=', $year);
            })->where(function ($inner) use ($year) {
                $inner->whereNull('model_discontinued')
                    ->orWhere('model_discontinued', '>=', $year);
            });
        });
    }
}
