<?php

namespace Modules\Slider\Services;

use Illuminate\Http\Request;
use Modules\Slider\Interfaces\SliderRepositoryInterface;
use Modules\Slider\Models\Slider;

class SliderService
{

    public function __construct(private SliderRepositoryInterface $sliderRepository)
    {}

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

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

        if (@$request['product_id'])
        {
            $builder->where('sliders.product_id', $request['product_id']);
        }

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

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

    public function getItem($id)
    {
        $slider = $this->checkItem($id);
        $sliderDescription = $this->sliderRepository->getItem($id);
        foreach ($sliderDescription as $row) {
            $slider[$row->local]= $row;
        }
        return $slider;
    }

    public function getItemTrashed($id)
    {
        $slider = $this->sliderRepository->getItemTrashed($id);
        return $slider;
    }

    public function create(array $request): Slider
    {
        $slider = $this->sliderRepository->create($request);
        $this->sliderRepository->saveData($request,$slider->id);
        return $slider;
    }
    public function update(array $request,int $id): Slider
    {
        $slider = $this->checkItem($id);


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