<?php

namespace Modules\HospitalPortal\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\User;
use Crypt;
use Error;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Modules\Internal\Emails\SendVerifyEmail;
use Modules\Internal\Events\ForgetPassword;
use Illuminate\Support\Facades\Validator;
use Modules\HospitalPortal\Helpers\ApiResponse;

class AuthController extends Controller
{
    public function login(Request $request)
    {
        $data = [
            'email' => $request->email,
            'password' => $request->password
        ];
        $validator = Validator::make($request->all(), [
            'email' => 'required|email',
            'password' => 'required'
        ], [
            'email.required' => trans('validation.required',['attribute' => 'Email']),
            'email.email' => trans('validation.email'),
            'password.required' => trans('validation.required',['attribute' => 'Password']),
        ]);

        if ($validator->fails())
        {
           return ApiResponse::apiResponse('Bad Request', $data, $validator->errors(), 400);
        }
        else
        {
            $user = User::where('email', $request->email)->first();
            if (!$user) {
                return ApiResponse::apiResponse('Not Found', $data, trans('message.not_found'), 404);
            }

            if (!Hash::check($request->password, $user->password)) {
                return ApiResponse::apiResponse('Bad Request', $data, trans('message.password'), 400);
            }

            $res_data = [
                'user' => $user,
                'token' => $user->createToken('app')->plainTextToken
            ];

            return ApiResponse::apiResponse("Success", $res_data, trans('message.success'), 200);
        }
    }

    public function logout(Request $request)
    {
        $token = $request->bearerToken();
        Auth::user()->tokens()->where('id', $token)->delete();

        return response(['message' => 'Berhasil Logout.']);
    }

    public function resetPassword(Request $request)
    {
        $user = Auth::user();

        $request->validate([
            'old_password' => 'required',
            'new_password' => 'required',
            'confirm_new_password' => 'required'
        ]);

        if (!Hash::check($request['old_password'], $user->password)) {
            return response(['message' => 'Password Salah'], 403);
        }

        if ($request["new_password"] != $request["confirm_new_password"]) {
            return response([
                'message' => "Password Tidak Sama"
            ]);
        }

        $user->update([
            'password' => Hash::make($request->confirm_new_password),
        ]);
        return response()->json($user);
    }

    public function verifyEmail(Request $request)
    {
        $request->validate([
            'email' => 'required|email',
        ]);

        $user = User::query()
            ->where('email', $request->email)
            ->first();

        if (!$user) {
            return response(['message' => 'User Tidak Ditemukan'], 404);
        }

        Event(new ForgetPassword($user));

        // Mail::to($user->email)->send(new SendVerifyEmail($user));

        return response()->json($user);
    }

    public function forgetPassword(Request $request)
    {
        $request->validate([
            'new_password' => 'required',
            'confirm_new_password' => 'required'
        ]);

        $token = Crypt::decryptString($request->token);
        $email = explode('|', $token)[0];

        $user = User::query()
            ->where('email', $email)
            ->first();

        if (!$user) {
            return response(['message' => 'User Tidak Ditemukan'], 404);
        }

        if ($request["new_password"] != $request["confirm_new_password"]) {
            return response([
                'message' => "Password Tidak Sama"
            ], 404);
        }

        $user->update([
            'password' => Hash::make($request->confirm_new_password),
        ]);
        return response()->json($user);
    }
}
