export interface QuestionItem {
  questionTitle: string
  maxScore: number
  referenceAnswer: string
  rubric: string
  referenceAnswerImage?: string
}

export interface GradeResult {
  score: number
  confidence: number
  deductions: string[]
  comment: string
  commit?: string
}

export interface ApiConfig {
  apiKey: string
  baseUrl: string
  model: string
}

export interface AppSettings {
  apiKey: string
  baseUrl: string
  model: string
  shortcut: string
  theme: string
  fontSize: number
  proxy: string
  timeout: number
  screenshotSavePath: string
}

export interface HistoryRecord {
  id: number
  question_id: number
  image_path: string
  student_answer: string
  ai_score: number
  teacher_score: number
  confidence: number
  created_at: string
  question_title?: string
  max_score?: number
  reference_answer?: string
  rubric?: string
  deductions?: string
  comment?: string
  result_json?: string
  image_data?: string
  reference_answer_image?: string
}

export interface TemplateItem {
  id: number
  name: string
  course: string
  questions: QuestionItem[]
  created_at: string
}

export interface ElectronAPI {
  window: {
    dragMove: (deltaX: number, deltaY: number) => Promise<void>
    dragEnd: (x: number, y: number) => Promise<void>
    resizeMain: (width: number) => Promise<void>
  }
  screenshot: {
    start: () => Promise<void>
    capture: (region: { x: number; y: number; width: number; height: number }) => Promise<string | null>
    cancel: () => Promise<void>
    getLastImage: () => Promise<string | null>
    onCompleted: (callback: (base64: string) => void) => () => void
  }
  ai: {
    grade: (data: { imageBase64: string; rubric: string; apiConfig: ApiConfig; maxScore?: number; referenceAnswer?: string; questionTitle?: string; referenceAnswerImage?: string }) => Promise<GradeResult>
  }
  settings: {
    get: () => Promise<AppSettings>
    set: (settings: Record<string, unknown>) => Promise<boolean>
    selectDirectory: () => Promise<string | null>
  }
  history: {
    list: () => Promise<HistoryRecord[]>
    delete: (id: number) => Promise<boolean>
    getById: (id: number) => Promise<HistoryRecord | null>
  }
  grading: {
    submit: (data: {
      questionId: number
      imagePath: string
      aiScore: number
      teacherScore: number
      confidence: number
      studentAnswer?: string
      questionTitle?: string
      maxScore?: number
      referenceAnswer?: string
      rubric?: string
      deductions?: string[]
      comment?: string
      resultJson?: string
      imageData?: string
      referenceAnswerImage?: string
    }) => Promise<boolean>,
    storeData: (data: any) => Promise<void>,
    getData: () => Promise<any>
  }
  rubric: {
    storeData: (data: any) => Promise<void>
    getData: () => Promise<any>
    exportJson: () => Promise<boolean>
    importJson: () => Promise<{ questions: QuestionItem[]; currentIndex: number } | null>
    getNextIndex: () => Promise<number>
    setNextIndex: (idx: number) => Promise<void>
    resetNextIndex: () => Promise<void>
    onUpdated: (callback: () => void) => () => void
  }
  template: {
    list: () => Promise<TemplateItem[]>
    save: (data: { name: string; course: string; questions?: QuestionItem[] }) => Promise<number | null>
    delete: (id: number) => Promise<boolean>
    apply: (id: number) => Promise<boolean>
    exportJson: () => Promise<boolean>
    importJson: () => Promise<{ count: number } | null>
  }
  dialog: {
    open: (type: string, data?: any) => Promise<void>
    close: () => void
    getPayload: (type: string) => Promise<any>
    openImage: () => Promise<{ base64: string; mime: string; filePath: string } | null>
  }
}

declare global {
  interface Window {
    electronAPI: ElectronAPI
  }
}
