<?php

namespace Modules\PaymentWay\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\PaymentWay\Interfaces\PaymentWayRepositoryInterface;
use Modules\PaymentWay\Models\PaymentWay;
use Modules\PaymentWay\Models\PaymentWayDescription;

class PaymentWayRepository implements PaymentWayRepositoryInterface
{
    public function get(array $request)
    {
        $builder = PaymentWay::query()
            ->join('payment_way_descriptions AS sd', 'payment_ways.id', 'sd.payment_way_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'payment_ways.*']);
        return $builder;
    }

    public function getItem(int $id)
    {
        return PaymentWayDescription::where('payment_way_id', $id)
            ->join('languages', 'languages.id', 'payment_way_descriptions.language_id')
            ->join('payment_ways', 'payment_ways.id', 'payment_way_descriptions.payment_way_id')
            ->select(['payment_way_descriptions.*', 'languages.local','payment_ways.*'])->get();
    }

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

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

    public function create(array $request): PaymentWay
    {
        $payment_way = PaymentWay::create($request);
        return $payment_way;
    }

    public function update(array $request, ?PaymentWay $payment_way): PaymentWay
    {
        $payment_way->update($request);

        return $payment_way;
    }

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

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

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

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