<?php

namespace Modules\Review\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Review\Interfaces\ReviewRepositoryInterface;
use Modules\Review\Models\Review;
use Modules\Review\Models\ReviewDescription;

class ReviewRepository implements ReviewRepositoryInterface
{
    public function get(Request $request)
    {
        $query = Review::orderBy('id', 'DESC')
            ->join('review_descriptions AS sd', 'reviews.id', 'sd.review_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.user','sd.description', 'reviews.*']);

        if ($request->title) {
            $query->where('sd.title', 'LIKE', '%'.$request->title.'%');
        }
        if ($request->date_from) {
            $query->whereDate('reviews.created_at', '>=', $request->date_from);
        }

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


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

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

        return $reviews;
    }

    public function getItem(Review $review)
    {
        $query = ReviewDescription::where('review_id', $review->id)
            ->join('languages', 'languages.id', 'review_descriptions.language_id')
            ->select(['review_descriptions.*', 'languages.local']);

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

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

    private function saveData(Request $request, int $review_id ) :void
    {
        $requestData= $request->all();
        foreach (languages() as $lang) {
            $data=[
                'review_id'=> $review_id,
                'language_id'=> $lang->id,
                'user'=> $requestData['user_'.$lang->local],
                'description'=> $requestData['description_'.$lang->local],
            ];

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