<?php

namespace App\Http\Controllers\Auth;

use App\Enums\Activity;
use App\Enums\Ask;
use App\Http\Requests\GuestSignupPhoneRequest;
use App\Http\Resources\MenuResource;
use App\Http\Resources\PermissionResource;
use App\Http\Resources\UserResource;
use App\Libraries\AppLibrary;
use App\Services\MenuService;
use App\Services\PermissionService;
use Carbon\Carbon;
use Exception;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use App\Enums\Role as EnumRole;
use App\Services\OtpManagerService;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use App\Http\Requests\VerifyPhoneRequest;
use Dipokhalder\Settings\Facades\Settings;

class GuestSignupController extends Controller
{
    private OtpManagerService $otpManagerService;
    public string $token;
    public PermissionService $permissionService;
    public MenuService $menuService;

    public function __construct(
        OtpManagerService $otpManagerService,
        MenuService $menuService,
        PermissionService $permissionService,
    ) {
        $this->otpManagerService    = $otpManagerService;
        $this->menuService          = $menuService;
        $this->permissionService    = $permissionService;
    }


    public function otp(
        GuestSignupPhoneRequest $request
    ): \Illuminate\Http\Response | \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\Routing\ResponseFactory {
        try {
            $this->otpManagerService->otp($request);
            return response(['status' => true, 'message' => trans("all.message.check_your_phone_for_code")]);
        } catch (Exception $exception) {
            return response(['status' => false, 'message' => $exception->getMessage()], 422);
        }
    }

    public function verify(VerifyPhoneRequest $request)
    {
        try {
            if (Settings::group('site')->get('site_phone_verification') == Activity::DISABLE) {
                $otp = DB::table('otps')->where([
                    ['phone', $request->post('phone')]
                ]);
                $otp?->delete();
                return $this->register(
                    ['code' => $request->code, 'phone' => $request->phone, 'token' => $request->token]
                );
            } elseif ($this->otpManagerService->verify($request) && Settings::group('site')->get(
                'site_phone_verification'
            ) == Activity::ENABLE) {
                return $this->register(
                    ['code' => $request->code, 'phone' => $request->phone, 'token' => $request->token]
                );
            }
        } catch (Exception $exception) {
            return response(['status' => false, 'message' => $exception->getMessage()], 422);
        }
    }

    private function register($array): JsonResponse
    {
        $user = User::where(['phone' => $array['phone']])->first();
        if (!$user) {
            $name = "Guest User";
            $user = User::create([
                'name'              => $name,
                'username'          => Str::slug($name) . rand(11111, 99999),
                'phone'             => $array['phone'],
                'country_code'      => $array['code'],
                'email_verified_at' => Carbon::now()->getTimestamp(),
                'is_guest'          => Ask::YES,
                'password'          => Hash::make(rand(111111, 999999))
            ]);
            $user->assignRole(EnumRole::CUSTOMER);
        }

        if ($user) {
            Auth::guard('web')->loginUsingId($user->id);

            $this->token = $user->createToken('auth_token')->plainTextToken;

            $permission        = PermissionResource::collection($this->permissionService->permission($user->roles[0]));
            $defaultPermission = AppLibrary::defaultPermission($permission);
            $defaultMenu       = (object)AppLibrary::defaultMenu($this->menuService->menu($user->roles[0]), $defaultPermission);

            return new JsonResponse([
                'message'           => trans('all.message.login_success'),
                'token'             => $this->token,
                'user'              => new UserResource($user),
                'menu'              => MenuResource::collection(collect($this->menuService->menu($user->roles[0]))),
                'permission'        => $permission,
                'defaultPermission' => $defaultPermission,
                'defaultMenu'       => $defaultMenu,
            ], 201);
        }
        return new JsonResponse([
            'status'  => false,
            'message' => trans('all.message.credentials_invalid'),
        ], 422);
    }
}