<?php

namespace App\Http\Controllers\API\Auth;

use Carbon\Carbon;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;

class RegisterController extends Controller
{
    public function register(Request $request)
    {
        $validator = Validator::make($request->all(), [

            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required',
            'c_password' => 'required|same:password',
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 400);
        }

        $user = new User;
        $user->first_name = $request->first_name ?? "test";
        $user->last_name = $request->last_name ?? "test";
        $user->street = $request->street ?? null;
        $user->city = $request->city ?? null;
        $user->zip = $request->zip ?? null;
        $user->role = $request->role ?? 'user';
        $user->country = $request->country ?? null;
        $user->phone = $request->phone ?? null;
        $user->email_code = rand(1000, 9999);
        $user->email = $request->email;
        $user->password = bcrypt($request->password);
        $user->save();

        $details = [
            'title' => 'Mail from Exercice Team',
            'body' => 'Hi, welcome to Exercice Lesson',
            'otp' => $user->email_code,
        ];

        // Mail::to($user->email)->send(new \App\Mail\OtpMail($details));
        // $success['token'] = $user->createToken('Exercice')->plainTextToken;
        return response()->json([
            'data' => $user,
            'message' => 'success',
            // 'token_type' => 'Bearer'
        ], 200);
    }

    // Verify Email
    public function verifyEmailOtp(Request $req)
    {
        $user = User::find(Auth::id());
        $userOtp = User::where('id', Auth::id())->where('email_code', $req->otp)->first();
        if ($userOtp) {
            $user->update([
                'email_verified_at' => Carbon::now(),
            ]);
            return [
                'status' => 200,
                'message' => 'Email is verified',
            ];
        } else {
            return [
                'status' => 401,
                'message' => 'Please Enter a Valid Otp',
            ];
        }
    }

    // resend email varification code again.
    public function resendEmail()
    {

        $user = User::find(Auth::id());
        $details = [
            'title' => 'Mail from Exercice Team',
            'body' => 'Hi, welcome in Exercice Lesson',
            'otp' => $user->email_code,
        ];

        Mail::to(Auth::user()->email)->send(new \App\Mail\OtpMail($details));
        return [
            'status' => 200,
            'message' => 'Otp has been sent ',
        ];
    }
    public function changePassword(Request $request){
        $validator = Validator::make($request->all(), [
            'password' => 'required',
            'c_password' => 'required|same:password',
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 400);
        }

        User::find(auth()->user()->id)->update(['password' => bcrypt($request->password)]);
        $user = User::find(Auth::id());

        return response()->json([
                'data' => $user,
                'message' => 'success',
                // 'token_type' => 'Bearer'
            ], 200);
    }
    public function changeAvatar(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required',
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 400);
        }

        User::find(auth()->user()->id)->update(['avatar' => $request->name]);
        $user = User::find(Auth::id());

        return response()->json([
            'data' => $user,
            'message' => 'success',
            // 'token_type' => 'Bearer'
        ], 200);
    }
    public function getProfile()
    {
        $user = User::find(Auth::id());

        return response()->json([
            'data' => $user,
            'message' => 'success',
            // 'token_type' => 'Bearer'
        ], 200);
    }
}
