<?php

namespace Modules\Category\Services;

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

class CategoryService
{
    private CategoryRepositoryInterface $categoryRepository;

    public function __construct(CategoryRepositoryInterface $categoryRepository)
    {
        $this->categoryRepository = $categoryRepository;
    }

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

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

        if (@$request['category_id']) {
            if ($request['category_id'] == 'main') {
                $builder->whereNull('categories.category_id');
            } elseif ($request['category_id'] == 'sub') {
                $builder->whereNotNull('categories.category_id');
            } else {
                $builder->where('categories.category_id', $request['category_id']);
            }
        }
        if (@$request['canteen_id']) {
            $builder->where('categories.canteen_id', $request['canteen_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', 'DESC');
        }

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

    public function getItem($id)
    {
        $category = $this->checkItem($id);
        $categoryDescription = $this->categoryRepository->getItem($id);
        foreach ($categoryDescription as $row) {
            $category[$row->local] = $row;
        }
        return $category;
    }

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

    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']);
        }
    }
}
