<?php

namespace App\Services\Repositories;

use App\Models\User;
use App\Services\Interfaces\DeliveryRepositoryInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class DeliveryRepository implements DeliveryRepositoryInterface
{
    public function get(Request $request)
    {
        $query = User::where('role','delivery')->orderBy('id', 'DESC');
        if(isset($request->show_all)) {
            if ($request->name) {
                $query->where('name', 'LIKE', '%' . $request->name . '%');
            }

            if ($request->phone) {
                $query->where('phone', $request->phone);
            }

            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) {
            $clients= $query->paginate(50);
        }else{
            $clients=$query->cursor();
        }

        return $clients;
    }

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

    /**
     * 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= User::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= User::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= User::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;
        User::whereIn('id', $ids)->onlyTrashed()->restore();
        return [['message'=> __('dashboard.saveDone')]];
    }
}
