export interface ApiResponse<T> {
  code: number
  message: string
  data: T
}

export interface PageMeta {
  current_page: number
  per_page: number
  total: number
  last_page: number
}

export interface PageData<T> {
  items: T[]
  meta: PageMeta
}

export interface User {
  id: number
  name: string
  email: string
  role: 'admin' | 'teacher' | 'user'
  is_active: boolean
  permissions?: Permission[]
}

export interface Permission {
  id: number
  code: string
  name: string
  type: 'menu' | 'button'
  parent_code?: string
  menu_path?: string
  sort?: number
}

export interface QuestionBank {
  id: number
  name: string
  description?: string
  visibility: 'public' | 'private' | 'assigned'
  questions_count?: number
  wrong_questions_count?: number
  is_active: boolean
}

export interface ImportJob {
  id: number
  question_bank_id: number
  type: string
  status: string
  total_count: number
  success_count: number
  skipped_count: number
  report?: Array<Record<string, unknown>>
}

export interface QuestionOption {
  id: number
  content: string
  is_correct: boolean
  sort: number
}

export interface Question {
  id: number
  question_bank_id: number
  type: 'single' | 'multiple' | 'judge' | 'blank'
  content: string
  explanation?: string
  answers?: string[]
  options: QuestionOption[]
  is_active: boolean
}

export interface SchoolClass {
  id: number
  name: string
  description?: string
  join_code: string
  members_count?: number
  is_active: boolean
}

export interface Paper {
  id: number
  title: string
  description?: string
  question_bank_id?: number
  duration_minutes?: number
  attempt_limit?: number
  questions_count?: number
  questions?: Array<Question & { pivot?: { score?: string | number; sort?: number } }>
  is_active: boolean
}

export interface TaxonomyItem {
  id: number
  question_bank_id: number
  parent_id?: number
  name: string
  sort?: number
}

export interface AttemptItem {
  id: number
  question_id: number
  wrong_question_id?: number
  answer?: number[] | string[]
  is_correct?: boolean
  explanation_viewed: boolean
  question: Question
}

export interface QuizAttempt {
  id: number
  question_bank_id?: number
  mode: 'memorize' | 'wrong_memorize' | 'practice' | 'wrong_practice' | 'sequence' | 'random' | 'wrong_random' | 'paper'
  status: 'in_progress' | 'submitted'
  total_questions: number
  correct_count: number
  score: string
  current_index: number
  expires_at?: string
  items: AttemptItem[]
}

export interface QuizAttemptHistory {
  id: number
  mode: QuizAttempt['mode']
  status: QuizAttempt['status']
  total_questions: number
  correct_count: number
  score: string
  started_at?: string
  submitted_at?: string
}

export interface WrongQuestion {
  id: number
  question_id: number
  wrong_count: number
  consecutive_correct_count: number
  last_wrong_at?: string
  question: Question
}

export interface ReportFilters {
  [key: string]: unknown
  date_from?: string
  date_to?: string
  class_id?: number
  user_id?: number
  question_bank_id?: number
  category_id?: number
  tag_id?: number
  type?: Question['type']
  mode?: string
  page?: number
  per_page?: number
}

export interface ReportOverview {
  students: number
  active_students: number
  attempts: number
  answered_count: number
  accuracy: number
  completion_rate: number
  avg_duration_seconds: number
  wrong_questions: number
  mastered_wrong_questions: number
}

export interface ReportTrend {
  day: string
  attempts: number
  active_students: number
  answered_count: number
  accuracy: number
  avg_duration_seconds: number
  wrong_added: number
}

export interface ReportDimension {
  id: number | string | null
  name: string
  bank_name?: string
  answered_count: number
  correct_count: number
  wrong_count: number
  accuracy: number
  avg_duration_seconds: number
}

export interface ReportStudent {
  id: number
  name: string
  email: string
  attempts: number
  answered_count: number
  correct_count: number
  accuracy: number
  avg_duration_seconds: number
  wrong_questions: number
  last_answered_at?: string
}

export interface ReportQuestionError {
  id: number
  content: string
  type: Question['type']
  bank_name: string
  category_name: string
  attempts: number
  wrong_count: number
  wrong_rate: number
  last_wrong_at?: string
}

export interface ReportMastery {
  banks: ReportDimension[]
  categories: ReportDimension[]
  types: ReportDimension[]
  tags: ReportDimension[]
}

export interface ReportInsights {
  students: ReportStudent[]
  questions: ReportQuestionError[]
  categories: ReportDimension[]
}

export interface ReportOptions {
  banks: Array<Pick<QuestionBank, 'id' | 'name'>>
  classes: Array<Pick<SchoolClass, 'id' | 'name'>>
  students: Array<Pick<User, 'id' | 'name' | 'email'>>
  categories: TaxonomyItem[]
  tags: TaxonomyItem[]
}

export interface ReportStudentDetail {
  student: Pick<User, 'id' | 'name' | 'email'>
  overview: ReportOverview
  banks: ReportDimension[]
  categories: ReportDimension[]
  types: ReportDimension[]
  question_errors: ReportQuestionError[]
}

export interface ReportBankDetail {
  overview: ReportOverview
  categories: ReportDimension[]
  tags: ReportDimension[]
  types: ReportDimension[]
  question_errors: ReportQuestionError[]
}

export interface DashboardData {
  stats: {
    banks: number
    questions: number
    today_answered: number
    accuracy: number
    users?: number | null
    wrong_questions: number
  }
  trend: Array<{ day: string; answered_count: number }>
  class_ranking: Array<{ id: number; name: string; members_count: number; attempts: number }>
  recent_attempts: Array<{
    id: number
    user_name: string
    bank_name: string
    mode: string
    total_questions: number
    correct_count: number
    started_at: string
  }>
}
