/**
 * What a tool is called when a person reads it.
 *
 * There were three of these tables. `piggy/tool.tsx` named the eleven read
 * tools; `piggy/approval-card.tsx` named the five write tools; the evidence
 * rail title-cased whatever it was handed. So the transcript could call a step
 * "Idle capacity" while the approval card two rows below called its own step
 * "Log Activity", and a tool absent from every table arrived as "Get Record By
 * Id" — the identifier with its underscores taken out, presented as English.
 *
 * One table, therefore, covering every tool Piggy can call, read and write
 * alike, and one fallback. The fallback capitalises the first letter only:
 * "Get record by id" is still a bad name, but it reads as a name rather than as
 * a title, so a missing entry looks like an omission instead of a decision.
 *
 * The source of truth is `apps/piggy/src`: `chat-tools.ts` (the focused read and
 * the four lookups), `page-tools.ts` (one per route), `lifecycle-tools.ts`,
 * `write-tools.ts` (the five that need approval) and `tools.ts` (the two given
 * only to a queued background task). When a tool is added there, it is added
 * here in the same change — a chat transcript that cannot name a step it just
 * took is a transcript nobody trusts.
 */

/**
 * Reads. Named for what they return, not for how they are fetched, because the
 * reader is scanning for the subject: "Renewal deadlines" answers "what did it
 * look at" in a way that "List renewals" does not.
 */
const READ_TOOL_LABELS: Record<string, string> = {
  // chat-tools.ts — the record the panel was opened from, and the lookups.
  pig_get_record: 'Record in focus',
  pig_get_record_by_id: 'Record lookup',
  pig_search_records: 'Record search',
  pig_list_renewals: 'Renewal deadlines',
  pig_list_inventory: 'Provider inventory',

  // lifecycle-tools.ts — only ever given alongside an account context.
  pig_get_account_lifecycle: 'Account lifecycle',

  // page-tools.ts — one per route, chosen by where the dock is standing.
  pig_get_workspace_summary: 'Workspace summary',
  pig_get_margin_summary: 'Margin book',
  pig_get_idle_capacity: 'Idle capacity',
  pig_get_pipeline: 'Open pipeline',
  pig_get_calendar_ahead: 'Calendar ahead',

  // tools.ts — the queued research task's own pair. These never appear in an
  // interactive transcript, but they do appear in the run ledger and in the
  // evidence counts, which is where the title-cased fallback was showing.
  pig_get_subject: 'Task subject',
  pig_record_fact: 'Propose a fact',
};

/**
 * Writes. These carry a `label` in `write-tools.ts` and the strings here are
 * that label verbatim: the server names the change in the approval payload and
 * the client names the step in the transcript, and the two must not disagree
 * about what the user just approved.
 */
const WRITE_TOOL_LABELS: Record<string, string> = {
  pig_log_activity: 'Log activity',
  pig_create_contact: 'Create contact',
  pig_update_deal_stage: 'Update deal stage',
  pig_update_record_fields: 'Update record fields',
  pig_create_task: 'Create task',
};

/** Every tool Piggy can call, read and write, in one lookup. */
export const PIGGY_TOOL_LABELS: Record<string, string> = {
  ...READ_TOOL_LABELS,
  ...WRITE_TOOL_LABELS,
};

/**
 * The tools that change the book. Exported as a set rather than left implicit
 * so a list can group reads above writes — the ordering the run ledger and the
 * evidence rail both want — without a second copy of the membership test.
 */
export const PIGGY_WRITE_TOOL_NAMES: ReadonlySet<string> = new Set(
  Object.keys(WRITE_TOOL_LABELS),
);

/** True for a tool whose result needs a person's approval before it lands. */
export function isPiggyWriteTool(name: string): boolean {
  return PIGGY_WRITE_TOOL_NAMES.has(name);
}

/**
 * The reader's name for a tool.
 *
 * Unknown identifiers keep their words and lose only the `pig_` prefix and the
 * underscores. Title-casing them was the old behaviour and it was the worse
 * one: "Get Record By Id" claims to be a designed label, while "Get record by
 * id" reads as the identifier it is.
 */
export function piggyToolLabel(name: string): string {
  const known = PIGGY_TOOL_LABELS[name];
  if (known) return known;

  const words = name.replace(/^pig_/, '').replaceAll('_', ' ').trim();
  if (!words) return name;
  return `${words.charAt(0).toUpperCase()}${words.slice(1)}`;
}
