<?php

namespace Modules\Store\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Store\Interfaces\StoreRepositoryInterface;
use Modules\Store\Models\Store;
use Modules\Store\Models\StoreDescription;

class StoreRepository implements StoreRepositoryInterface
{
    public function get(array $request)
    {
        $builder = Store::query()
            ->join('store_descriptions AS sd', 'stores.id', 'sd.store_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'stores.*']);
        return $builder;
    }

    public function getItem(int $id)
    {
        return StoreDescription::where('store_id', $id)
            ->join('languages', 'languages.id', 'store_descriptions.language_id')
            ->join('stores', 'stores.id', 'store_descriptions.store_id')
            ->select(['store_descriptions.*', 'languages.local','stores.*'])->get();
    }

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

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

    public function create(array $request): Store
    {
        $store = Store::create($request);
        return $store;
    }

    public function update(array $request, ?Store $store): Store
    {
        $store->update($request);

        return $store;
    }

    public function saveData(array $request, int $store_id ) :void
    {
        StoreDescription::where('store_id', $store_id)->delete();
        foreach (languages() as $lang) {
            $data=[
                'store_id'=> $store_id,
                'language_id'=> $lang->id,
                'title'=> $request['title_'.$lang->local],
                'keywords'=> $request['keywords_'.$lang->local],
                'meta_description'=> $request['meta_description_'.$lang->local],
                'address'=> $request['address_'.$lang->local],
            ];
            StoreDescription::create($data);
        }
    }
    /**
     * Remove the specified store from storage.
     * @param Store $store
     * @return void
     */
    public function destroy(Store $store) :void
    {
        $store->delete();
    }

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

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

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