defmodule Blogex.Test.Setup do
  @moduledoc """
  Reusable setup blocks for blog tests.
  """

  import Blogex.Test.PostBuilder

  @doc """
  Starts a FakeBlog with a standard set of posts.
  Returns `%{blog: module, posts: posts}`.
  """
  def with_blog(context \\ %{}, opts \\ []) do
    posts = Keyword.get(opts, :posts, default_posts())
    blog_opts = Keyword.drop(opts, [:posts])

    {:ok, _} = Blogex.Test.FakeBlog.start(posts, blog_opts)

    Map.merge(context, %{blog: Blogex.Test.FakeBlog, posts: posts})
  end

  @doc "A small set of posts covering common scenarios."
  def default_posts do
    [
      build(
        id: "newest-post",
        date: ~D[2026-03-10],
        tags: ["elixir", "otp"],
        published: true
      ),
      build(
        id: "middle-post",
        date: ~D[2026-02-15],
        tags: ["elixir", "testing"],
        published: true
      ),
      build(
        id: "oldest-post",
        date: ~D[2026-01-05],
        tags: ["devops"],
        published: true
      ),
      build(
        id: "draft-post",
        date: ~D[2026-03-12],
        tags: ["elixir"],
        published: false
      ),
      build(
        id: "future-post",
        date: ~D[2099-01-01],
        tags: ["future-only"],
        published: true
      )
    ]
  end
end
