/**
 * Which read tool answers which page.
 *
 * Two callers need this mapping and they must not drift: `page-tools.ts` uses
 * it to decide what to hand the model, and `chat.ts` uses it to name the tool
 * in the system prompt. A model told "you are on /margin" without being told
 * which tool reads the margin book tends to guess at figures instead of
 * calling anything.
 *
 * Deliberately free of database imports so the prompt module does not pull
 * @pig/db in behind it.
 */
import type { PiggyPageRoute } from '@pig/core';

/**
 * Every tool a page may be given. Each name starts `pig_` because
 * `assertPigToolBoundary` refuses the request otherwise, before inference.
 */
export const PIGGY_PAGE_TOOL_NAMES = [
  'pig_get_workspace_summary',
  'pig_get_margin_summary',
  'pig_get_idle_capacity',
  'pig_get_pipeline',
  'pig_get_calendar_ahead',
] as const;

export type PiggyPageToolName = (typeof PIGGY_PAGE_TOOL_NAMES)[number];

export interface PiggyPageGuide {
  /** How the page is named to the model. */
  label: string;
  /** The one tool that grounds an answer about this page. */
  tool: PiggyPageToolName;
}

/**
 * Partial rather than exhaustive: a route added to `PIGGY_PAGE_ROUTES` in
 * @pig/core should fall back to the workspace summary, not fail to compile.
 * The dock publishes a route on every navigation, and a page that cannot be
 * navigated to is worse than a page Piggy knows less about.
 */
const GUIDES: Partial<Record<PiggyPageRoute, PiggyPageGuide>> = {
  '/': { label: 'the dashboard', tool: 'pig_get_workspace_summary' },
  '/growth': { label: 'the growth view', tool: 'pig_get_pipeline' },
  '/margin': { label: 'the margin report', tool: 'pig_get_margin_summary' },
  '/calendar': { label: 'the calendar', tool: 'pig_get_calendar_ahead' },
  '/capacity': { label: 'the capacity book', tool: 'pig_get_idle_capacity' },
  '/demand': { label: 'the demand pipeline', tool: 'pig_get_pipeline' },
  '/supply': { label: 'the supply pipeline', tool: 'pig_get_pipeline' },
  '/accounts': { label: 'the accounts list', tool: 'pig_get_workspace_summary' },
  '/contracts': { label: 'the contracts list', tool: 'pig_get_calendar_ahead' },
  '/imports': { label: 'the imports page', tool: 'pig_get_workspace_summary' },
  '/team': { label: 'the team page', tool: 'pig_get_workspace_summary' },
  '/facts': { label: 'the facts queue', tool: 'pig_get_workspace_summary' },
  '/settings': { label: 'the settings page', tool: 'pig_get_workspace_summary' },
  '/piggy': { label: 'the Piggy page', tool: 'pig_get_workspace_summary' },
};

export function piggyPageGuide(route: PiggyPageRoute): PiggyPageGuide {
  return GUIDES[route] ?? { label: `the ${route} page`, tool: 'pig_get_workspace_summary' };
}
