require_relative "config"

module Backlog
  # Pure decision over a project's configured tracker. No filesystem, no
  # network: every input (repo path, config contents) is passed in.
  #
  # Post-ADR-0042 (Planka retired), there is no board inventory to resolve
  # against — a project either has a `tracker` key in `.cc-os/config` or it
  # doesn't. Decision, exactly one of:
  #   "use <tracker>"       - a tracker key is configured; use it
  #   "stop-and-discuss"    - no tracker configured; ask the human
  #     (see /os-backlog:route)
  class Resolver
    Decision = Struct.new(:action, :tracker) do
      def to_s
        action == :use ? "use #{tracker}" : "stop-and-discuss"
      end
    end

    # @param repo_path [String] absolute path to the repo (kept for callers
    #   that pass it through; not otherwise used since board heuristics
    #   were retired with Planka)
    # @param config_contents [String, nil] raw contents of .cc-os/config, or nil/absent
    def initialize(repo_path:, config_contents:, **)
      @repo_path = File.expand_path(repo_path)
      @config = Config.new(config_contents)
    end

    # @return [Decision]
    def resolve
      return use(@config.tracker) if @config.tracker_configured?

      stop_and_discuss
    end

    # @return [String] "use <tracker>" or "stop-and-discuss"
    def resolve_string = resolve.to_s

    private

    def use(tracker) = Decision.new(:use, tracker)
    def stop_and_discuss = Decision.new(:stop_and_discuss, nil)
  end
end
