<?php

namespace Modules\FaqCategory\Services;

use Illuminate\Http\Request;
use Modules\FaqCategory\Interfaces\FaqCategoryRepositoryInterface;
use Modules\FaqCategory\Models\FaqCategory;

class FaqCategoryService
{
    private FaqCategoryRepositoryInterface $faqCategoryRepository ;

    public function __construct(FaqCategoryRepositoryInterface $faqCategoryRepository)
    {
        $this->faqCategoryRepository = $faqCategoryRepository;
    }

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

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

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

    public function getItem($id)
    {
        $faq_category = $this->checkItem($id);
        $faq_categoryDescription = $this->faqCategoryRepository->getItem($id);
        foreach ($faq_categoryDescription as $row) {
            $faq_category[$row->local]= $row;
        }
        return $faq_category;
    }

    public function getItemTrashed($id)
    {
        $faq_category = $this->faqCategoryRepository->getItemTrashed($id);
        return $faq_category;
    }

    public function create(array $request): FaqCategory
    {
        $faq_category = $this->faqCategoryRepository->create($request);
        $this->faqCategoryRepository->saveData($request,$faq_category->id);
        return $faq_category;
    }
    public function update(array $request,int $id): FaqCategory
    {
        $faq_category = $this->checkItem($id);


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