<?php

namespace Modules\Rate\Repositories;

use Illuminate\Http\Request;
use Modules\Rate\Interfaces\RateRepositoryInterface;
use Modules\Rate\Models\Rate;

class RateRepository implements RateRepositoryInterface
{
    public function get(Request $request)
    {
        $query = Rate::orderBy('id', 'DESC');

        if ($request->rate) {
            $query->where('rate', $request->rate);
        }
        if ($request->product) {
            $query->whereHas('product.currentDescription', function ($q) use ($request){
                $q->where('title','LIKE', '%'.$request->product.'%');
            });
        }
        if ($request->user) {
            $query->where(function ($queryData) use ($request){
                $queryData->whereHas('user', function ($q) use ($request){
                    $q->where('name','LIKE', '%'.$request->user.'%');
                });
            })->orWhere(function ($queryData) use ($request){
                $queryData->whereHas('visitor', function ($q) use ($request){
                    $q->where('name','LIKE', '%'.$request->user.'%');
                });
            });
        }
        if ($request->date_from) {
            $query->whereDate('created_at', '>=', $request->date_from);
        }

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


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

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

        return $rates;
    }

    public function getItem(Rate $rate)
    {
        return $rate ;
    }

    //
    public function save(Request $request, ?Rate $rate): Rate
    {
        $data= $request->all();
        unset($data['id']);// for security reasons
        if (!$rate) {
            $rate = Rate::create($data);
        } else {
            $rate->update($data);
        }
        return $rate;
    }

    /**
     * Remove the specified client from storage.
     * @param Request $request
     * @param int $id
     * @return array
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :array
    {
        $rate= Rate::withTrashed()->find($id);
        if ($rate) {
            if ($rate->deleted_at) {
                $rate->forceDelete();
            } else {
                $rate->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= Rate::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
    {
        $rate= Rate::withTrashed()->find($id);
        if ($rate) {
            $rate->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;
        Rate::whereIn('id', $ids)->onlyTrashed()->restore();
        return [['message'=> __('dashboard.saveDone')]];
    }
}
