<?php

namespace Modules\ShippingWay\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\ShippingWay\Interfaces\ShippingWayRepositoryInterface;
use Modules\ShippingWay\Models\ShippingWay;
use Modules\ShippingWay\Models\ShippingWayDescription;

class ShippingWayRepository implements ShippingWayRepositoryInterface
{
    public function get(array $request)
    {
        $builder = ShippingWay::query()
            ->join('shipping_way_descriptions AS sd', 'shipping_ways.id', 'sd.shipping_way_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'shipping_ways.*']);
        return $builder;
    }

    public function getItem(int $id)
    {
        return ShippingWayDescription::where('shipping_way_id', $id)
            ->join('languages', 'languages.id', 'shipping_way_descriptions.language_id')
            ->join('shipping_ways', 'shipping_ways.id', 'shipping_way_descriptions.shipping_way_id')
            ->select(['shipping_way_descriptions.*', 'languages.local','shipping_ways.*'])->get();
    }

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

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

    public function create(array $request): ShippingWay
    {
        $shipping_way = ShippingWay::create($request);
        return $shipping_way;
    }

    public function update(array $request, ?ShippingWay $shipping_way): ShippingWay
    {
        $shipping_way->update($request);

        return $shipping_way;
    }

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

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

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

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