<?php

namespace Modules\User\Controllers\Api;

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\Enums\UserActivationEnum;
use Modules\User\Models\Resources\UserResource;
use Modules\User\Models\Resources\VisitorResource;
use Modules\User\Requests\Api\ChangeCityRequest;
use Modules\User\Requests\Api\LoginRequest;
use Modules\User\Requests\Api\ProfileRequest;
use Modules\User\Requests\Api\SendCodeRequest;
use Modules\User\Requests\Api\ChangeTypeRequest;
use Modules\User\Services\AccountService;
use Modules\User\Services\AddressService;
use Modules\User\Services\UserService;

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

    public function sendCode(SendCodeRequest $request) :JsonResponse
    {
        $this->userService->sendCode($request->validated());

        $response =
            [
                'status'=>200,
                'data'=>[
                    'email'=>$request->email,
                    'code'=>1234
                ],
                'message'=>__('web.success')
            ];
        return ResponseService::response($response);
    }

    public function login(LoginRequest $request)
    {
        $msg = __('web.success');
        $user = $this->userService->getItemByEmail($request->email);
        if (!$user)
        {
            $user = $this->userService->createUser($request->validated());
        }
        $this->userService->login($user);
        $user = $this->userService->getItem($user->id);
        if ($user->activation == UserActivationEnum::INACTIVE->value && !$request->active)
        {
            return
            [
                'status'=>400,
                'data'=>null,
                'message'=>__('web.inactive_user')
            ];
        }
        if ($user->activation == UserActivationEnum::INACTIVE->value && $request->active)
        {
            $msg = __('web.activated');
            $this->userService->activate();
        }
        $response =
        [
            'status' => 200,
            'data' =>
            [
                'user'=>new UserResource($user),
                'api_token'=>$user->api_token
            ],
            'message' => $msg
        ];
        return ResponseService::response($response);
    }

    public function logout()
    {
        $this->userService->logout();
        $response =
            [
                'status'=>200,
                'data'=>null,
                'message'=>__('web.success')
            ];
        return ResponseService::response($response);
    }

    public function deactivate()
    {
        $this->userService->deactivate();
        $response =
            [
                'status'=>200,
                'data'=>null,
                'message'=>__('web.success')
            ];
        return ResponseService::response($response);
    }

    public function changeCity(ChangeCityRequest $request)
    {
        $account = $this->accountService->getAccount();

        $this->cartService->emptyCart([
            'user_id'=>$account['user']->id,
            'user_status'=>$account['status']
        ]);
        $this->addressService->changeCity(['city_id'=>$request->city_id,'address_id'=>$account['user']->address_id]);
        $response =
            [
                'status'=>200,
                'data'=>'',
                'message'=>__('web.success')
            ];
        return ResponseService::response($response);
    }
    public function changeType(ChangeTypeRequest $request)
    {
        $account = $this->accountService->getAccount();
        if ($account['status'] == AccountStatusEnum::USER->value)
        {
            $user = $this->userService->update([
                'type_id'=> $request->type_id
            ],$account['user']->id);
            $user = $this->userService->getItem($user->id);
            $resource = new UserResource($user);
        }
        else
        {
            $user = $this->accountService->update([
                'type_id'=> $request->type_id
            ],$account['user']);
            $resource = new VisitorResource($user);
        }

        $this->cartService->emptyCart([
            'user_id'=>$account['user']->id,
            'user_status'=>$account['status']
        ]);

        $response =
            [
                'status'=>200,
                'data'=>$resource,
                'message'=>__('web.success')
            ];
        return ResponseService::response($response);
    }

    public function profile()
    {
        $account = $this->accountService->getAccount();
        $user = $account['user'];
        if($account['status'] == AccountStatusEnum::USER->value)
        {
            $user = $this->userService->getItem($user->id);
            $resource = new UserResource($user);
        }
        else
        {
            $resource = new VisitorResource($user);
        }


        $response =
            [
                'status'=>200,
                'data'=>$resource,
                'message'=>''
            ];
        return ResponseService::response($response);
    }

    public function editProfile(ProfileRequest $request)
    {
        $account = $this->accountService->getAccount();
        $user = $account['user'];
        $this->userService->update($request->validated(),$user->id);
        $user = $this->userService->getItem($user->id);
        $response =
            [
                'status'=>200,
                'data'=> new UserResource($user),
                'message'=>__('web.success')
            ];
        return ResponseService::response($response);
    }
}
