<?php

namespace Modules\Category\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Category\Interfaces\CategoryRepositoryInterface;
use Modules\Category\Models\Category;
use Modules\Category\Models\CategoryDescription;

class CategoryRepository implements CategoryRepositoryInterface
{
    public function get(array $request)
    {
        $builder = Category::orderBy('id', 'DESC')
            ->join('category_descriptions AS sd', 'categories.id', 'sd.category_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'categories.*']);
        return $builder;
    }

    public function getItem(int $id)
    {
        return CategoryDescription::where('category_descriptions.category_id', $id)
            ->join('languages', 'languages.id', 'category_descriptions.language_id')
            ->join('categories', 'categories.id', 'category_descriptions.category_id')
            ->select(['category_descriptions.*', 'languages.local', 'categories.*'])->get();
    }

    public function checkItem(int $id)
    {
        return Category::find($id);
    }

    public function getItemTrashed(int $id)
    {
        $category = Category::onlyTrashed()->find($id);
        return $category;
    }

    public function create(array $request): Category
    {
        $category = Category::create($request);
        return $category;
    }

    public function update(array $request, ?Category $category): Category
    {
        $category->update($request);

        return $category;
    }

    public function saveData(array $request, int $category_id): void
    {
        CategoryDescription::where('category_id', $category_id)->delete();
        foreach (languages() as $lang) {
            $data = [
                'category_id' => $category_id,
                'language_id' => $lang->id,
                'title' => $request['title_' . $lang->local],
                // 'description'=> $request['description_'.$lang->local],
            ];
            CategoryDescription::create($data);
        }
    }
    /**
     * Remove the specified category from storage.
     * @param Category $category
     * @return void
     */
    public function destroy(Category $category): void
    {
        $category->delete();
    }

    /**
     * Remove several categories by IDs.
     *
     * @param array $request
     * @return void
     */
    public function destroyAll(array $ids): void
    {
        Category::whereIn('id', $ids)->delete();
    }

    /**
     * Restore the specified category from storage.
     *
     * @param Category $category
     * @return void
     */
    public function restore(Category $category): void
    {
        $category->restore();
    }

    /**
     * Restore several categories by IDs.
     *
     * @param array $request
     * @return void
     */
    public function restoreAll(array $ids): void
    {
        Category::whereIn('id', $ids)->onlyTrashed()->restore();
    }
}
