import type { AssistantEvalBroadnessLevel, AssistantEvalQuestionType } from "./stage1Contracts";
import type { ProblemUnitType } from "./stage2ProblemUnits";

export const ASSISTANT_STAGE2_EVAL_RECORD_SCHEMA_VERSION = "assistant_stage2_eval_record_v0_1" as const;

export interface AssistantStage2MetricVector {
  problem_unit_precision: number | null;
  problem_unit_recall_proxy: number | null;
  duplicate_collapse_rate: number | null;
  mechanism_coherence_score: number | null;
  problem_clarity_score: number | null;
  problem_first_answer_rate: number | null;
  entity_leakage_rate: number | null;
}

export type AssistantStage2MetricName = keyof AssistantStage2MetricVector;
export type AssistantStage2RubricScore = 0 | 3 | 5;

export interface AssistantStage2RubricBand {
  score: AssistantStage2RubricScore;
  label: string;
  description: string;
}

export interface AssistantStage2EvalRecord {
  schema_version: typeof ASSISTANT_STAGE2_EVAL_RECORD_SCHEMA_VERSION;
  created_at: string;
  case_id: string;
  scenario_tag: string;
  session_id: string | null;
  trace_id: string | null;
  question_type: AssistantEvalQuestionType;
  broadness_level: AssistantEvalBroadnessLevel;
  expected_problem_unit_types: ProblemUnitType[];
  expected_problem_first: boolean;
  problem_units_detected: number;
  candidate_evidence_detected: number;
  duplicate_collapses_detected: number;
  metric_subscores: AssistantStage2MetricVector;
  raw_signals: Record<string, unknown>;
  limitations: string[];
  notes: string[];
}

export const ASSISTANT_STAGE2_SCORING_RUBRIC_V01: Record<AssistantStage2MetricName, AssistantStage2RubricBand[]> = {
  problem_unit_precision: [
    { score: 0, label: "Weak", description: "Problem unit typing is often mismatched against expected case profile." },
    { score: 3, label: "Mixed", description: "Problem unit typing is partially aligned with expected case profile." },
    { score: 5, label: "Strong", description: "Problem unit typing is consistently aligned with expected case profile." }
  ],
  problem_unit_recall_proxy: [
    { score: 0, label: "Weak", description: "Expected problem categories are frequently missing from output." },
    { score: 3, label: "Mixed", description: "Some expected problem categories are captured." },
    { score: 5, label: "Strong", description: "Most expected problem categories are captured." }
  ],
  duplicate_collapse_rate: [
    { score: 0, label: "Weak", description: "Duplicate collapse is rarely observed when candidate evidence is present." },
    { score: 3, label: "Mixed", description: "Duplicate collapse works on part of candidate evidence." },
    { score: 5, label: "Strong", description: "Duplicate collapse consistently reduces noisy candidate evidence." }
  ],
  mechanism_coherence_score: [
    { score: 0, label: "Weak", description: "Mechanism narrative is missing or disconnected from problem units." },
    { score: 3, label: "Mixed", description: "Mechanism narrative is partially connected to problem units." },
    { score: 5, label: "Strong", description: "Mechanism narrative is explicit and coherent with problem units." }
  ],
  problem_clarity_score: [
    { score: 0, label: "Weak", description: "Answer framing remains generic and unclear for accountant workflow." },
    { score: 3, label: "Mixed", description: "Problem framing is partially explicit, but still uneven." },
    { score: 5, label: "Strong", description: "Problem framing is explicit, scoped, and actionable." }
  ],
  problem_first_answer_rate: [
    { score: 0, label: "Weak", description: "Problem-first rendering is rarely applied on applicable cases." },
    { score: 3, label: "Mixed", description: "Problem-first rendering is applied inconsistently." },
    { score: 5, label: "Strong", description: "Problem-first rendering is consistently applied on applicable cases." }
  ],
  entity_leakage_rate: [
    { score: 0, label: "High Leakage", description: "User-facing answers frequently leak raw technical identifiers." },
    { score: 3, label: "Moderate Leakage", description: "Technical identifier leakage appears in a minority of answers." },
    { score: 5, label: "Low Leakage", description: "User-facing answers rarely leak raw technical identifiers." }
  ]
};
