<?php

namespace App\Models\Settings;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Color extends Model
{
    use HasFactory, SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'active',
        'deleted_by',
        'created_by',
        'updated_by',
    ];

    /** Get data
     * @param mixed $status
     * @param string $searchValue
     * @param array $columnsFilter
     * 
     * @return array
     */
    public function getModelData(mixed $status, string $searchValue, array $columnsFilter) : array
    {
        $query = Color::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();

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

    }
}
