export interface TokenResponse {
  access_token: string
  refresh_token: string
  token_type: string
}

export interface User {
  id: string
  username: string
  is_superuser: boolean
  created_at: string
}

export interface PublicSettings {
  registration_enabled: boolean
}

export interface AppFeatures {
  ai_review_enabled: boolean
}

export interface SystemSettings {
  registration_enabled: boolean
  ai_review_enabled: boolean
  ai_provider: AIProvider
  ollama_base_url: string | null
  ollama_model: string | null
  openai_base_url: string | null
  openai_model: string | null
  openai_api_key_set: boolean
  ocr_service_url: string | null
  updated_at: string
}

export interface AdminUser {
  id: string
  username: string
  is_superuser: boolean
  created_at: string
}

export type WrongQuestionCategory = 'regular' | 'olympiad'

export type AIProvider = 'ollama' | 'openai'

export type SchoolLevel = 'junior_high' | 'senior_high'

export interface Student {
  id: string
  name: string
  school_level: SchoolLevel
  grade: string | null
  class_name: string | null
  school_name: string | null
  has_avatar: boolean
  created_at: string
}

export interface BackupInfo {
  filename: string
  size_bytes: number
  created_at: string
}

export interface Subject {
  id: number
  name: string
}

export interface Score {
  id: string
  subject_id: number
  subject_name?: string
  total_score: number
  obtained_score: number
  ratio: number
  review_statuses?: ReviewStatus[]
}

export type ReviewStatus = 'careless' | 'unknown' | 'nervous' | 'normal'

export const REVIEW_STATUS_LABELS: Record<ReviewStatus, string> = {
  careless: '粗心',
  unknown: '不会',
  nervous: '紧张',
  normal: '正常发挥',
}

export const REVIEW_STATUS_OPTIONS = (
  Object.entries(REVIEW_STATUS_LABELS) as [ReviewStatus, string][]
).map(([value, label]) => ({ value, label }))

export const PROBLEM_REVIEW_STATUSES: ReviewStatus[] = ['careless', 'unknown', 'nervous']

export function hasReviewProblem(statuses: ReviewStatus[] | undefined): boolean {
  return (statuses || []).some((s) => PROBLEM_REVIEW_STATUSES.includes(s))
}

export function formatReviewStatuses(statuses: ReviewStatus[] | undefined): string {
  if (!statuses?.length) return '-'
  return statuses.map((s) => REVIEW_STATUS_LABELS[s]).join('、')
}

export type ExamType = 'weekly' | 'monthly' | 'final'

export interface Exam {
  id: string
  exam_type: ExamType
  exam_date: string
  title: string | null
  created_at: string
  scores: Score[]
}

export interface ScoreInput {
  subject_id: number
  total_score: number
  obtained_score: number
  review_statuses?: ReviewStatus[]
}

export interface TrendPoint {
  exam_id: string
  exam_type: ExamType
  exam_date: string
  title: string | null
  ratio: number
  ratio_percent: number
  delta: number | null
  delta_percent: number | null
  is_volatile: boolean
  direction: 'up' | 'down' | 'flat' | null
}

export interface TrendResponse {
  subject_id: number
  subject_name: string
  threshold: number
  points: TrendPoint[]
}

export type WrongQuestionStatus = 'pending' | 'ocr_done' | 'solved' | 'failed'

export interface WrongQuestion {
  id: string
  student_id: string
  subject_id: number
  subject_name?: string
  category: WrongQuestionCategory
  image_path: string
  ocr_raw_text: string | null
  question_text: string | null
  solution_approach: string | null
  solution_text: string | null
  mark_regions: MarkRegion[] | null
  has_annotated_image: boolean
  has_cropped_image: boolean
  error_message: string | null
  status: WrongQuestionStatus
  created_at: string
}

export interface MarkRegion {
  line_id: number
  text: string
  bbox: number[]
  type: string
  label: string
}

export const EXAM_TYPE_LABELS: Record<ExamType, string> = {
  weekly: '周考',
  monthly: '月考',
  final: '期末',
}

export const STATUS_LABELS: Record<WrongQuestionStatus, string> = {
  pending: '处理中',
  ocr_done: '已识别',
  solved: '已生成解法',
  failed: '失败',
}

export type CompositionStatus = 'pending' | 'generating' | 'done' | 'failed'
export type CompositionInputMode = 'manual' | 'ocr'

export interface Composition {
  id: string
  student_id: string
  topic: string
  input_mode: CompositionInputMode
  writing_plan: string | null
  sample_essay: string | null
  error_message: string | null
  status: CompositionStatus
  created_at: string
  updated_at: string
}

export const COMPOSITION_STATUS_LABELS: Record<CompositionStatus, string> = {
  pending: '等待生成',
  generating: '生成中',
  done: '已完成',
  failed: '失败',
}
