<?php

namespace Modules\User\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Illuminate\Http\JsonResponse;
use Modules\Order\Services\CartService;
use Modules\User\Enums\AccountStatusEnum;
use Modules\User\Requests\Web\ChangeTypeRequest;
use Modules\User\Requests\Web\LoginRequest;
use Modules\User\Requests\Web\RegistRequest;
use Modules\User\Requests\Web\VerifyRequest;
use Modules\User\Requests\Web\EmailRequest;
use Modules\User\Services\AccountService;
use Modules\User\Services\UserService;

class AuthController extends Controller
{
    public function __construct(
        private UserService $userService,
        private CartService $cartService,
        private AccountService $accountService,
    ){}

    public function login(LoginRequest $request) :JsonResponse
    {
        $this->userService->sendCode($request->validated());
        $response =
        [
            'status'=>200,
            'data'=>[
                'email'=>$request->email
            ],
            'message'=>__('web.success')
        ];
        return ResponseService::response($response);
    }

    public function regist(RegistRequest $request) :JsonResponse
    {
        $this->userService->sendCode($request->validated());
        $response =
        [
            'status'=>200,
            'data'=>[
                'email'=>$request->email
            ],
            'message'=>__('web.success')
        ];
        return ResponseService::response($response);
    }

    public function verify(VerifyRequest $request)
    {
        $user = $this->userService->getItemByEmail($request->email);
        if (!$user)
        {
            $user = $this->userService->createUser($request->validated());
        }
        $this->userService->login($user);
        $response =
            [
                'status'=>200,
                'data'=>route('web.home'),
                'message'=>__('web.success')
            ];
        return ResponseService::response($response);
    }

    public function resend(EmailRequest $request)
    {
        $this->userService->sendCode($request->validated());
        $response =
        [
            'status'=>200,
            'data'=>[
                'email'=>$request->email
            ],
            'message'=>__('web.success')
        ];
        return ResponseService::response($response);
    }
    public function logout()
    {
        $this->userService->logout();
        return redirect(route('web.home'));
    }

    public function changeType(ChangeTypeRequest $request)
    {
        $account = $this->accountService->getAccount();
        if ($account['status'] == AccountStatusEnum::USER->value)
        {
            $this->userService->update([
                'type_id'=> $request->type_id
            ],$account['user']->id);
        }
        else
        {
            $this->accountService->update([
                'type_id'=> $request->type_id
            ],$account['user']);
        }

        $this->cartService->emptyCart([
            'user_id'=>$account['user']->id,
            'user_status'=>$account['status']
        ]);
        session()->flash(__('web.success'));
        return redirect()->back();
    }
}
