<?php

namespace Modules\Warehouse\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Warehouse\Interfaces\WarehouseRepositoryInterface;
use Modules\Warehouse\Models\Warehouse;
use Modules\Warehouse\Models\WarehouseDescription;

class WarehouseRepository implements WarehouseRepositoryInterface
{
    public function get(array $request)
    {
        $builder = Warehouse::orderBy('id', 'DESC')
            ->join('warehouse_descriptions AS sd', 'warehouses.id', 'sd.warehouse_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'warehouses.*']);
        return $builder;
    }

    public function getItem(int $id)
    {
        return WarehouseDescription::where('warehouse_descriptions.warehouse_id', $id)
            ->join('languages', 'languages.id', 'warehouse_descriptions.language_id')
            ->join('warehouses', 'warehouses.id', 'warehouse_descriptions.warehouse_id')
            ->select([
                'warehouse_descriptions.*',
                'languages.local',
                'warehouses.*'])
            ->get();
    }

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

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

    public function citiesId(Warehouse $warehouse)
    {
        return $warehouse->cities->pluck('id')->toArray() ;
    }

    public function updateCities(array $request , $warehouse)
    {
        $warehouse->cities()->sync($request['city_id']);
    }

    public function create(array $request): Warehouse
    {
        $warehouse = Warehouse::create($request);
        return $warehouse;
    }

    public function update(array $request, ?Warehouse $warehouse): Warehouse
    {
        $warehouse->update($request);

        return $warehouse;
    }

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

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

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

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