<?php

namespace Modules\WarehouseCategory\Repositories;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\WarehouseCategory\Interfaces\WarehouseCategoryRepositoryInterface;
use Modules\WarehouseCategory\Models\WarehouseCategory;
use Modules\WarehouseCategory\Models\WarehouseCategoryDescription;

class WarehouseCategoryRepository implements WarehouseCategoryRepositoryInterface
{
    public function get(array $request)
    {
        $builder = WarehouseCategory::query()
            ->join('warehouse_category_descriptions AS sd', 'warehouse_categories.id', 'sd.warehouse_category_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'warehouse_categories.*']);

        return $builder;
    }

    public function getItem(int $id)
    {
        return WarehouseCategoryDescription::where('warehouse_category_id', $id)
            ->join('languages', 'languages.id', 'warehouse_category_descriptions.language_id')
            ->join('warehouse_categories', 'warehouse_categories.id', 'warehouse_category_descriptions.warehouse_category_id')
            ->select(['warehouse_category_descriptions.*', 'languages.local','warehouse_categories.*'])->get();
    }

    public function checkItem(int $id)
    {
        return WarehouseCategory::find($id);
    }

    public function getItemTrashed(int $id)
    {
        $warehouse_category = WarehouseCategory::onlyTrashed()->find($id);
        return $warehouse_category ;
    }

    public function create(array $request): WarehouseCategory
    {
        $warehouse_category = WarehouseCategory::create($request);
        return $warehouse_category;
    }

    public function update(array $request, ?WarehouseCategory $warehouse_category): WarehouseCategory
    {
        $warehouse_category->update($request);

        return $warehouse_category;
    }

    public function saveData(array $request, int $warehouse_category_id ) :void
    {
        WarehouseCategoryDescription::where('warehouse_category_id', $warehouse_category_id)->delete();
        foreach (languages() as $lang) {
            $data=[
                'warehouse_category_id'=> $warehouse_category_id,
                'language_id'=> $lang->id,
                'title'=> $request['title_'.$lang->local],
            ];
            WarehouseCategoryDescription::create($data);
        }
    }
    /**
     * Remove the specified warehouse_category from storage.
     * @param WarehouseCategory $warehouse_category
     * @return void
     */
    public function destroy(WarehouseCategory $warehouse_category) :void
    {
        $warehouse_category->delete();
    }

    /**
     * Remove several warehouse_categories by IDs.
     *
     * @param array $request
     * @return void
     */
    public function destroyAll(array $ids) :void
    {
         WarehouseCategory::whereIn('id', $ids)->delete();
    }

    /**
     * Restore the specified warehouse_category from storage.
     *
     * @param WarehouseCategory $warehouse_category
     * @return void
     */
    public function restore(WarehouseCategory $warehouse_category) :void
    {
        $warehouse_category->restore();
    }

    /**
     * Restore several warehouse_categories by IDs.
     *
     * @param array $request
     * @return void
     */
    public function restoreAll(array $ids) :void
    {
        WarehouseCategory::whereIn('id', $ids)->onlyTrashed()->restore();
    }
}
