defmodule WhoNeedHelp.CatalogModeration do
  @moduledoc """
  Produces a PII-free snapshot for an operator-run, advisory Codex review.

  Codex never writes to the database. An administrator must review and apply
  every recommendation through a separate moderation action.
  """

  alias WhoNeedHelp.Catalog
  alias WhoNeedHelp.Catalog.Category

  def export_open_proposals do
    proposals =
      Catalog.list_proposals()
      |> Enum.map(fn proposal ->
        %{
          proposal_id: proposal.id,
          proposed_name: proposal.proposed_name,
          reason: proposal.reason,
          mode: proposal.mode,
          parent_slug: parent_slug(proposal.parent),
          community_votes: proposal.vote_count
        }
      end)

    Jason.encode!(%{
      generated_at: DateTime.utc_now(:second),
      policy: "Advisory review only. No automatic approval, rejection, merge, or database write.",
      proposals: proposals
    })
  end

  defp parent_slug(%Category{slug: slug}), do: slug
  defp parent_slug(_), do: nil
end
