export type TabKey = "normalized" | "fragments" | "scope" | "flags" | "route" | "raw" | "validation" | "logs";

export interface ConnectionState {
  apiKey: string;
  model: string;
  baseUrl: string;
  temperature: number;
  maxOutputTokens: number;
}

export interface PromptState {
  systemPrompt: string;
  developerPrompt: string;
  domainPrompt: string;
  schemaNotes: string;
  fewShotExamples: string;
}

export interface QueryState {
  userQuestion: string;
  batchQuestionsRaw: string;
  periodHint: string;
  businessContext: string;
  expectedRoute: string;
}

export interface NormalizeResultState {
  trace_id: string;
  ok: boolean;
  normalized: unknown;
  route_hint_summary: unknown;
  raw_model_output: unknown;
  validation: {
    passed: boolean;
    errors: string[];
  };
  usage: {
    input_tokens: number;
    output_tokens: number;
    total_tokens: number;
  };
  latency_ms: number;
  prompt_version: string;
  schema_version: string;
}

export interface HistoryItem {
  trace_id: string;
  timestamp: string;
  model: string;
  question_short: string;
  confidence: string | null;
  validation_passed: boolean;
  route_hint: string | null;
  save_status: "saved";
}

export interface RuntimeRun {
  sessionId: string;
  runId: string;
  status: string;
  initiator: string;
  source: string;
  createdAt: string;
  updatedAt: string;
}

export type UiMode = "assistant" | "decomposition";

export type AssistantFallbackType = "none" | "out_of_scope" | "clarification" | "partial" | "unknown";
export type AssistantReplyType =
  | "factual"
  | "factual_with_explanation"
  | "partial_coverage"
  | "clarification_required"
  | "out_of_scope"
  | "empty_but_valid"
  | "no_grounded_answer"
  | "route_mismatch_blocked"
  | "backend_error";
export type RetrievalResultStatus = "ok" | "empty" | "partial" | "error";
export type RetrievalResultType = "list" | "summary" | "object" | "chain" | "ranking";
export type RetrievalConfidence = "high" | "medium" | "low";

export interface AssistantRequirement {
  requirement_id: string;
  source_fragment_id: string | null;
  requirement_text: string;
  subject_tokens: string[];
  status: "covered" | "partially_covered" | "uncovered" | "clarification_needed" | "out_of_scope";
  route: string | null;
}

export interface RequirementCoverageReport {
  requirements_total: number;
  requirements_covered: number;
  requirements_uncovered: string[];
  requirements_partially_covered: string[];
  clarification_needed_for: string[];
  out_of_scope_requirements: string[];
}

export interface AnswerGroundingCheck {
  status: "grounded" | "partial" | "no_grounded_answer" | "route_mismatch_blocked";
  route_subject_match: boolean;
  missing_requirements: string[];
  reasons: string[];
  why_included_summary: string[];
  selection_reason_summary: string[];
}

export interface UnifiedRetrievalResult {
  fragment_id: string;
  requirement_ids: string[];
  route: string;
  status: RetrievalResultStatus;
  result_type: RetrievalResultType;
  items: Array<Record<string, unknown>>;
  summary: Record<string, unknown>;
  evidence: Array<Record<string, unknown>>;
  why_included: string[];
  selection_reason: string[];
  risk_factors: string[];
  business_interpretation: string[];
  confidence: RetrievalConfidence;
  limitations: string[];
  errors: string[];
}

export interface AssistantDebugState {
  trace_id: string;
  prompt_version: string;
  schema_version: string;
  fallback_type: AssistantFallbackType;
  route_summary: unknown;
  fragments: unknown[];
  requirements_extracted: AssistantRequirement[];
  coverage_report: RequirementCoverageReport;
  routes: Array<Record<string, unknown>>;
  retrieval_status: Array<{
    fragment_id: string;
    requirement_ids: string[];
    route: string;
    status: RetrievalResultStatus;
    result_type: RetrievalResultType;
  }>;
  retrieval_results: UnifiedRetrievalResult[];
  answer_grounding_check: AnswerGroundingCheck;
  dropped_intent_segments: string[];
  normalized: unknown;
}

export interface AssistantConversationItem {
  message_id: string;
  session_id: string;
  role: "user" | "assistant";
  text: string;
  reply_type: AssistantReplyType | null;
  created_at: string;
  trace_id: string | null;
  debug: AssistantDebugState | null;
}

export interface AssistantMessageResultState {
  ok: boolean;
  session_id: string;
  assistant_reply: string;
  reply_type: AssistantReplyType;
  conversation_item: AssistantConversationItem;
  debug: AssistantDebugState;
  conversation: AssistantConversationItem[];
}
