<?php

namespace Modules\Quotation\Repositories;

use Illuminate\Http\Request;
use Modules\Quotation\Interfaces\QuotationRepositoryInterface;
use Modules\Quotation\Models\Quotation;

class QuotationRepository implements QuotationRepositoryInterface
{
    public function get(Request $request)
    {
        $query = Quotation::orderBy('id', 'DESC');

        if ($request->order_id) {
            $query->where('order_id',$request->order_id);
        }
        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->phone) {
            $query->where(function ($queryData) use ($request){
                $queryData->whereHas('user', function ($q) use ($request){
                    $q->where('phone',$request->phone);
                });
            })->orWhere(function ($queryData) use ($request){
                $queryData->whereHas('visitor', function ($q) use ($request){
                    $q->where('phone',$request->phone);
                });
            });
        }
        if ($request->email) {
            $query->where(function ($queryData) use ($request){
                $queryData->whereHas('user', function ($q) use ($request){
                    $q->where('email',$request->email);
                });
            })->orWhere(function ($queryData) use ($request){
                $queryData->whereHas('visitor', function ($q) use ($request){
                    $q->where('email',$request->email);
                });
            });
        }
        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) {
            $quotations= $query->paginate(50);
        }else{
            $quotations=$query->cursor();
        }

        return $quotations;
    }

    public function getItem(Quotation $quotation)
    {
        return $quotation ;
    }

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

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