#!/usr/bin/env ruby
# Deterministic ADR retrieval (layers 1-3). AI judgment belongs in the `find`
# skill, applied only to this command's output.
#
# Usage: adr-find [--root DIR] [--paths a,b] [--components x,y] [--all-statuses]
#   ->  JSON {"candidates": [{id, title, status, date, file, matched_on}]}

require "json"
require_relative "../lib/adr"

root = Dir.pwd
paths = []
components = []
all_statuses = false
ARGV.each_with_index do |arg, i|
  case arg
  when "--root"          then root = ARGV[i + 1]
  when "--paths"         then paths = ARGV[i + 1].to_s.split(",")
  when "--components"    then components = ARGV[i + 1].to_s.split(",")
  when "--all-statuses"  then all_statuses = true
  end
end

repo = Adr::Repository.new(root: root)
finder = Adr::Finder.new(
  repository: repo,
  graph_path: File.join(root, "graphify-out", "graph.json")
)
candidates = finder.find(paths: paths, components: components, all_statuses: all_statuses)
puts JSON.pretty_generate("candidates" => candidates)
