/**
 * 考试作答共享类型定义（ai14）
 *
 * 定义考试作答场景的所有视图模型与数据结构，
 * 供 hooks / components / pages 共享引用。
 */

/** 题型枚举 */
export type QuestionType =
  "single-choice" | "multiple-choice" | "fill-blank" | "short-answer" | "essay";

/** 答案输入（联合类型，按题型区分） */
export type AnswerInput =
  | { type: "single-choice"; optionId: string }
  | { type: "multiple-choice"; optionIds: string[] }
  | { type: "fill-blank"; values: string[] }
  | { type: "short-answer"; text: string }
  | { type: "essay"; text: string };

/** 选项 */
export interface QuestionOption {
  id: string;
  text: string;
}

/** 题目 */
export interface ExamQuestion {
  id: string;
  type: QuestionType;
  content: string;
  options?: QuestionOption[];
  score: number;
  maxScore: number;
}

/** 考试详情 */
export interface ExamDetail {
  id: string;
  name: string;
  durationSeconds: number;
  startsAt: string;
  expiresAt: string;
  questions: ExamQuestion[];
  questionCount: number;
}

/** 考试作答草稿（IDB 持久化） */
export interface ExamTakingDraft {
  examId: string;
  studentId: string;
  answers: Record<string, AnswerInput>;
  startedAt: number;
  serverStartedAt: number;
  lastSavedAt: number | null;
  durationSeconds: number;
  expiresAt: string;
}

/** 可疑行为类型 */
export type ViolationType =
  | "tab-switch"
  | "copy"
  | "paste"
  | "contextmenu"
  | "fullscreen-exit"
  | "multi-tab"
  | "window-blur";

/** 违规记录 */
export interface ViolationRecord {
  type: ViolationType;
  timestamp: number;
  details?: string;
}

/** 题目作答结果 */
export interface QuestionResult {
  questionId: string;
  questionContent: string;
  questionType: QuestionType;
  studentAnswer: AnswerInput | null;
  correctAnswer: AnswerInput | null;
  isCorrect: boolean;
  isAnswered: boolean;
  explanation: string;
  maxScore: number;
  earnedScore: number;
}

/** 考试结果 */
export interface ExamResult {
  examId: string;
  examName: string;
  score: number;
  maxScore: number;
  grade: string;
  submittedAt: string;
  durationSeconds: number;
  questionResults: QuestionResult[];
}

/** 考试结果摘要 */
export interface ExamResultSummary {
  totalQuestions: number;
  correctCount: number;
  wrongCount: number;
  unansweredCount: number;
}

/** 服务器时间同步状态 */
export interface ServerTimeSyncState {
  serverTime: number;
  offset: number;
  isSyncing: boolean;
  lastSyncAt: number | null;
  isDegraded: boolean;
}

/** 获取当前学生 ID（从 localStorage 读取，模拟 auth） */
export function getStudentId(): string {
  if (typeof window === "undefined") return "ssr-placeholder";
  try {
    const stored = window.localStorage.getItem("edu-user");
    if (stored) {
      const user = JSON.parse(stored) as { id?: string };
      if (user.id) return user.id;
    }
  } catch {
    // 解析失败，使用默认值
  }
  return "current-student";
}

/** 构建草稿存储 key */
export function buildDraftKey(examId: string, studentId: string): string {
  return `exam-draft:${examId}:${studentId}`;
}

/** 类名拼接工具（条件类名管理） */
export function cn(
  ...classes: Array<string | false | null | undefined>
): string {
  return classes.filter(Boolean).join(" ");
}
