<?php

namespace Modules\FaqCategory\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\FaqCategory\Interfaces\FaqCategoryRepositoryInterface;
use Modules\FaqCategory\Models\FaqCategory;
use Modules\FaqCategory\Models\FaqCategoryDescription;

class FaqCategoryRepository implements FaqCategoryRepositoryInterface
{
    public function get(array $request)
    {
        $builder = FaqCategory::query()
            ->join('faq_category_descriptions AS sd', 'faq_categories.id', 'sd.faq_category_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'faq_categories.*']);
        return $builder;
    }

    public function getItem(int $id)
    {
        return FaqCategoryDescription::where('faq_category_id', $id)
            ->join('languages', 'languages.id', 'faq_category_descriptions.language_id')
            ->join('faq_categories', 'faq_categories.id', 'faq_category_descriptions.faq_category_id')
            ->select(['faq_category_descriptions.*', 'languages.local','faq_categories.*'])->get();
    }

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

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

    public function create(array $request): FaqCategory
    {
        $faq_category = FaqCategory::create($request);
        return $faq_category;
    }

    public function update(array $request, ?FaqCategory $faq_category): FaqCategory
    {
        $faq_category->update($request);

        return $faq_category;
    }

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

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

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

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