<?php

namespace App\Services\Repositories;

use App\Models\Page;
use App\Models\PageDescription;
use App\Services\Interfaces\PageRepositoryInterface;
use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class PageRepository implements PageRepositoryInterface
{
    public function get(Request $request)
    {
        $query = Page::orderBy('id', 'DESC')
            ->join('page_descriptions AS pd', 'pages.id', 'pd.page_id')
            ->join('languages', 'languages.id', 'pd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['pd.title','pd.description', 'pages.*']);
        if ($request->ids)
        {
            $query->whereIn('pages.id',$request->ids);
        }
        if ($request->name) {
            $query->where('pd.title', 'LIKE', '%'.$request->name.'%');
        }
        if ($request->date_from) {
            $query->whereDate('pages.created_at', '>=', $request->date_from);
        }

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


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

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

        return $clients;
    }

    public function getItem(Page $page)
    {
        $query = PageDescription::where('page_id', $page->id)
            ->join('languages', 'languages.id', 'page_descriptions.language_id')
            ->select(['page_descriptions.*', 'languages.local']);

        $pageDescription= $query->get();
        // dd($pageDescription->toArray());
        foreach ($pageDescription as $row) {
            $page[$row->local]= $row;
        }
        $page['title'] = $page[getCurrentLocale()]->title;
        $page['description'] = $page[getCurrentLocale()]->description;
        return $page ;
    }

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

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

            PageDescription::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
    {
        $client= Page::withTrashed()->find($id);
        if ($client) {
            if ($client->deleted_at) {
                $client->forceDelete();
            } else {
                $client->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= Page::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
    {
        $client= Page::withTrashed()->find($id);
        if ($client) {
            $client->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;
        Page::whereIn('id', $ids)->onlyTrashed()->restore();
        return [['message'=> __('dashboard.saveDone')]];
    }
}
