<?php

namespace Modules\Banner\Services;

use Illuminate\Http\Request;
use Modules\Banner\Interfaces\BannerRepositoryInterface;
use Modules\Banner\Models\Banner;

class BannerService
{

    public function __construct(private BannerRepositoryInterface $bannerRepository)
    {}

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

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

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

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

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

    public function getItem($id)
    {
        $banner = $this->checkItem($id);
        $bannerDescription = $this->bannerRepository->getItem($id);
        foreach ($bannerDescription as $row) {
            $banner[$row->local]= $row;
        }
        return $banner;
    }

    public function getItemTrashed($id)
    {
        $banner = $this->bannerRepository->getItemTrashed($id);
        return $banner;
    }

    public function create(array $request): Banner
    {
        $banner = $this->bannerRepository->create($request);
        $this->bannerRepository->saveData($request,$banner->id);
        return $banner;
    }
    public function update(array $request,int $id): Banner
    {
        $banner = $this->checkItem($id);


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