<?php

namespace Modules\Type\Repositories;

use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Type\Enums\TypeBoolEnum;
use Modules\Type\Interfaces\TypeRepositoryInterface;
use Modules\Type\Models\Type;
use Modules\Type\Models\TypeDescription;

class TypeRepository implements TypeRepositoryInterface
{
    public function get(array $request)
    {
        $builder = Type::query()
            ->join('type_descriptions AS sd', 'types.id', 'sd.type_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'types.*']);
        return $builder;
    }

    public function getItem(int $id)
    {
        $type = Type::find($id);

        $query = TypeDescription::where('type_id', $type->id)
            ->join('types', 'type_descriptions.type_id','types.id')
            ->join('languages', 'languages.id', 'type_descriptions.language_id')
            ->select(['types.*','type_descriptions.title', 'languages.local']);

        $typeDescription= $query->get();
        // dd($countryDescription->toArray());
        foreach ($typeDescription as $key=>$row) {

            $type[$row->local] = $row;
        }
        return $type;
    }

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

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

    public function create(array $request): Type
    {
        $type = Type::create($request);
        return $type;
    }

    public function update(array $request, ?Type $type): Type
    {
        $type->update($request);

        return $type;
    }

    public function defaultType()
    {
        return Type::where('default_type',TypeBoolEnum::TRUE->value)->first();
    }
    public function unDefaultTypes(Type $type)
    {
        $type->update(['default_type'=>TypeBoolEnum::FALSE->value]);

    }

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

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

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

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