<?php

namespace Modules\Reason\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Reason\Interfaces\ReasonRepositoryInterface;
use Modules\Reason\Models\Reason;
use Modules\Reason\Models\ReasonDescription;

class ReasonRepository implements ReasonRepositoryInterface
{
    public function get(array $request)
    {
        $builder = Reason::orderBy('id', 'DESC')
            ->join('reason_descriptions AS sd', 'reasons.id', 'sd.reason_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'reasons.*']);
        return $builder;
    }

    public function getItem(int $id)
    {
        $reason = Reason::find($id);

        $query = ReasonDescription::where('reason_id', $reason->id)
            ->join('reasons', 'reason_descriptions.reason_id','reasons.id')
            ->join('languages', 'languages.id', 'reason_descriptions.language_id')
            ->select(['reasons.*','reason_descriptions.title', 'languages.local']);

        $reasonDescription= $query->get();
        // dd($countryDescription->toArray());
        foreach ($reasonDescription as $key=>$row) {

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

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

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

    public function create(array $request): Reason
    {
        $reason = Reason::create($request);
        return $reason;
    }

    public function update(array $request, ?Reason $reason): Reason
    {
        $reason->update($request);

        return $reason;
    }

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

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

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

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