<?php

namespace Modules\Faq\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Faq\Interfaces\FaqRepositoryInterface;
use Modules\Faq\Models\Faq;
use Modules\Faq\Models\FaqDescription;

class FaqRepository implements FaqRepositoryInterface
{
    public function get(Request $request)
    {
        $query = Faq::orderBy('id', 'DESC')
            ->join('faq_descriptions AS sd', 'faqs.id', 'sd.faq_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.question', 'faqs.*']);

        if ($request->question) {
            $query->where('sd.question', 'LIKE', '%'.$request->question.'%');
        }
        if (!is_null($request->statusType)) {
            $query->where('faqs.status',  $request->statusType);
        }
        if ($request->date_from) {
            $query->whereDate('faqs.created_at', '>=', $request->date_from);
        }

        if ($request->date_to) {
            $query->whereDate('faqs.created_at', '<=', $request->date_to);
        }


        if ( !is_null($request->status) && $request->status == 0) {
            $query->onlyTrashed();
        }

        if ($request->paginate ?? null) {
            $faqs= $query->paginate(50);
        }else{
            $faqs=$query->cursor();
        }

        return $faqs;
    }

    public function getItem(Faq $faq)
    {
        $query = FaqDescription::where('faq_id', $faq->id)
            ->join('languages', 'languages.id', 'faq_descriptions.language_id')
            ->select(['faq_descriptions.*', 'languages.local']);

        $faqDescription= $query->get();
        // dd($faqDescription->toArray());
        foreach ($faqDescription as $row) {
            $faq[$row->local]= $row;
        }
        return $faq ;
    }

    //
    public function save(Request $request, ?Faq $faq): Faq
    {
        $data= $request->all();
        unset($data['id']);// for security reasons
        if (!$faq) {
            $faq = Faq::create($data);
        } else {
            $faq->update($data);
            FaqDescription::where('faq_id', $faq->id)->delete();
        }
        $this->saveData( $request, $faq->id );
        return $faq;
    }

    private function saveData(Request $request, int $faq_id ) :void
    {
        $requestData= $request->all();
        foreach (languages() as $lang) {
            $data=[
                'faq_id'=> $faq_id,
                'language_id'=> $lang->id,
                'question'=> $requestData['question_'.$lang->local],
                'answer'=> $requestData['answer_'.$lang->local],
            ];

            FaqDescription::create($data);
        }
    }
    /**
     * Remove the specified client from storage.
     * @param Request $request
     * @param int $id
     * @return array
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :array
    {
        $faq= Faq::withTrashed()->find($id);
        if ($faq) {
            if ($faq->deleted_at) {
                $faq->forceDelete();
            } else {
                $faq->delete();
            }
            return [ ['message'=> __('dashboard.deletedDone')]];
        } else{
            return [ ['message'=> __('dashboard.noResult')], 400];
        }
    }

    /**
     * Remove several clients by IDs.
     *
     * @param Request $request
     * @return array
     */
    public function destroyAll(Request $request) :array
    {
        $ids= $request->ids;
        $query= Faq::whereIn('id', $ids);
        if ($request->force) {
            $deleted= $query->onlyTrashed()->forceDelete();
        } else {
            $deleted= $query->delete();
        }
        if($deleted){
            return [['message'=> __('dashboard.deletedDone')]];
        }
        return [ ['message'=> __('dashboard.noResult')], 400];
    }

    /**
     * Restore the specified client from storage.
     *
     * @param Request $request
     * @param int $id
     * @return array
     */
    public function restore(Request $request, int $id) :array
    {
        $faq= Faq::withTrashed()->find($id);
        if ($faq) {
            $faq->restore();
            return [['message'=> __('dashboard.saveDone')]];
        }
        return [['message'=> __('dashboard.noResult')], 400];

    }

    /**
     * Restore several clients by IDs.
     *
     * @param Request $request
     * @return array
     */
    public function restoreAll(Request $request) :array
    {
        $ids= $request->ids;
        Faq::whereIn('id', $ids)->onlyTrashed()->restore();
        return [['message'=> __('dashboard.saveDone')]];
    }
}
