<?php

namespace App\Http\Controllers\Api;

use App\Http\Services\AuthService;
use App\Http\Controllers\Controller;
use App\Http\Resources\UserResource;
use App\Http\Requests\Auth\LoginRequest;
use App\Http\Requests\Auth\RegisterRequest;
use App\Http\Requests\Auth\PasswordRecoveryCheck;
use App\Http\Requests\Auth\PasswordRecoveryReset;
use App\Http\Requests\Auth\PasswordRecoveryForgot;
use App\Http\Requests\Auth\IdentifierVerificationRequest;
use App\Http\Requests\Auth\IdentifierVerificationActiveRequest;

class AuthController extends Controller
{
    public function __construct(
        protected AuthService $authServices
    ) {
    }

    public function register(RegisterRequest $request)
    {
        $user_data = $this->authServices->register($request->validated());

        return respond()->created([
            'token' => $user_data['token'],
            'user' => new UserResource($user_data['user'])
        ], __("Verification code has been sent"));
    }

    public function identifierVerificationActive(IdentifierVerificationActiveRequest $request)
    {
        $user = $this->authServices->identifierVerificationActive($request->validated());

        return respond()->success(
            [
                'token' => $user->createToken('auth token')->plainTextToken,
                'user' => new UserResource($user)
            ],
            __('Verification completed successfully')
        );
    }

    public function identifierVerificationRequest(IdentifierVerificationRequest $request)
    {
        $this->authServices->identifierVerificationRequest($request->validated());

        return respond()->ok(__('Verification code has been sent'));
    }

    public function passwordRecoveryForgot(PasswordRecoveryForgot $request)
    {
        $this->authServices->passwordRecoveryForgot($request->validated());

        return respond()->ok(__('Verification code has been sent'));
    }


    public function passwordRecoveryCheck(PasswordRecoveryCheck $request)
    {
        $this->authServices->passwordRecoveryCheck($request->validated());

        return respond()->ok(__('Code is correct'));
    }

    public function passwordRecoveryReset(PasswordRecoveryReset $request)
    {
        $this->authServices->passwordRecoveryReset($request->validated());

        return respond()->ok(__('Your password has been reset'));
    }

    public function login(LoginRequest $request)
    {
        $login_data = $this->authServices->login($request->validated());

        return respond()->ok(
            message: __('You are logged'),
            data: [
                'token' => $login_data['token'],
                'user' => new UserResource($login_data['user'])
            ]
        );
    }

    public function logout()
    {
        $this->authServices->logout();

        return respond()->ok(__('You are logged out'));
    }
}
