import { ElectronAPI } from '@electron-toolkit/preload'

type ModelListResponse =
  | {
      ok: true
      models: string[]
    }
  | {
      ok: false
      message: string
    }

type TestModelConnectionResponse =
  | {
      ok: true
      message: string
    }
  | {
      ok: false
      message: string
    }

type ModelConfig = {
  provider: string
  baseUrl: string
  apiKey: string
  model: string
  temperature: string
  maxTokens: string
  systemPrompt: string
}

type ClassType = string

type ClassTypeConfig = {
  id: string
  label: string
  courseContent: string
}

type ClassProfile = {
  id: string
  name: string
  type: ClassType
  teacherNames: string[]
  familyPhoto?: string
}

type TemplateType = 'portrait' | 'landscape'
type TemplateSemester = 'first' | 'second'

type TemplatePlaceholder = {
  name: string
  kind: 'text' | 'image'
}

type TemplateItem = {
  id: string
  name: string
  grade: string
  semester: TemplateSemester
  type: TemplateType
  originalFileName: string
  filePath: string
  fileExtension: string
  placeholders: TemplatePlaceholder[]
  createdAt: string
  updatedAt: string
}

type ReportFormat = 'word' | 'ppt' | 'pdf' | 'excel'

type ReportItem = {
  id: string
  title: string
  classId: string
  className: string
  studentId: string
  studentName: string
  format: ReportFormat
  originalFileName: string
  filePath: string
  fileExtension: string
  templateId: string
  createdAt: string
  updatedAt: string
}

type ReportGenerationProgress = {
  batchId: string
  status:
    | 'started'
    | 'student-started'
    | 'student-finished'
    | 'student-failed'
    | 'paused'
    | 'stopped'
    | 'finished'
  classId: string
  className: string
  studentId?: string
  studentName?: string
  current: number
  total: number
  report?: ReportItem
  error?: string
}

type ReportDownloadProgress = {
  batchId: string
  status: 'started' | 'file-started' | 'file-finished' | 'writing' | 'finished' | 'failed'
  classId: string
  className: string
  reportId?: string
  reportTitle?: string
  current: number
  total: number
  filePath?: string
  error?: string
}

type CommentGenerationProgress = {
  batchId: string
  status:
    | 'started'
    | 'student-started'
    | 'student-finished'
    | 'student-failed'
    | 'paused'
    | 'stopped'
    | 'finished'
  classId?: string
  className?: string
  studentId?: string
  studentName?: string
  current: number
  total: number
  profile?: ChildProfile
  error?: string
}

type ChildProfile = {
  id: string
  classId: string
  className: string
  name: string
  englishName: string
  gender: string
  birthday: string
  zodiac: string
  friends: string[]
  hobbies: string[]
  favoriteGames: string[]
  favoriteFoods: string[]
  traits: string
  comment: string
  commentGeneratedAt: string
  reportGenerated: boolean
  meImage: string
  workImage1: string
  workImage2: string
  importedAt: string
}

type SaveSettingsResponse =
  | {
      ok: true
    }
  | {
      ok: false
      message: string
    }

type LoadSettingsResponse =
  | {
      ok: true
      modelConfig: ModelConfig | null
      classTypeConfigs: ClassTypeConfig[]
    }
  | {
      ok: false
      message: string
    }

type ExportClassFolderResponse =
  | {
      ok: true
      zipPath: string
      childFolderCount: number
    }
  | {
      ok: false
      canceled?: boolean
      message: string
    }

type LoadStudentDataResponse =
  | {
      ok: true
      classes: ClassProfile[]
      profiles: ChildProfile[]
    }
  | {
      ok: false
      message: string
    }

type LoadStudentDataInput = {
  includeProfiles?: boolean
  includeImages?: boolean
  includeClassImages?: boolean
}

type ListStudentProfilesResponse =
  | {
      ok: true
      profiles: ChildProfile[]
      total: number
      page: number
      pageSize: number
    }
  | {
      ok: false
      message: string
    }

type StudentProfileMutationResponse =
  | {
      ok: true
      profile: ChildProfile
    }
  | {
      ok: false
      message: string
    }

type BasicMutationResponse =
  | {
      ok: true
    }
  | {
      ok: false
      message: string
    }

type LoadTemplatesResponse =
  | {
      ok: true
      templates: TemplateItem[]
    }
  | {
      ok: false
      message: string
    }

type SelectTemplateFileResponse =
  | {
      ok: true
      filePath: string
      fileName: string
    }
  | {
      ok: false
      canceled?: boolean
      message: string
    }

type SaveTemplateResponse =
  | {
      ok: true
      template: TemplateItem
    }
  | {
      ok: false
      message: string
    }

type ParseTemplatePlaceholdersResponse =
  | {
      ok: true
      placeholders: TemplatePlaceholder[]
    }
  | {
      ok: false
      message: string
    }

type LoadReportsResponse =
  | {
      ok: true
      reports: ReportItem[]
    }
  | {
      ok: false
      message: string
    }

type GenerateReportsResponse =
  | {
      ok: true
      reports: ReportItem[]
      skipped: Array<{ studentName: string; reason: string }>
      stopped?: boolean
    }
  | {
      ok: false
      message: string
    }

type GenerateStudentCommentsResponse =
  | {
      ok: true
      profiles: ChildProfile[]
      skipped: Array<{ studentName: string; reason: string }>
      stopped?: boolean
    }
  | {
      ok: false
      message: string
    }

type AddClassZodiacsResponse =
  | {
      ok: true
      profiles: ChildProfile[]
      updatedCount: number
      skipped: Array<{ studentName: string; reason: string }>
    }
  | {
      ok: false
      message: string
    }

type ReplaceClassProfilesResponse =
  | {
      ok: true
      profiles: ChildProfile[]
      insertedCount: number
      updatedCount: number
      skippedCount: number
    }
  | {
      ok: false
      message: string
    }

type DownloadReportResponse =
  | {
      ok: true
      filePath: string
    }
  | {
      ok: false
      canceled?: boolean
      message: string
    }

type DownloadClassReportsResponse =
  | {
      ok: true
      filePath: string
      reportCount: number
    }
  | {
      ok: false
      canceled?: boolean
      message: string
    }

type DeleteAllReportsResponse =
  | {
      ok: true
      deletedCount: number
    }
  | {
      ok: false
      message: string
    }

type AppAPI = {
  deleteClass: (classId: string) => Promise<BasicMutationResponse>
  deleteStudentProfile: (profileId: string) => Promise<BasicMutationResponse>
  exportEmptyClassFolder: (payload: {
    className: string
    classId: string
    childNames: string[]
  }) => Promise<ExportClassFolderResponse>
  loadStudentData: (payload?: LoadStudentDataInput) => Promise<LoadStudentDataResponse>
  listStudentProfiles: (payload: {
    page: number
    pageSize: number
    query?: string
    classId?: string
  }) => Promise<ListStudentProfilesResponse>
  getStudentProfile: (profileId: string) => Promise<StudentProfileMutationResponse>
  updateStudentProfile: (profile: ChildProfile) => Promise<StudentProfileMutationResponse>
  generateStudentComment: (payload: {
    profileId: string
  }) => Promise<StudentProfileMutationResponse>
  generateStudentComments: (payload: {
    classId?: string
    profileIds?: string[]
  }) => Promise<GenerateStudentCommentsResponse>
  addClassZodiacs: (payload: { classId: string }) => Promise<AddClassZodiacsResponse>
  pauseCommentGeneration: (batchId: string) => Promise<BasicMutationResponse>
  resumeCommentGeneration: (batchId: string) => Promise<BasicMutationResponse>
  stopCommentGeneration: (batchId: string) => Promise<BasicMutationResponse>
  listModels: (payload: { baseUrl: string; apiKey: string }) => Promise<ModelListResponse>
  testModelConnection: (modelConfig: ModelConfig) => Promise<TestModelConnectionResponse>
  replaceClassProfiles: (payload: {
    classItem: ClassProfile
    profiles: ChildProfile[]
  }) => Promise<ReplaceClassProfilesResponse>
  migrateLocalStudentData: (payload: {
    classes: ClassProfile[]
    profiles: ChildProfile[]
  }) => Promise<BasicMutationResponse>
  saveClasses: (classes: ClassProfile[]) => Promise<BasicMutationResponse>
  loadSettings: () => Promise<LoadSettingsResponse>
  saveSettings: (modelConfig: ModelConfig) => Promise<SaveSettingsResponse>
  saveClassTypeConfigs: (classTypeConfigs: ClassTypeConfig[]) => Promise<SaveSettingsResponse>
  loadTemplates: () => Promise<LoadTemplatesResponse>
  selectTemplateFile: () => Promise<SelectTemplateFileResponse>
  parseTemplatePlaceholders: (filePath: string) => Promise<ParseTemplatePlaceholdersResponse>
  getDroppedFilePath: (file: File) => string
  saveTemplate: (template: {
    id?: string
    name: string
    grade: string
    semester: TemplateSemester
    type: TemplateType
    sourceFilePath: string
  }) => Promise<SaveTemplateResponse>
  openTemplate: (filePath: string) => Promise<BasicMutationResponse>
  deleteTemplate: (id: string) => Promise<BasicMutationResponse>
  loadReports: () => Promise<LoadReportsResponse>
  generateReports: (payload: {
    templateId: string
    classId: string
    studentIds?: string[]
  }) => Promise<GenerateReportsResponse>
  pauseReportGeneration: (batchId: string) => Promise<BasicMutationResponse>
  resumeReportGeneration: (batchId: string) => Promise<BasicMutationResponse>
  stopReportGeneration: (batchId: string) => Promise<BasicMutationResponse>
  openReport: (filePath: string) => Promise<BasicMutationResponse>
  downloadReport: (id: string) => Promise<DownloadReportResponse>
  downloadClassReports: (payload: { classId: string }) => Promise<DownloadClassReportsResponse>
  onReportDownloadProgress: (callback: (progress: ReportDownloadProgress) => void) => () => void
  onReportGenerationProgress: (callback: (progress: ReportGenerationProgress) => void) => () => void
  onCommentGenerationProgress: (
    callback: (progress: CommentGenerationProgress) => void
  ) => () => void
  deleteReport: (id: string) => Promise<BasicMutationResponse>
  deleteAllReports: () => Promise<DeleteAllReportsResponse>
}

declare global {
  interface Window {
    electron: ElectronAPI
    api: AppAPI
  }
}
