<?php

namespace Modules\Rate\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Rate\Interfaces\RateRepositoryInterface;
use Modules\Rate\Models\Rate;
use Modules\Rate\Models\RateDescription;

class RateRepository implements RateRepositoryInterface
{
    public function get(array $request)
    {
        $builder = Rate::query();
        return $builder;
    }

    public function getItem(int $id)
    {
        return Rate::find($id);
    }

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

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

    public function create(array $request): Rate
    {
        $rate = Rate::create($request);
        return $rate;
    }

    public function update(array $request, ?Rate $rate): Rate
    {
        $rate->update($request);

        return $rate;
    }

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

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

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

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