defmodule BerrypodWeb.ContactController do
  use BerrypodWeb, :controller

  alias Berrypod.ContactNotifier

  plug BerrypodWeb.Plugs.RateLimit, [type: :api] when action == :create

  @doc """
  Handles contact form submission (no-JS fallback).
  """
  def create(conn, params) do
    case ContactNotifier.deliver_contact_message(params) do
      {:ok, _} ->
        conn
        |> put_flash(:info, "Message sent! We'll get back to you soon.")
        |> redirect(to: R.contact())

      {:error, :invalid_params} ->
        conn
        |> put_flash(:error, "Please fill in all required fields.")
        |> redirect(to: R.contact())

      {:error, _} ->
        conn
        |> put_flash(:error, "Sorry, something went wrong. Please try again.")
        |> redirect(to: R.contact())
    end
  end
end
