<?php

namespace App\Domains\Auth\Controllers;

use App\Domains\Auth\Models\ResetPasswordAttempt;
use App\Domains\Auth\Requests\ValidateResetPasswordAttemptRequest;
use App\Domains\Auth\Services\ResetPasswordAttemptService;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Validation\ValidationException;

class ValidateResetPasswordAttemptController extends Controller
{
    public function __construct(
        private readonly ResetPasswordAttemptService $resetPasswordAttemptService,
    ) {}

    /**
     * @throws ValidationException
     */
    public function __invoke(ValidateResetPasswordAttemptRequest $request): JsonResponse
    {
        $data = $request->validated();

        if (! $this->resetPasswordAttemptService->validateCode(
            $data['email'],
            $data['codigo'],
        )) {
            throw ValidationException::withMessages([
                'codigo' => __('api.auth.reset_code_invalid'),
            ]);
        }

        return response()->json([
            'code' => 'auth.reset_code_valid',
            'message' => __('api.auth.reset_code_valid'),
            'status' => ResetPasswordAttempt::STATUS_VALIDATED,
        ]);
    }
}
