<?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;

class AttributeRepository implements AttributeRepositoryInterface
{
    public function get(Request $request)
    {
        $query = Attribute::orderBy('id', 'DESC')
            ->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.*']);

        if ($request->title) {
            $query->where('sd.title', 'LIKE', '%'.$request->title.'%');
        }
        if (!is_null($request->type)) {
            $query->where('attributes.status', $request->type);
        }
        if ($request->date_from) {
            $query->whereDate('attributes.created_at', '>=', $request->date_from);
        }

        if ($request->date_to) {
            $query->whereDate('attributes.created_at', '<=', $request->date_to);
        }


        if ( !is_null($request->status) && $request->status == 0) {
            $query->onlyTrashed();
        }

        if ($request->paginate ?? null) {
            $attributes= $query->paginate(50);
        }else{
            $attributes=$query->cursor();
        }

        return $attributes;
    }

    public function getItem(Attribute $attribute)
    {
        $query = AttributeDescription::where('attribute_id', $attribute->id)
            ->join('languages', 'languages.id', 'attribute_descriptions.language_id')
            ->select(['attribute_descriptions.*', 'languages.local']);

        $attributeDescription= $query->get();
        // dd($attributeDescription->toArray());
        foreach ($attributeDescription as $row) {
            $attribute[$row->local]= $row;
        }
        return $attribute ;
    }

    //
    public function save(Request $request, ?Attribute $attribute): Attribute
    {
        $data= $request->all();
        unset($data['id']);// for security reasons
        if (!$attribute) {
            $attribute = Attribute::create($data);
        } else {
            $attribute->update($data);
            AttributeDescription::where('attribute_id', $attribute->id)->delete();
        }
        $this->saveData( $request, $attribute->id );
        return $attribute;
    }

    private function saveData(Request $request, int $attribute_id ) :void
    {
        $requestData= $request->all();
        foreach (languages() as $lang) {
            $data=[
                'attribute_id'=> $attribute_id,
                'language_id'=> $lang->id,
                'title'=> $requestData['title_'.$lang->local],
            ];

            AttributeDescription::create($data);
        }
    }
    /**
     * Remove the specified client from storage.
     * @param Request $request
     * @param int $id
     * @return array
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :array
    {
        $attribute= Attribute::withTrashed()->find($id);
        if ($attribute) {
            if ($attribute->deleted_at) {
                $attribute->forceDelete();
            } else {
                $attribute->delete();
            }
            return [ ['message'=> __('dashboard.deletedDone')]];
        } else{
            return [ ['message'=> __('dashboard.noResult')], 400];
        }
    }

    /**
     * Remove several clients by IDs.
     *
     * @param Request $request
     * @return array
     */
    public function destroyAll(Request $request) :array
    {
        $ids= $request->ids;
        $query= Attribute::whereIn('id', $ids);
        if ($request->force) {
            $deleted= $query->onlyTrashed()->forceDelete();
        } else {
            $deleted= $query->delete();
        }
        if($deleted){
            return [['message'=> __('dashboard.deletedDone')]];
        }
        return [ ['message'=> __('dashboard.noResult')], 400];
    }

    /**
     * Restore the specified client from storage.
     *
     * @param Request $request
     * @param int $id
     * @return array
     */
    public function restore(Request $request, int $id) :array
    {
        $attribute= Attribute::withTrashed()->find($id);
        if ($attribute) {
            $attribute->restore();
            return [['message'=> __('dashboard.saveDone')]];
        }
        return [['message'=> __('dashboard.noResult')], 400];

    }

    /**
     * Restore several clients by IDs.
     *
     * @param Request $request
     * @return array
     */
    public function restoreAll(Request $request) :array
    {
        $ids= $request->ids;
        Attribute::whereIn('id', $ids)->onlyTrashed()->restore();
        return [['message'=> __('dashboard.saveDone')]];
    }
}
