export type ConfidenceLevel = "high" | "medium" | "low";

export type IntentClass =
  | "heavy_analytical"
  | "cross_entity"
  | "drilldown_explain"
  | "rule_based_account_control"
  | "anomaly_probe"
  | "period_close_risk"
  | "ambiguous_human_query"
  | "simple_factual";

export type RouteHint =
  | "store_canonical"
  | "store_feature_risk"
  | "hybrid_store_plus_live"
  | "live_mcp_drilldown"
  | "batch_refresh_then_store";

export type DeterministicRouteHint = RouteHint | "no_route";

export type PromptVersion =
  | "normalizer_v1"
  | "normalizer_v1_1"
  | "normalizer_v1_1_1"
  | "normalizer_v1_1_2"
  | "normalizer_v1_1_2_1"
  | "normalizer_v2"
  | "normalizer_v2_0_1"
  | "normalizer_v2_0_2";

export type EvalRunMode = "standard" | "single-pass-strict";

export interface NormalizedQueryV1 {
  schema_version: "normalized_query_v1";
  user_question_raw: string;
  normalized_question: string;
  intent_class: IntentClass;
  business_problem_type: string;
  domain_entities: string[];
  accounts_mentioned: string[];
  documents_mentioned: string[];
  registers_mentioned: string[];
  period_scope: {
    type: "explicit" | "inferred" | "missing";
    value: string | null;
    confidence: ConfidenceLevel;
  };
  requires: {
    needs_cross_entity_join: boolean;
    needs_causal_chain: boolean;
    needs_exact_object_trace: boolean;
    needs_ranking: boolean;
    needs_anomaly_summary: boolean;
    needs_runtime_truth: boolean;
    needs_period_cut: boolean;
    needs_evidence: boolean;
  };
  expected_output_shape:
    | "ranked_list"
    | "evidence_chain"
    | "anomaly_summary"
    | "point_answer"
    | "reconciliation_report"
    | "prioritized_review_list";
  route_hint: RouteHint;
  ambiguities: Array<{
    field: string;
    reason: string;
    severity: "low" | "medium" | "high";
  }>;
  confidence: {
    overall: ConfidenceLevel;
    intent_class: ConfidenceLevel;
    route_hint: ConfidenceLevel;
  };
}

export type V2DomainRelevance = "in_scope" | "out_of_scope" | "unclear";
export type V2BusinessScope = "company_specific_accounting" | "generic_accounting" | "offtopic" | "unclear";
export type ExecutionReadiness = "executable" | "executable_with_soft_assumptions" | "needs_clarification" | "no_route";
export type SoftAssumption =
  | "period_from_session_context"
  | "company_scope_defaulted"
  | "problem_scan_mode_enabled";
export type RouteStatus = "routed" | "no_route";
export type NoRouteReason = "out_of_scope" | "insufficient_specificity" | "missing_mapping" | "unsupported_fragment_type";

export interface NormalizedFragmentV2 {
  fragment_id: string;
  raw_fragment_text: string;
  normalized_fragment_text: string;
  domain_relevance: V2DomainRelevance;
  business_scope: V2BusinessScope;
  entity_hints: string[];
  account_hints: string[];
  document_hints: string[];
  register_hints: string[];
  time_scope: {
    type: "explicit" | "inferred" | "missing";
    value: string | null;
    confidence: ConfidenceLevel;
  };
  flags: {
    has_multi_entity_scope: boolean;
    asks_for_chain_explanation: boolean;
    asks_for_ranking_or_top: boolean;
    asks_for_period_summary: boolean;
    asks_for_rule_check: boolean;
    asks_for_anomaly_scan: boolean;
    asks_for_exact_object_trace: boolean;
    asks_for_evidence: boolean;
    mentions_period_close_context: boolean;
  };
  candidate_labels: IntentClass[];
  confidence: ConfidenceLevel;
}

export interface NormalizedFragmentV2_0_1 extends NormalizedFragmentV2 {
  execution_readiness: ExecutionReadiness;
  clarification_reason: string | null;
  soft_assumption_used: SoftAssumption[];
}

export interface NormalizedFragmentV2_0_2 extends NormalizedFragmentV2_0_1 {
  route_status: RouteStatus;
  no_route_reason: NoRouteReason | null;
}

export interface DiscardedFragmentV2 {
  raw_fragment_text: string;
  reason: string;
}

export interface NormalizedQueryV2 {
  schema_version: "normalized_query_v2";
  user_message_raw: string;
  message_in_scope: boolean;
  scope_confidence: ConfidenceLevel;
  contains_multiple_tasks: boolean;
  fragments: NormalizedFragmentV2[];
  discarded_fragments: DiscardedFragmentV2[];
  global_notes: {
    needs_clarification: boolean;
    clarification_reason: string | null;
  };
}

export interface NormalizedQueryV2_0_1 {
  schema_version: "normalized_query_v2_0_1";
  user_message_raw: string;
  message_in_scope: boolean;
  scope_confidence: ConfidenceLevel;
  contains_multiple_tasks: boolean;
  fragments: NormalizedFragmentV2_0_1[];
  discarded_fragments: DiscardedFragmentV2[];
  global_notes: {
    needs_clarification: boolean;
    clarification_reason: string | null;
  };
}

export interface NormalizedQueryV2_0_2 {
  schema_version: "normalized_query_v2_0_2";
  user_message_raw: string;
  message_in_scope: boolean;
  scope_confidence: ConfidenceLevel;
  contains_multiple_tasks: boolean;
  fragments: NormalizedFragmentV2_0_2[];
  discarded_fragments: DiscardedFragmentV2[];
  global_notes: {
    needs_clarification: boolean;
    clarification_reason: string | null;
  };
}

export interface RouteHintSummaryV1 {
  mode: "legacy_v1";
  intent_class: IntentClass;
  route_hint: RouteHint;
  confidence: ConfidenceLevel;
  decision_flags: {
    needs_cross_entity_join: boolean;
    needs_causal_chain: boolean;
    needs_exact_object_trace: boolean;
    needs_ranking: boolean;
    needs_anomaly_summary: boolean;
    needs_runtime_truth: boolean;
    needs_period_cut: boolean;
    needs_evidence: boolean;
  };
  period_scope: NormalizedQueryV1["period_scope"];
  entities: {
    domain_entities: string[];
    accounts_mentioned: string[];
    documents_mentioned: string[];
    registers_mentioned: string[];
  };
}

export interface RouteDecisionV2 {
  fragment_id: string;
  domain_relevance: V2DomainRelevance;
  business_scope: V2BusinessScope;
  candidate_labels: IntentClass[];
  decision_flags: NormalizedFragmentV2["flags"];
  execution_readiness?: ExecutionReadiness | null;
  clarification_reason?: string | null;
  soft_assumption_used?: SoftAssumption[];
  route_status?: RouteStatus | null;
  no_route_reason?: NoRouteReason | null;
  route: DeterministicRouteHint;
  reason: string;
}

export interface RouteHintSummaryV2 {
  mode: "deterministic_v2";
  message_in_scope: boolean;
  scope_confidence: ConfidenceLevel;
  planner: {
    total_fragments: number;
    in_scope_fragments: number;
    out_of_scope_fragments: number;
    discarded_fragments: number;
    contains_multiple_tasks: boolean;
  };
  decisions: RouteDecisionV2[];
  fallback: {
    type: "none" | "out_of_scope" | "clarification" | "partial";
    message: string | null;
  };
}

export type RouteHintSummary = RouteHintSummaryV1 | RouteHintSummaryV2;
export type NormalizedPayload = NormalizedQueryV1 | NormalizedQueryV2 | NormalizedQueryV2_0_1 | NormalizedQueryV2_0_2;

export interface NormalizeRequestPayload {
  apiKey?: string;
  model?: string;
  baseUrl?: string;
  temperature?: number;
  maxOutputTokens?: number;
  promptVersion?: PromptVersion | string;
  systemPrompt?: string;
  developerPrompt?: string;
  domainPrompt?: string;
  schemaVersion?: string;
  userQuestion: string;
  context?: {
    period_hint?: string;
    business_context?: string;
    expected_route?: RouteHint;
    eval_label?: string;
    case_id?: string;
    eval_mode?: EvalRunMode;
  };
  fewShotExamples?: string;
  saveAsTestCase?: boolean;
  useMock?: boolean;
  retryPolicy?: "default" | "single-pass-strict";
}

export interface ValidationResult {
  passed: boolean;
  errors: string[];
}

export interface NormalizeResponsePayload {
  trace_id: string;
  ok: boolean;
  normalized: NormalizedPayload | null;
  route_hint_summary: RouteHintSummary | null;
  raw_model_output: unknown;
  validation: ValidationResult;
  usage: {
    input_tokens: number;
    output_tokens: number;
    total_tokens: number;
  };
  latency_ms: number;
  prompt_version: string;
  schema_version: string;
  request_count_for_case: number;
}
