<?php

namespace Modules\ShippingWay\Services;

use Illuminate\Http\Request;
use Modules\ShippingWay\Interfaces\ShippingWayRepositoryInterface;
use Modules\ShippingWay\Models\ShippingWay;

class ShippingWayService
{
    private ShippingWayRepositoryInterface $shippingWayRepository ;

    public function __construct(ShippingWayRepositoryInterface $shippingWayRepository)
    {
        $this->shippingWayRepository = $shippingWayRepository;
    }

    public function get(array $request)
    {
        $builder = $this->shippingWayRepository->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->shippingWayRepository->checkItem($id);
        return $product_type;
    }

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

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

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


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