<?php

namespace Modules\Gallery\Services;

use Modules\Gallery\Models\Gallery;
use Modules\Gallery\Interfaces\GalleryRepositoryInterface;

class GalleryService
{
    public function __construct(
        private GalleryRepositoryInterface $galleryRepository
    ){}

    public function get(array $request = [])
    {
        $builder = $this->galleryRepository->get($request);

        if (@$request['title'])
        {
            $builder->where('sd.title', 'LIKE', '%'.$request['title'].'%');
        }
        if (isset($request['typeStatus']))
        {
            if (is_array($request['typeStatus']))
            {
                $builder->whereIn('galleries.status', $request['typeStatus']);
            }
            else
            {
                $builder->where('galleries.status', $request['typeStatus']);
            }
        }
        if (@$request['date_from'])
        {
            $builder->whereDate('galleries.created_at', '>=', $request['date_from']);
        }

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

    public function getItem($id)
    {
        $gallery = $this->checkItem($id);
        $galleryDescription = $this->galleryRepository->getItem($id);
        foreach ($galleryDescription as $row) {
            $gallery[$row->local]= $row;
        }
        return $gallery;
    }

    public function getItemTrashed($id)
    {
        $gallery = $this->galleryRepository->getItemTrashed($id);
        return $gallery;
    }

    public function create(array $request): Gallery
    {
        $gallery = $this->galleryRepository->create($request);

        if (isset($request['choices']))
        {
            $this->galleryRepository->updateOptions($request,$gallery);
        }

        $this->galleryRepository->saveData($request,$gallery->id);
        return $gallery;
    }
    public function update(array $request,int $id): Gallery
    {
        $gallery = $this->checkItem($id);

        if (isset($request['choices']))
        {
            $this->galleryRepository->updateOptions($request,$gallery);
        }

        $gallery = $this->galleryRepository->update($request,$gallery);
        $this->galleryRepository->saveData($request,$gallery->id);
        return $gallery;
    }

    public function choices(int $gallery_id)
    {
        $gallery = $this->getItem($gallery_id);
        return $this->galleryRepository->choices($gallery);
    }

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