import { del, get, http, post, type JavaApiResponse, unwrapJavaResponse } from '@/shared/api/http'

const JAVA_API_PREFIX = '/newApi/api'

export interface UploadedFileRef {
  fileKey: string
  originalFilename?: string
  relativePath?: string
}

export interface UploadFileVo {
  fileKey: string
  originalFilename: string
  localPath: string
  size: number
  relativePath?: string
}

export interface DedupeResultItem {
  resultId?: number
  sourceFilename: string
  outputFilename?: string
  success: boolean
  error?: string
  downloadUrl?: string
}

export interface DedupeRunVo {
  total: number
  successCount: number
  failedCount: number
  items: DedupeResultItem[]
}

export interface DedupeHistoryVo {
  items: DedupeResultItem[]
}

export interface DedupeRunRequest {
  files: UploadedFileRef[]
  selectedColumns: string[]
  keepIntegerIds: boolean
  keepUnderscoreIds: boolean
  keepIntegerMainIdsWhenNoSubIds: boolean
  archiveName?: string
}

export interface SplitArchiveEntry {
  filename: string
  rowCount?: number
}

export interface SplitResultItem {
  resultId?: number
  sourceFilename: string
  outputFilename?: string
  success: boolean
  error?: string
  rowCount?: number
  downloadUrl?: string
  entryCount?: number
  entries?: SplitArchiveEntry[]
}

export interface SplitRunVo {
  total: number
  successCount: number
  failedCount: number
  items: SplitResultItem[]
}

export interface SplitHistoryVo {
  items: SplitResultItem[]
}

export interface SplitRunRequest {
  files: UploadedFileRef[]
  selectedColumns: string[]
  splitMode: 'rows_per_file' | 'parts'
  rowsPerFile?: number
  parts?: number
  archiveName?: string
}

export interface ConvertResultItem {
  resultId?: number
  sourceFilename: string
  outputFilename?: string
  success: boolean
  error?: string
  downloadUrl?: string
}

export interface ConvertRunVo {
  total: number
  successCount: number
  failedCount: number
  items: ConvertResultItem[]
}

export interface ConvertHistoryVo {
  items: ConvertResultItem[]
}

export interface ConvertRunRequest {
  files: UploadedFileRef[]
  templateId: string
  archiveName?: string
}

export interface ConvertTemplateVo {
  id: string
  templateCode: string
  templateName: string
  outputFilename: string
  isDefault?: boolean
  builtIn?: boolean
}

export interface ConvertTemplateImportRequest {
  templateName: string
  templateContent: string
}

export interface ExcelInfoVo {
  headers: string[]
  totalRows: number
}

export function getExcelInfo(fileKey: string) {
  return unwrapJavaResponse(get<JavaApiResponse<ExcelInfoVo>>(`${JAVA_API_PREFIX}/files/excel-info`, {
    params: { fileKey },
  }))
}

export function runDedupe(request: DedupeRunRequest) {
  return unwrapJavaResponse(post<JavaApiResponse<DedupeRunVo>, DedupeRunRequest>(`${JAVA_API_PREFIX}/dedupe/run`, request))
}

export function getDedupeHistory() {
  return unwrapJavaResponse(get<JavaApiResponse<DedupeHistoryVo>>(`${JAVA_API_PREFIX}/dedupe/history`))
}

export function deleteDedupeHistory(resultId: number) {
  return unwrapJavaResponse(del<JavaApiResponse<null>>(`${JAVA_API_PREFIX}/dedupe/history/${resultId}`))
}

export function runSplit(request: SplitRunRequest) {
  return unwrapJavaResponse(post<JavaApiResponse<SplitRunVo>, SplitRunRequest>(`${JAVA_API_PREFIX}/split/run`, request))
}

export function getSplitHistory() {
  return unwrapJavaResponse(get<JavaApiResponse<SplitHistoryVo>>(`${JAVA_API_PREFIX}/split/history`))
}

export function deleteSplitHistory(resultId: number) {
  return unwrapJavaResponse(del<JavaApiResponse<null>>(`${JAVA_API_PREFIX}/split/history/${resultId}`))
}

export function getConvertTemplates() {
  return unwrapJavaResponse(get<JavaApiResponse<ConvertTemplateVo[]>>(`${JAVA_API_PREFIX}/convert/templates`))
}

export function importConvertTemplate(request: ConvertTemplateImportRequest) {
  return unwrapJavaResponse(
    post<JavaApiResponse<ConvertTemplateVo>, ConvertTemplateImportRequest>(`${JAVA_API_PREFIX}/convert/templates/import`, request),
  )
}

export function setDefaultConvertTemplate(templateCode: string) {
  return unwrapJavaResponse(post<JavaApiResponse<null>>(`${JAVA_API_PREFIX}/convert/templates/${templateCode}/default`))
}

export function deleteConvertTemplate(templateCode: string) {
  return unwrapJavaResponse(del<JavaApiResponse<null>>(`${JAVA_API_PREFIX}/convert/templates/${templateCode}`))
}

export function runConvert(request: ConvertRunRequest) {
  return unwrapJavaResponse(post<JavaApiResponse<ConvertRunVo>, ConvertRunRequest>(`${JAVA_API_PREFIX}/convert/run`, request))
}

export function getConvertHistory() {
  return unwrapJavaResponse(get<JavaApiResponse<ConvertHistoryVo>>(`${JAVA_API_PREFIX}/convert/history`))
}

export function deleteConvertHistory(resultId: number) {
  return unwrapJavaResponse(del<JavaApiResponse<null>>(`${JAVA_API_PREFIX}/convert/history/${resultId}`))
}

export function getJavaDownloadUrl(path: string) {
  return path.startsWith('http://') || path.startsWith('https://') ? path : `${JAVA_API_PREFIX}${path}`
}

export { JAVA_API_PREFIX, http }
