<?php

namespace Modules\User\Repositories;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Modules\User\Interfaces\UserRepositoryInterface;
use Modules\User\Models\User;
use Modules\User\Models\UserCode;

class UserRepository implements UserRepositoryInterface
{
    public function get(Request $request)
    {
        $query = User::orderBy('id', 'DESC');

        if ($request->roles) {
            $query->whereIn('role', $request->roles);
        }
        if ($request->name)
        {
            $query->where('name','LIKE','%'.$request->name.'%');
        }
        if ($request->phone)
        {
            $query->where('phone',$request->phone);
        }
        if ($request->email)
        {
            $query->where('email',$request->email);
        }
        if ($request->store_id)
        {
            $query->where('store_id',$request->store_id);
        }
        if ($request->role)
        {
            $query->where('role',$request->role);
        }
        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) {
            $users= $query->paginate(50);
        }else{
            $users=$query->cursor();
        }

        return $users;
    }

    public function getItem(User $user)
    {
        return $user ;
    }

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

        if ($request->modules){
            $user->modules()->sync($request->modules);
        }

        return $user;
    }

    /**
     * Remove the specified client from storage.
     * @param Request $request
     * @param int $id
     * @return array
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :array
    {
        $user= User::withTrashed()->find($id);
        if ($user) {
            if ($user->deleted_at) {
                $user->forceDelete();
            } else {
                $user->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
    {
        $user= User::withTrashed()->find($id);
        if ($user) {
            $user->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')]];
    }

    public function login(array $requestData)
    {
        $response = null;
        if(Auth::attempt(['phone'=>$requestData['phone'],'password'=>$requestData['password']]))
        {
            $user = Auth::user();
            $response = $user;
        }
        return $response;
    }

    public function sendCode(array $requestData)
    {
        UserCode::create($requestData);
    }

    public function checkCode(array $requestData)
    {
        $code = UserCode::where('target',$requestData['phone'])->orderBy('id','DESC')->first();
        return $code;
    }

    public function getItemByPhone(int $phone)
    {
        $user = User::where('phone',$phone)->first();
        return $user;
    }
}
