<?php

namespace Modules\Category\Services;

use Illuminate\Http\Request;
use Modules\Category\Interfaces\CategoryRepositoryInterface;
use Modules\Category\Models\Category;

class CategoryService
{

    public function __construct(
        private CategoryRepositoryInterface $categoryRepository
    ){}

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

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

        if (@$request['not_id'])
        {
            $builder->where('categories.id','!=',$request['not_id']);
        }

        if (@$request['type_id'])
        {
            // $builder->where('categories.type_id',$request['type_id']);
        }
        if (@$request['show_footer'])
        {
            // $builder->where('categories.show_footer',$request['show_footer']);
        }
        if (@$request['show_home'])
        {
            // $builder->where('categories.show_home',$request['show_home']);
        }

        if (@$request['parent_id'])
        {
            if ($request['parent_id'] == 'main')
            {
                $builder->whereNull('categories.parent_id');
            }
            elseif ($request['parent_id'] == 'sub')
            {
                $builder->whereNotNull('categories.parent_id');
            }
            else
            {
                $builder->where('categories.parent_id',$request['parent_id']);
            }
        }

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

        if (@$request['date_to'])
        {
            $builder->whereDate('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','ASC');
        }

        if (@$request['paginate']) {
            $categories= $builder->paginate(50);
        }else{
            $categories=$builder->get();
        }
        return $categories;
    }
    public function checkItem($id)
    {
        $category = $this->categoryRepository->checkItem($id);
        return $category;
    }

    public function getItem($id)
    {
        $category = $this->categoryRepository->getItem($id);

        return $category;
    }

    public function getItemTrashed($id)
    {
        $category = $this->categoryRepository->getItemTrashed($id);
        return $category;
    }


    public function attributes($id)
    {
        $attributes = $this->categoryRepository->attributes($id);
        return $attributes;
    }

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