/** @module
  @summary The ExpertGuidance type shared by the expert agents, with render
  and fail-open helpers.
*/

/** What an expert consult returns.
- `rules` are for the solver to read;
- `checklist` are concrete, checkable acceptance criteria.
Empty rules and checklist means "nothing domain-specific here". */
export type ExpertGuidance = {
  domain: string;
  rules: string[];
  checklist: string[]
}

export static const emptyGuidance: ExpertGuidance = {
  domain: "",
  rules: [],
  checklist: []
}

export def renderGuidance(guidance: ExpertGuidance): string {
  """Fold expert guidance into a context block the coding agent reads
  before it starts. Empty guidance yields an empty string."""
  if (guidance.rules.length == 0 && guidance.checklist.length == 0) {
    return ""
  }
  const rules = map(guidance.rules, \rule -> "- ${rule}").join("\n")
  const checks = map(guidance.checklist, \check -> "- ${check}").join("\n")
  return "Domain expertise for this task (${guidance.domain}):\n\nKey rules and conventions:\n${rules}\n\nAcceptance checklist — your finished work MUST satisfy every item:\n${checks}"
}

export def guidanceOrEmpty(result: Result<ExpertGuidance>): ExpertGuidance {
  """Unwrap a consult Result, failing open: a consult that errored yields
  empty guidance, so the caller proceeds as if no consult happened."""
  return match(result) {
    success(guidance) => guidance
    failure(_) => emptyGuidance
  }
}
