<?php

namespace App\Http\Controllers\Auth;

use App\Actions\Auth\RegisterUserAction;
use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\RegisterRequest;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
use InvalidArgumentException;

class RegisteredUserController extends Controller
{
    public function create(): View
    {
        return view('pages.auth.register');
    }

    public function store(RegisterRequest $request, RegisterUserAction $registerUser): RedirectResponse
    {
        try {
            $user = $registerUser->execute($request->validated());
        } catch (InvalidArgumentException $exception) {
            return back()
                ->withInput($request->except('password', 'password_confirmation'))
                ->withErrors(['phone' => $exception->getMessage()]);
        }

        event(new Registered($user));

        Auth::login($user);

        return redirect()->route('verification.notice');
    }
}
