<?php

namespace App\Services\Repositories;

use App\Models\Category;
use App\Models\CategoryDescription;
use App\Services\Integration\Integration\Connection;
use App\Services\Interfaces\CategoryRepositoryInterface;
use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class CategoryRepository implements CategoryRepositoryInterface
{
    public function get(Request $request)
    {
        $query = Category::orderBy('id', 'DESC')
            ->join('category_descriptions AS pd', 'categories.id', 'pd.category_id')
            ->join('languages', 'languages.id', 'pd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['pd.name', 'categories.*']);

        if ($request->category_id)
        {
            $query->where('category_id',$request->category_id);
        }

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

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

        return $categories;
    }

    public function checkItem($id)
    {
        $category = Category::withTrashed()->where('foodics_id', $id)->first();
        return $category;
    }

    public function getItem($id)
    {
        $category = Category::where('foodics_id', $id)->firstOrFail();
        $query = Category::where('categories.foodics_id', $id)
            ->join('category_descriptions', 'category_descriptions.category_id','categories.id')
            ->join('languages', 'languages.id', 'category_descriptions.language_id')
            ->select(['categories.*','category_descriptions.name', 'languages.local']);

        $categoryDescription= $query->get();

        foreach ($categoryDescription as $key=>$row) {
            if ($row->local == getCurrentLocale())
            {
                $category = $row;
            }
            $category[$row->local] = $row;
        }
        return $category ;
    }

    public function create($request)
    {
        $data = $request instanceof Request ?  $request->all() : (array) $request;
        unset($data['created_at']);
        unset($data['updated_at']);
        unset($data['deleted_at']);
        $data['body'] = json_encode($data);
        $data['foodics_id'] = $data['id'];
        $category = Category::create($data);
        foreach (languages() as $lang) {
            $content=[
                'category_id'=> $category->id,
                'language_id'=> $lang->id,
                'name'=> $lang->local == 'en' ?  $data['name'] : (is_null($data['name_localized']) ? $data['name'] : $data['name_localized']) ,
            ];

            CategoryDescription::create($content);
        }
    }
    public function update($request,$id)
    {
        $category = Category::withTrashed()->where('foodics_id', $id)->firstOrFail();
        $data = $request instanceof Request ?  $request->all() : (array) $request;

        unset($data['created_at']);
        unset($data['updated_at']);
        unset($data['deleted_at']);
        $data['body'] = json_encode($data);
        $data['foodics_id'] = $data['id'];
        $category->update($data);
        CategoryDescription::where('category_id',$category->id)->delete();
        foreach (languages() as $lang) {
            $content=[
                'category_id'=> $category->id,
                'language_id'=> $lang->id,
                'name'=> $lang->local == 'en' ?  $data['name'] : (is_null($data['name_localized']) ? $data['name'] : $data['name_localized']) ,
            ];
            CategoryDescription::create($content);
        }
    }
    public function delete($request,$id)
    {
        $category = Category::where('foodics_id', $id)->firstOrFail();
        $category->delete();
    }
    public function restore($request,$id)
    {
        $category = Category::onlyTrashed()->where('foodics_id', $id)->firstOrFail();
        $category->restore();
    }

}
