class UsersController < ApplicationController
  allow_unauthenticated_access only: %i[ new create ]

  def new
    render inertia: "users/New", props: {
      flash: flash.to_h
    }
  end

  def create
    @user = User.new(user_params)

    if @user.save
      # Automatically log in the user after registration
      start_new_session_for @user
      redirect_to after_authentication_url, notice: "Welcome! Your account has been created successfully."
    else
      render inertia: "users/New", props: {
        errors: @user.errors.as_json,
        flash: flash.to_h
      }
    end
  end

  private

  def user_params
    params.require(:user).permit(:name, :email_address, :password, :password_confirmation)
  end
end