<?php

namespace Modules\Page\Repositories;

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

class PageRepository implements PageRepositoryInterface
{
    public function get(Request $request)
    {
        $query = Page::orderBy('id', 'ASC')
            ->join('page_descriptions AS sd', 'pages.id', 'sd.page_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title','sd.description', 'pages.*']);

        if ($request->title) {
            $query->where('sd.title', 'LIKE', '%'.$request->title.'%');
        }
        if($request->ids)
        {
            $query->whereIn('pages.id',$request->ids);
        }
        if($request->not_ids)
        {
            $query->whereNotIn('pages.id',$request->not_ids);
        }
        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) {
            $pages= $query->paginate(50);
        }else{
            $pages=$query->cursor();
        }

        return $pages;
    }

    public function getItemById(int $id)
    {
        return Page::find($id);
    }

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

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

    //
    public function save(Request $request, ?Page $page): Page
    {
        $data= $request->all();
        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,
                'title'=> $requestData['title_'.$lang->local],
                'description'=> $requestData['description_'.$lang->local],
            ];

            PageDescription::create($data);
        }
    }

}
