<?php

namespace Modules\ProductType\Services;

use Illuminate\Http\Request;
use Modules\ProductType\Interfaces\ProductTypeRepositoryInterface;
use Modules\ProductType\Models\ProductType;

class ProductTypeService
{
    private ProductTypeRepositoryInterface $productTypeRepository ;

    public function __construct(ProductTypeRepositoryInterface $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('product_types.created_at', '>=', $request['date_from']);
        }

        if (@$request['date_to'])
        {
            $builder->whereDate('product_types.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']) {
            $product_types= $builder->paginate(50);
        }else{
            $product_types=$builder->cursor();
        }
        return $product_types;
    }
    public function checkItem($id)
    {
        $product_type = $this->productTypeRepository->checkItem($id);
        return $product_type;
    }

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

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

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


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