<?php

namespace Modules\Currency\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Country\Models\Country;
use Modules\Country\Models\CountryDescription;
use Modules\Currency\Interfaces\CurrencyRepositoryInterface;
use Modules\Currency\Models\Currency;
use Modules\Currency\Models\CurrencyDescription;

class CurrencyRepository implements CurrencyRepositoryInterface
{
    public function get(array $request)
    {
        $builder = Currency::query()
            ->join('currency_descriptions AS sd', 'currencies.id', 'sd.currency_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'currencies.*']);
        return $builder;
    }

    public function getItem(int $id)
    {
        return CurrencyDescription::where('currency_id', $id)
            ->join('languages', 'languages.id', 'currency_descriptions.language_id')
            ->join('currencies', 'currencies.id', 'currency_descriptions.currency_id')
            ->select(['currency_descriptions.*', 'languages.local','currencies.*'])->get();

    }

    public function checkItem(int $id)
    {
        return Currency::find($id);
    }
    public function getItemTrashed(int $id)
    {
        return Currency::onlyTrashed()->find($id);
    }

    public function create(array $request): Currency
    {
        return Currency::create($request);
    }

    public function update(array $request, Currency $currency): Currency
    {
        $currency->update($request);

        return $currency;
    }

    public function saveData(array $request, int $currency_id ) :void
    {
        CurrencyDescription::where('currency_id', $currency_id)->delete();

        foreach (languages() as $lang) {
            $data=[
                'currency_id'=> $currency_id,
                'language_id'=> $lang->id,
                'title'=> $request['title_'.$lang->local],
            ];
            CurrencyDescription::create($data);
        }
    }
    /**
     * Remove the specified currency from storage.
     * @param Currency $currency
     * @return void
     */
    public function destroy(Currency $currency) :void
    {
        $currency->delete();
    }

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

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

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