<?php

namespace Modules\ProductType\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\ProductType\Interfaces\ProductTypeRepositoryInterface;
use Modules\ProductType\Models\ProductType;
use Modules\ProductType\Models\ProductTypeDescription;

class ProductTypeRepository implements ProductTypeRepositoryInterface
{
    public function get(array $request)
    {
        $builder = ProductType::query()
            ->join('product_type_descriptions AS sd', 'product_types.id', 'sd.product_type_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'product_types.*']);
        return $builder;
    }

    public function getItem(int $id)
    {
        return ProductTypeDescription::where('product_type_id', $id)
            ->join('languages', 'languages.id', 'product_type_descriptions.language_id')
            ->join('product_types', 'product_types.id', 'product_type_descriptions.product_type_id')
            ->select(['product_type_descriptions.*', 'languages.local','product_types.*'])->get();
    }

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

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

    public function create(array $request): ProductType
    {
        $product_type = ProductType::create($request);
        return $product_type;
    }

    public function update(array $request, ?ProductType $product_type): ProductType
    {
        $product_type->update($request);

        return $product_type;
    }

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

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

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

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