<?php

namespace App\Models;

use App\Models\Image;
use App\Models\Settings\Brand;
use App\Models\Settings\Color;
use App\Models\Settings\Modele;
use App\Models\Settings\CarType;
use App\Models\Settings\FuelType;
use Illuminate\Database\Eloquent\Model;
use App\Models\Settings\TransmissionType;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Car extends Model
{
    use HasFactory, SoftDeletes;

    public function modele(): BelongsTo
    {
        return $this->belongsTo(Modele::class);
    }
    public function brand(): BelongsTo
    {
        return $this->belongsTo(Brand::class);
    }
    public function cartype(): BelongsTo
    {
        return $this->belongsTo(CarType::class);
    }
    public function color(): BelongsTo
    {
        return $this->belongsTo(Color::class);
    }
    public function fueltype(): BelongsTo
    {
        return $this->belongsTo(FuelType::class);
    }
    public function transmissiontype(): BelongsTo
    {
        return $this->belongsTo(TransmissionType::class);
    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'price',
        'number_of_places',
        'immatriculation',
        'description',
        'status',
        'created_by',
        'updated_by',
        'brand_id',
        'modele_id',
        'cartype_id',
        'fueltype_id',
        'transmissiontype_id',
        'color_id',
        'active',
        'deleted_by',
        'active',
    ];


    /**
     * @return HasMany
     */
    public function images() : HasMany
    {
        return $this->hasMany(Image::class);
    }
    


    /**
     * Get data
     * @param int $status
     * @param string $searchValue
     * @param array $columnsFilter
     * @return array
     */
    public function getData(mixed $status, string $searchValue, array $columnsFilter) : array
    {
        // Set query
        $query = Car::with('modele' ,'brand' ,'cartype' ,'color' , 'fueltype' ,'transmissiontype')
        ->when($searchValue, function ($query) use ($searchValue, $columnsFilter) {
            $query->where(function ($subQuery) use ($columnsFilter, $searchValue) {
                $subQuery->where($columnsFilter[0], 'LIKE', '%' . $searchValue . '%');
                for ($i = 1; $i < count($columnsFilter); $i++) {
                    $subQuery->orWhere($columnsFilter[$i], 'LIKE', '%' . $searchValue . '%');
                }
            });
        })
            ->when($status, function ($query) use ($status) {
                $query->where('status', $status);
            })
            ->where('deleted_at', null)
            ->where('active', 1);

        $data = $query->offset($_REQUEST['start'])
            ->limit($_REQUEST['length'])
            ->orderBy('id', 'desc')
            ->get()
            ->toArray();


            // dd($data);

        // Construct the response array for DataTables
        // Return data
        return array(
            "recordsTotal" => $query->count(),
            "recordsFiltered" => $query->count(),
            "data" => $data
        );
    }
}
