<?php

namespace App\Services\Repositories;

use App\Models\PhoneCode;
use App\Models\User;
use App\Services\Integration\Integration\SMS;
use App\Services\Interfaces\MemberRepositoryInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class UserRepository implements MemberRepositoryInterface
{
    public function get(Request $request)
    {
        $query = User::where('role','super_admin')->orderBy('id', 'DESC');
        if ($request->user_id) {
            $query->where('id',  $request->user_id);
        }

        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 $user): User
    {
        // dd($request->all('warranties')['warranties']);
        $request->merge(['role'=>'user']);
        $data= $request->all();
        unset($data['id']);// for security reasons
        // dd($data);
        if (!$user) {
            $user = User::create($data);
        } else {
            $user->update($data);
        }
        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
    {
        $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')]];
    }

    public function sendCode(Request $request)
    {
        $code = rand(11111,99999);
        PhoneCode::create([
            'phone'=>$request->phone,
            'code'=>$code
        ]);
        $message = "Your Code : ".$code;
        SMS::sendSMS($request->phone,$message);
        return true;
    }
    

    public function checkCode(Request $request)
    {
        $code = PhoneCode::where('phone',$request->phone)->first();
        if($code)
        {
            if($code->code == $request->code)
            {
                $member = User::where('phone',$request->phone)->first();
                if (!$member)
                {
                    $member = User::create([
                        'phone'=>$request->phone
                    ]);
                }
                $request->merge(['role'=>'user']);
                $this->save($request,$member);
                Auth::login($member);
                $response = [
                    'status'=>200,
                    'message'=>__('web.success_code'),
                    'data'=> route('web.home'),
                ];
                return $response;
            }
        }

        $response = [
            'status'=>400,
            'message'=>[__('web.check_code')],
            'data'=> route('web.code').'?phone='.$request->phone,
        ];
        return $response;
    }
}
