defmodule BerrypodWeb.NewsletterExportController do
  use BerrypodWeb, :controller

  alias Berrypod.Newsletter

  def export(conn, _params) do
    csv = Newsletter.export_all_subscribers_csv()
    today = Date.to_iso8601(Date.utc_today())
    filename = "newsletter-subscribers-#{today}.csv"

    conn
    |> put_resp_content_type("text/csv")
    |> put_resp_header("content-disposition", ~s(attachment; filename="#{filename}"))
    |> send_resp(200, csv)
  end
end
