// 练习方式
export type PracticeMode = 'rounds' | 'time' | 'free'

// 评分方式
export type ScoreMode = 'numeric' | 'letter' | 'comment_only'

// AI 角色
export interface Role {
  id: string
  name: string
  avatar: string
  avatarUrl?: string
  identity: string
  personality: string
  difficulty: number
  isRecommended?: boolean
}

// 评估维度
export interface EvalDimensions {
  accuracy: boolean
  fluency: boolean
  completeness: boolean
  rhythm: boolean
}

// 练习设置
export interface PracticeSettings {
  mode: PracticeMode
  rounds: number
  duration: number // 时长（分钟）
  showEnglish: boolean
  taskHint: boolean
  stutterHint: boolean
}

// 评估设置
export interface EvaluationSettings {
  showReport: boolean
  dimensions: EvalDimensions
  scoreMode: ScoreMode
}

// 学习目标
export interface LearningGoals {
  vocabulary: string[]
  sentences: string[]
}

// 话题讨论配置
export interface TopicDiscussionConfig {
  topic: string
  selectedRole: string
  learningGoals: LearningGoals
  practice: PracticeSettings
  evaluation: EvaluationSettings
}

// 推荐任务卡片
export interface TopicDiscussionTask {
  id: string
  titleZh: string
  titleEn: string
  subtitle: string
  difficulty: 1 | 2 | 3
  rounds: number
  durationMinutes: number
  vocabulary: string[]
  sentences: string[]
  note: string
}

// 教材单元
export interface CurriculumUnit {
  id: string
  number: string
  title: string
  description: string
  vocabulary: string[]
  sentences: string[]
}

// 教材年级
export interface CurriculumGrade {
  id: string
  name: string
  semester: string
  units: CurriculumUnit[]
}

// 教材
export interface CurriculumTextbook {
  id: string
  name: string
  grades: CurriculumGrade[]
}

// 教材数据根
export interface CurriculumData {
  textbooks: CurriculumTextbook[]
}

// 创建方式
export type CreationMode = 'smart' | 'ai' | 'manual'

// 页面层级
export type PageMode = 'layer1' | 'layer2' | 'config'

// 练习类型
export type ExerciseType = 'speaking' | 'listening' | 'reading' | 'writing'

// ==================== 预览组件类型定义 ====================

// 评分等级
export type ScoreLevel = 'excellent' | 'good' | 'fair' | 'needsWork'

// 评分展示方式（复用配置侧的 ScoreMode）
export type ScoreDisplayMode = ScoreMode

// 对话统计数据
export interface DialogueStatistics {
  totalRounds: number
  averageScore: number
  highestScore: number
  highestRound: number
  grammarErrors: number
  excellentExpressions: number
  totalDuration: number // 秒
}

// 下次挑战推荐
export interface NextChallenge {
  difficulty?: string
  unlockedTopic?: string
  suggestedMode?: string
}

// 单句评价
export interface SentenceEvaluation {
  id: string
  round: number
  role: 'ai' | 'student'
  content: string
  contentZh?: string
  audioUrl?: string
  audioDuration?: number
  score?: number
  pronunciation?: {
    accuracy: number
    intonation: number
    stress: number
    fluency: number
  }
  feedback?: {
    highlights: string[]
    corrections: {
      original: string
      corrected: string
      explanation: string
    }[]
    suggestions: string[]
  }
}

// 整体评价报告
export interface OverallEvaluation {
  overallScore: number
  scoreLevel: ScoreLevel
  percentile: number
  dimensions: {
    fluency: number
    interaction: number
    vocabulary: number
    grammar: number
  }
  aiComment: string
  highlights: string[]
  improvements: string[]
  nextChallenge: NextChallenge
  statistics: DialogueStatistics
  sentenceEvaluations: SentenceEvaluation[]
}

// 预览用 AI 角色（扩展配置侧的 Role）
export interface PreviewAIRole {
  id: string
  name: string
  avatar: string
  identity: string
  personality: string
  speakingStyle: 'formal' | 'casual' | 'playful'
  speed: 'slow' | 'normal' | 'fast'
  isCustom: boolean
  isRecommended?: boolean
}

// 消息状态
export type MessageStatus = 'loading' | 'done' | 'error'

// 对话消息
export interface PreviewChatMessage {
  id: string
  role: 'ai' | 'student'
  content: string
  timestamp: Date
  status?: MessageStatus
  error?: string
  /** true 表示这个错误无法重试(如 session 404/409),UI 应提示用户重开 */
  unrecoverable?: boolean
  audioBlob?: Blob
  evaluation?: {
    dimensions: {
      accuracy: 'excellent' | 'good' | 'improve'
      fluency: 'excellent' | 'good' | 'improve'
      completeness: 'excellent' | 'good' | 'improve'
      rhythm: 'excellent' | 'good' | 'improve'
    }
    suggestion: string
    betterExpression?: string
    suggestedWords?: string[]
    wordAnalysis?: {
      word: string
      status: 'correct' | 'improvable'
      userPronunciation?: string
      standardPronunciation?: string
      tip?: string
    }[]
  }
}

// ==================== 对话引擎类型定义 ====================

// SSE 事件类型
export type SSEEvent =
  | { type: 'transcript'; text: string }
  | { type: 'token'; text: string }
  | { type: 'done'; isComplete: boolean }

// 对话会话配置
export interface SessionConfig {
  topic: string
  roleId: string
  totalRounds: number
  vocabulary?: string[]
  sentences?: string[]
}

// 对话会话信息 (createSession 返回)
export interface SessionInfo {
  sessionId: string
  totalRounds: number
  currentRound: number
  expiresAt: string | null
}

// 开场白生成结果 (generateGreeting 返回)
export interface GreetingInfo {
  aiMessage: string
}

// 对话报告
export interface DialogueReport {
  evaluation: OverallEvaluation
}

// 对话 API 接口
export interface DialogueAPI {
  createSession(config: SessionConfig): Promise<SessionInfo>
  /** Throws DOMException('AbortError') on signal abort; throws DialogueApiError on non-OK HTTP. */
  generateGreeting(sessionId: string, signal?: AbortSignal): Promise<GreetingInfo>
  speak(sessionId: string, audioBlob: Blob, signal: AbortSignal): AsyncGenerator<SSEEvent>
  getReport(sessionId: string): Promise<DialogueReport>
}

// 徽章成就
export interface BadgeAchievement {
  id: string
  name: string
  nameEn: string
  icon: string
  description: string
}

// 预览对话状态
export type PreviewDialogueState = 'ready' | 'chatting' | 'completed'
