<?php

namespace App\Repositories\Catalog;

use App\Models\Brand;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class BrandRepository
{
    public function getList($params)
    {
        return Cache::remember('catalog_brand_'.json_encode($params), 60, function () use ($params) {

            $category_id = @$params['category_id'];
            if ($category_id) {
                $ids = DB::select('select distinct brand from store_category_map a
            left join item_dimension b
            left join items on b.no = items.number
            on a.category1 = b.category1
            and (a.category2 = b.category2 or a.category2 is null)
            and (a.category3 = b.category3 or a.category3 is null)
            and (a.category4 = b.category4 or a.category4 is null)
            where store_category_id = ?
            and items.is_publish = true ', [$category_id]);
                $ids = collect($ids)->pluck('brand');

                return Brand::whereIn('name', $ids)->orderBy('priority', 'desc')
                    ->where('priority', '>', 0)
                    ->orderBy('name', 'asc')->get();
            }

            return Brand::orderBy('priority', 'desc')->orderBy('name', 'asc')
                ->where('priority', '>', 0)
                ->get();

        });

    }
}
