<?php

namespace App\Models;

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 App\Models\Settings\Destination;
use Illuminate\Database\Eloquent\Model;
use App\Models\Settings\TransmissionType;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Reservation extends Model
{
    use HasFactory;

    /**
    * The attributes that are mass assignable.
    *
    * @var array<int, string>
    */
   protected $fillable = [
       'created_by',
       'updated_by',
       'car_id',
       'start_date',
       'end_date',
       'first_name',
       'last_name',
       'phone',
       'type_part',
       'part_number',
       'number_part',
       'number_permit',
       'adress',
       'email',
       'start_location',
       'end_location',
       'price',
       'is_paid',
       'conductor',
       'active',
       'deleted_by',
       'account',
       'delivery',
       'status',
   ];

   /**
    * @return BelongsTo
    */
   public function car() : BelongsTo
   {
       return $this->belongsTo(Car::class);
   }
//    public function start_location() : BelongsTo
//    {
//        return $this->belongsTo(Destination::class , 'start_location');
//    }
//    public function end_location() : BelongsTo
//    {
//        return $this->belongsTo(Destination::class , 'end_location');
//    }


   /**
    * @return BelongsTo
    */
//    public function cartype() : BelongsTo
//    {
//        return $this->belongsTo(CarType::class);
//    }
//    /**
//     * @return BelongsTo
//     */
//    public function color() : BelongsTo
//    {
//        return $this->belongsTo(Color::class);
//    }
//    /**
//     * @return BelongsTo
//     */
//    public function fueltype() : BelongsTo
//    {
//        return $this->belongsTo(FuelType::class);
//    }
//    /**
//     * @return BelongsTo
//     */
//    public function transmissiontype() : BelongsTo
//    {
//        return $this->belongsTo(TransmissionType::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 = Reservation::with( 'car.modele' ,'car.brand')
        ->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
        );
    }
}
