<?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;
use Modules\Category\Models\Attribute;
use Modules\Category\Models\AttributeDescription;

class CategoryRepository implements CategoryRepositoryInterface
{
    public function get(array $request)
    {
        $builder = Category::with(['main','sub','type'])
            ->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)
    {
        $category = Category::with(['main','sub','type'])->find($id);

        $query = CategoryDescription::where('category_descriptions.category_id', $category->id)
            ->join('categories', 'category_descriptions.category_id','categories.id')
            ->join('languages', 'languages.id', 'category_descriptions.language_id')
            ->select(['categories.*','category_descriptions.title', 'languages.local']);

        $categoryDescription= $query->get();

        // dd($categoryDescription->toArray());
        foreach ($categoryDescription as $key=>$row) {

            $category[$row->local] = $row;
        }
        return $category;
    }

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

    public function attributes(int $id)
    {
        return Attribute::where('category_id',$id)->cursor();
    }

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

    public function create(array $request): Category
    {
        // Filter out language-specific fields that don't belong to the main Category model
        $categoryData = array_filter($request, function($key) {
            return !str_starts_with($key, 'title_') && !str_starts_with($key, 'description_');
        }, ARRAY_FILTER_USE_KEY);

        $category = Category::create($categoryData);
        return $category;
    }

    public function update(array $request, ?Category $category): Category
    {
        // Filter out language-specific fields that don't belong to the main Category model
        $categoryData = array_filter($request, function($key) {
            return !str_starts_with($key, 'title_') && !str_starts_with($key, 'description_');
        }, ARRAY_FILTER_USE_KEY);

        $category->update($categoryData);

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