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 = 'cheap' | 'noble'

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

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

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

type TemplateFieldMapping = Record<string, string>

type TemplateItem = {
  id: string
  name: string
  grade: string
  semester: TemplateSemester
  type: TemplateType
  originalFileName: string
  filePath: string
  fileExtension: string
  placeholders: TemplatePlaceholder[]
  fieldMappings: TemplateFieldMapping
  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 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
  importedAt: string
}

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

type LoadSettingsResponse =
  | {
      ok: true
      modelConfig: ModelConfig | null
    }
  | {
      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 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 }>
    }
  | {
      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: () => Promise<LoadStudentDataResponse>
  listStudentProfiles: (payload: {
    page: number
    pageSize: number
    query?: string
    classId?: string
  }) => Promise<ListStudentProfilesResponse>
  updateStudentProfile: (profile: ChildProfile) => Promise<StudentProfileMutationResponse>
  generateStudentComment: (payload: {
    profileId: string
  }) => Promise<StudentProfileMutationResponse>
  listModels: (payload: { baseUrl: string; apiKey: string }) => Promise<ModelListResponse>
  testModelConnection: (modelConfig: ModelConfig) => Promise<TestModelConnectionResponse>
  replaceClassProfiles: (payload: {
    classItem: ClassProfile
    profiles: ChildProfile[]
  }) => Promise<BasicMutationResponse>
  migrateLocalStudentData: (payload: {
    classes: ClassProfile[]
    profiles: ChildProfile[]
  }) => Promise<BasicMutationResponse>
  saveClasses: (classes: ClassProfile[]) => Promise<BasicMutationResponse>
  loadSettings: () => Promise<LoadSettingsResponse>
  saveSettings: (modelConfig: ModelConfig) => 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
    fieldMappings?: TemplateFieldMapping
  }) => 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>
  openReport: (filePath: string) => Promise<BasicMutationResponse>
  deleteReport: (id: string) => Promise<BasicMutationResponse>
}

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