<?php

namespace Modules\Canteen\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Canteen\Interfaces\CanteenRepositoryInterface;
use Modules\Canteen\Models\Canteen;
use Modules\Canteen\Models\CanteenDescription;

class CanteenRepository implements CanteenRepositoryInterface
{
    public function get(array $request)
    {
        $builder = Canteen::query()
            ->join('canteen_descriptions AS sd', 'canteens.id', 'sd.canteen_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'canteens.*']);
        return $builder;
    }

    public function getItem(int $id)
    {
        return CanteenDescription::where('canteen_id', $id)
            ->join('languages', 'languages.id', 'canteen_descriptions.language_id')
            ->join('canteens', 'canteens.id', 'canteen_descriptions.canteen_id')
            ->select(['canteen_descriptions.*', 'languages.local','canteens.*'])->get();
    }

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

    public function getItemByCode(string $code)
    {
        return Canteen::where('code',$code)->first();
    }

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


    public function create(array $request): Canteen
    {
        $canteen = Canteen::create($request);
        return $canteen;
    }

    public function update(array $request, ?Canteen $canteen): Canteen
    {
        $canteen->update($request);

        return $canteen;
    }

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

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

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

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