require "minitest/autorun"
require "tmpdir"
require "fileutils"
require_relative "../lib/adr"

module AdrTestHelpers
  PLUGIN_ROOT = File.expand_path("..", __dir__)
  FIXTURES = File.join(__dir__, "fixtures")

  # Yields a tmp project root containing an initialized docs/adr/ populated
  # with the given ADR fixture contents (id => full file body).
  def with_project(adrs = {})
    Dir.mktmpdir do |root|
      dir = File.join(root, "docs", "adr")
      FileUtils.mkdir_p(dir)
      adrs.each { |filename, body| File.write(File.join(dir, filename), body) }
      yield root
    end
  end

  def sample_adr(id: "0001", title: "Use Postgres", status: "Accepted", extra_frontmatter: "")
    <<~MD
      ---
      id: "#{id}"
      date: 2026-07-03
      status: #{status}
      supersedes:
      superseded-by:
      affected-paths: [db/, lib/storage]
      affected-components: [storage]
      #{extra_frontmatter}---

      # #{id} — #{title}

      ## Context

      We need a database.

      ## Decision

      Use Postgres.

      ## Consequences

      Ops burden.

      ## Alternatives rejected

      SQLite — no concurrent writers.
    MD
  end
end
