defmodule Berrypod.NewsletterFixtures do
  @moduledoc """
  Test helpers for creating newsletter subscribers and campaigns.
  """

  alias Berrypod.Newsletter

  def unique_email, do: "user#{System.unique_integer([:positive])}@example.com"

  @doc """
  Creates a confirmed subscriber directly via Repo, bypassing the
  confirmation email flow.
  """
  def confirmed_subscriber_fixture(attrs \\ %{}) do
    now = DateTime.utc_now() |> DateTime.truncate(:second)

    attrs =
      Enum.into(attrs, %{
        email: unique_email(),
        status: "confirmed",
        confirmed_at: now,
        consent_text: "Test signup"
      })

    %Newsletter.Subscriber{}
    |> Newsletter.Subscriber.changeset(attrs)
    |> Berrypod.Repo.insert!()
  end

  def campaign_fixture(attrs \\ %{}) do
    attrs =
      Enum.into(attrs, %{
        subject: "Test campaign #{System.unique_integer([:positive])}",
        body: "Hello! Unsubscribe: {{unsubscribe_url}}"
      })

    {:ok, campaign} = Newsletter.create_campaign(attrs)
    campaign
  end
end
