<?php

namespace App\Models;

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

class Subcategory extends Model
{
    use HasFactory;
    protected $table = "tbl_sub_categories";
    protected $primaryKey = "id";

    protected $fillable = [
        'iderp',
        'name',
        'description',
        'idspecificsubcategories',
        'idcategory',
        'idcompany',
        'status'
    ];

    protected $hidden = [
        'created_at',
        'updated_at'
    ];

    protected $casts = [
        'idspecificsubcategories' => 'array'
    ];

    public function company(): HasOne
    {
        return $this->hasOne(Company::class, 'id', 'idcompany');
    }

    public function products(): HasMany
    {
        return $this->hasMany(Product::class, 'idsubcategory', 'id');
    }

    # Query scopes
    public function scopeActiveForID($query, $id)
    {
        return $query->where('id', $id)->where('status', 1)->company();
    }

    public function scopeActive($query)
    {
        return $query->where('status', 1);
    }

    public function scopeCompany($query)
    {
        return $query->when(
            request('idcompany'),
            fn ($query) => $query->where('idcompany', request('idcompany'))
        );
    }

    public function scopeName($query, $name, $id = null)
    {
        return $query->when($id, fn ($query) => $query->where('id', '<>', $id))->where('name', $name);
    }

    public function scopeSpecificAtributtes($query, $idspecificsubcategories)
    {
        return $query->whereRaw("JSON_SEARCH(idspecificsubcategories, 'one', '$idspecificsubcategories') IS NOT NULL");
    }

    # Filtros
    public function scopeSubCategoryFilters($query)
    {
        #Filtro de Buscador
        $query->when(
            request('search'),
            fn ($query) => $query->where('name', 'LIKE', '%' . request('search') . '%')
        );

        #Filtro de nombre
        $query->when(
            request('name'),
            fn ($query) => $query->where('name', 'LIKE', '%' . request('name') . '%')
        );

        #Filtro de descripcion
        $query->when(
            request('description'),
            fn ($query) => $query->where('description', 'LIKE', '%' . request('description') . '%')
        );

        #Filtro de iderp
        $query->when(
            request('iderp'),
            fn ($query) => $query->where('iderp', request('iderp'))
        );

        #Filtro de subcategoria especificos
        $query->when(
            request('idspecificsubcategory'),
            fn ($query) => $query->whereRaw("JSON_SEARCH(idspecificsubcategories, 'one', '" . request('idspecificsubcategory') . "') IS NOT NULL")

        );

        #Filtro de idcompany
        $query->company();

        #Filtro de estados
        $query->when(
            request('status') !== null,
            fn ($query) => $query->where('status', request('status'))
        )->when(
            request('status') === null,
            fn ($query) => $query->where('status', 1)
        );
    }
}
