<?php

namespace Modules\WarehouseCategory\Services;

use Illuminate\Http\Request;
use Modules\WarehouseCategory\Interfaces\WarehouseCategoryRepositoryInterface;
use Modules\WarehouseCategory\Models\WarehouseCategory;

class WarehouseCategoryService
{
    private WarehouseCategoryRepositoryInterface $productTypeRepository ;

    public function __construct(WarehouseCategoryRepositoryInterface $productTypeRepository)
    {
        $this->productTypeRepository = $productTypeRepository;
    }

    public function get(array $request)
    {
        $builder = $this->productTypeRepository->get($request);

        if (@$request['title'])
        {
            $builder->where('sd.title', 'LIKE', '%'.$request['title'].'%');
        }
        if (@$request['date_from'])
        {
            $builder->whereDate('warehouse_categories.created_at', '>=', $request['date_from']);
        }

        if (@$request['date_to'])
        {
            $builder->whereDate('warehouse_categories.created_at', '<=', $request['date_to']);
        }

        if (isset($request['status']))
        {
            if (@$request['status'] == 0)
            {
                $builder->onlyTrashed();
            }
        }

        if (@$request['orderBy'])
        {
            $builder->orderBy($request['orderBy']['key'],$request['orderBy']['value']);
        }
        else
        {
            $builder->orderBy('id','DESC');
        }

        if (@$request['paginate']) {
            $warehouse_categories= $builder->paginate(50);
        }else{
            $warehouse_categories=$builder->cursor();
        }
        return $warehouse_categories;
    }
    public function checkItem($id)
    {
        $warehouse_category = $this->productTypeRepository->checkItem($id);
        return $warehouse_category;
    }

    public function getItem($id)
    {
        $warehouse_category = $this->checkItem($id);
        $warehouse_categoryDescription = $this->productTypeRepository->getItem($id);
        foreach ($warehouse_categoryDescription as $row) {
            $warehouse_category[$row->local]= $row;
        }
        return $warehouse_category;
    }

    public function getItemTrashed($id)
    {
        $warehouse_category = $this->productTypeRepository->getItemTrashed($id);
        return $warehouse_category;
    }

    public function create(array $request): WarehouseCategory
    {
        $warehouse_category = $this->productTypeRepository->create($request);
        $this->productTypeRepository->saveData($request,$warehouse_category->id);
        return $warehouse_category;
    }
    public function update(array $request,int $id): WarehouseCategory
    {
        $warehouse_category = $this->checkItem($id);


        $warehouse_category = $this->productTypeRepository->update($request,$warehouse_category);
        $this->productTypeRepository->saveData($request,$warehouse_category->id);
        return $warehouse_category;
    }
    public function destroy(int $id): void
    {
        $warehouse_category = $this->checkItem($id);
        if ($warehouse_category)
        {
            $this->productTypeRepository->destroy($warehouse_category);
        }
        else
        {
            $warehouse_category = $this->getItemTrashed($id);
            $this->productTypeRepository->restore($warehouse_category);
        }
    }
    public function destroyAll(array $request): void
    {
        if ($request['status'] == 1)
        {
            $this->productTypeRepository->destroyAll($request['ids']);
        }
        else
        {
            $this->productTypeRepository->restoreAll($request['ids']);
        }
    }
}
