export interface Question {
  id: number            // SERIAL from question table
  partNumber: number    // from part.part_number — needed for session grouping
  text: string | null   // question_text (null for photo/audio-only questions)
  options: string[]     // answer_choice.label_text ordered A→D
  correctAnswer: number // 0-3 derived from answer_choice.is_correct
  explanation: string | null
  groupId: number
  audioUrl?: string     // from question_group
  imageUrl?: string     // from question_group
  passageText?: string  // from question_group (Part 6/7)
}

// One part's worth of questions inside a test session
export interface SessionPart {
  partNumber: number
  partName: string    // e.g. "Mô tả hình ảnh"
  questions: Question[]
}

// A test record from the test table
export interface TestRecord {
  id: number
  title: string
  description: string | null
  totalQuestions: number
  durationMinutes: number
  categoryName: string | null
}

// A part record from the part table
export interface PartRecord {
  id: number
  testId: number
  partNumber: number
  title: string
  questionCount: number
}

export interface VocabWord {
  id: string
  word: string
  phonetic: string
  meaningVi: string
  topic: VocabTopic
  example: string
}

export type VocabTopic =
  | 'Tất cả'
  | 'Business'
  | 'Office'
  | 'Travel'
  | 'Finance'
  | 'HR'
  | 'Marketing'

export const VOCAB_TOPICS: VocabTopic[] = [
  'Tất cả',
  'Business',
  'Office',
  'Travel',
  'Finance',
  'HR',
  'Marketing',
]

export interface WritingFeedback {
  score: string
  grammar: string[]
  vocabulary: string[]
  structure: string
  improvedVersion: string
  summary: string
}

export interface WritingSubmission {
  id: string
  content: string
  feedback: WritingFeedback
  created_at: string
}

export interface ToeicPart {
  id: number
  name: string
  nameVi: string
  questionCount: number
  icon: string
  progressPercent: number
}

export interface User {
  id: string
  email: string
  name: string
}

// Phase 3 — Gamification
export type UserLevel = 'beginner' | 'bronze' | 'silver' | 'gold' | 'master'

export type XuTransactionType =
  | 'earn_welcome'
  | 'earn_daily'
  | 'earn_streak'
  | 'earn_ads'
  | 'spend_freeze'
  | 'spend_writing'
  | 'spend_test'

export interface UserGamification {
  userId: string
  xp: number
  level: UserLevel
  streak: number
  longestStreak: number
  lastActive: string | null // DATE as ISO string
  xu: number
  freezeCount: number
  createdAt: string
}

export interface XuTransaction {
  id: string
  userId: string
  type: XuTransactionType
  amount: number
  balance: number
  description: string | null
  createdAt: string
}

export interface WeeklyLeaderboardEntry {
  id: string
  userId: string
  weekStart: string
  xpEarned: number
  rank: number | null
}
