<?php

namespace Modules\Attribute\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Attribute\Interfaces\AttributeRepositoryInterface;
use Modules\Attribute\Models\Attribute;
use Modules\Attribute\Models\AttributeDescription;
use Modules\Attribute\Models\AttributeOption;

class AttributeRepository implements AttributeRepositoryInterface
{
    public function get(array $request)
    {
        $builder = Attribute::query()
            ->join('attribute_descriptions AS sd', 'attributes.id', 'sd.attribute_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'attributes.*']);
        return $builder;
    }

    public function getItem(int $id)
    {
        return AttributeDescription::where('attribute_id', $id)
            ->join('languages', 'languages.id', 'attribute_descriptions.language_id')
            ->join('attributes', 'attributes.id', 'attribute_descriptions.attribute_id')
            ->select(['attribute_descriptions.*', 'languages.local','attributes.*'])->get();
    }

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

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

    public function create(array $request): Attribute
    {
        $attribute = Attribute::create($request);
        return $attribute;
    }

    public function update(array $request, ?Attribute $attribute): Attribute
    {
        $attribute->update($request);

        return $attribute;
    }

    public function saveData(array $request, int $attribute_id ) :void
    {
        AttributeDescription::where('attribute_id', $attribute_id)->delete();
        foreach (languages() as $lang) {
            $data =
            [
                'attribute_id'=> $attribute_id,
                'language_id'=> $lang->id,
                'title'=> $request['title_'.$lang->local],
                'description'=> $request['description_'.$lang->local] ?? null,
            ];
            AttributeDescription::create($data);
        }
    }


    public function updateOptions(array $request , $attribute)
    {
        $attribute->options()->sync($request['choices']);
    }

    public function choices(Attribute $attribute)
    {
        return AttributeOption::where('attribute_id',$attribute->id)->get();
    }

    /**
     * Remove the specified attribute from storage.
     * @param Attribute $attribute
     * @return void
     */
    public function destroy(Attribute $attribute) :void
    {
        $attribute->delete();
    }

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

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

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