<?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::with(['faqs'])
            ->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)
    {
        $category = FaqCategory::with(['faqs'])->find($id);

        $query = FaqCategoryDescription::where('faq_category_id', $category->id)
            ->join('faq_categories', 'faq_category_descriptions.faq_category_id','faq_categories.id')
            ->join('languages', 'languages.id', 'faq_category_descriptions.language_id')
            ->select(['faq_categories.*','faq_category_descriptions.title', 'languages.local']);

        $categoryDescription= $query->get();
        foreach ($categoryDescription as $key=>$row) {

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

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