/**
 * ModelRouter 类型定义。
 *
 * 与 packages/agent-protocol 暂保持本地副本（workspace 没配 pnpm/turbo，PR15.5 抽包统一）。
 * 详见 docs/modules/agent/02-architecture.md §1.12.4 ModelRouter 接口
 */

export type TaskType =
  | 'tool_call'
  | 'agent_loop'
  | 'classification'
  | 'translation'
  | 'reasoning'
  | 'code'
  | 'vision'
  | 'chat';

export type LatencyPriority = 'realtime' | 'interactive' | 'background';
export type CostPriority = 'budget' | 'standard' | 'premium';
/**
 * AgentSurfaceLabel —— ToolRegistry / RoutingRequest 内部用的 surface 标签（小写）。
 *
 * **跟 Prisma `AgentSurface` enum（大写 WEB/CLI/TEAMS/DESKTOP/IOS/ANDROID）是两个概念**：
 *   - DB enum 用 schema 大写约定（Prisma 风格 + PG 风格）
 *   - 这里用小写标签做 routing / tool availability 过滤
 *
 * 边界由 `normalizeSurfaceLabel()` 翻译；任何外部输入（query string / body）都必须先归一化。
 */
export type AgentSurfaceLabel = 'web' | 'desktop' | 'mobile' | 'teams' | 'cli';

/**
 * 把任意大小写的 surface 字符串归一化为 AgentSurfaceLabel；
 * 无法识别 → 返回 undefined（调用方应兜底到 'web' 或 reject）。
 *
 * 支持的输入：
 *   'web' / 'WEB' / 'Web'  → 'web'
 *   'desktop' / 'DESKTOP'  → 'desktop'
 *   'mobile' / 'IOS' / 'ANDROID' → 'mobile'（iOS/Android 在 routing 层统称 mobile）
 *   'teams' / 'TEAMS'      → 'teams'
 *   'cli' / 'CLI'          → 'cli'
 */
export function normalizeSurfaceLabel(input: string | undefined): AgentSurfaceLabel | undefined {
  if (!input) return undefined;
  const u = input.toUpperCase();
  if (u === 'WEB') return 'web';
  if (u === 'DESKTOP') return 'desktop';
  if (u === 'MOBILE' || u === 'IOS' || u === 'ANDROID') return 'mobile';
  if (u === 'TEAMS') return 'teams';
  if (u === 'CLI') return 'cli';
  return undefined;
}

export interface RoutingRequest {
  // ① 任务信号
  taskType: TaskType;
  toolName?: string;
  subAgentType?: string;
  skillIntent?: string;

  // ② 上下文信号
  contextTokens: number;
  hasImage: boolean;
  hasAudio: boolean;
  turnDepth: number;

  // ③ 偏好信号
  organizationId: string;
  userId: string;
  projectId?: string;
  surface: AgentSurfaceLabel;

  // ④ 历史信号（由后台 job 注入；MVP 可空）
  recentSuccessRate?: number;
  recentP50Latency?: number;

  // ⑤ 成本信号
  monthlyBudgetRemaining?: number;
  latencyPriority: LatencyPriority;
  costPriority: CostPriority;
}

export interface ProviderModelTuple {
  readonly provider: string;
  readonly model: string;
}

export type MatchSource = 'rule' | 'llm_routed' | 'scope_override' | 'default';

export interface RoutingDecision {
  primary: ProviderModelTuple;
  fallbacks: ProviderModelTuple[];
  matchSource: MatchSource;
  matchedRuleId?: string;
  reasoning?: string;
  estimatedCostUsd?: number;
}

/**
 * 规则匹配 pattern 支持的字段（与 RoutingRequest 同名）+ 比较操作符。
 * 简化版（PR3.5 day-1）：
 *   - 标量字段：`{ field: value }` 等值匹配
 *   - 数值字段：`{ field: { '>': N } }` / `{ '<': N }` / `{ '>=': N }` / `{ '<=': N }`
 *   - 布尔字段：`{ field: true }`
 * 不支持 OR / regex；这些进 v2。
 */
export interface RoutingRulePattern {
  taskType?: TaskType | TaskType[];
  toolName?: string | string[];
  subAgentType?: string;
  skillIntent?: string;
  surface?: AgentSurfaceLabel | AgentSurfaceLabel[];
  hasImage?: boolean;
  hasAudio?: boolean;
  latencyPriority?: LatencyPriority;
  costPriority?: CostPriority;
  contextTokens?: number | { '>'?: number; '<'?: number; '>='?: number; '<='?: number };
  turnDepth?: number | { '>'?: number; '<'?: number; '>='?: number; '<='?: number };
}
