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

const JAVA_API_PREFIX = '/newApi/api'

function getCurrentUserId() {
  const raw = typeof window === 'undefined' ? '' : window.localStorage.getItem('uid') || ''
  const value = Number(raw)
  if (!Number.isFinite(value) || value <= 0) {
    throw new Error('未获取到用户ID')
  }
  return value
}

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
  user_id: number
}

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
  user_id: number
}

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
  user_id: number
}

export interface DeleteBrandPreviewRow {
  rowIndex: number
  country: string
  asin: string
  status: string
}

export interface DeleteBrandCountryAsinItem {
  rowIndex: number
  asin: string
  status: string
}

export interface DeleteBrandCountryGroup {
  country: string
  asinCount: number
  items: DeleteBrandCountryAsinItem[]
}

export interface DeleteBrandCountryResultItem {
  asin: string
  status?: string
}

export interface DeleteBrandProcessedCountry {
  country: string
  items: DeleteBrandCountryResultItem[]
}

export interface DeleteBrandResultFile {
  fileKey?: string
  sourceFilename: string
  fileIndex?: number
  fileTotal?: number
  chunkIndex?: number
  chunkTotal?: number
  processedRows?: number
  totalRows?: number
  currentCountry?: string
  currentAsin?: string
  countries: DeleteBrandProcessedCountry[]
}

export interface DeleteBrandSubmitResultRequest {
  submissionId?: string
  files: DeleteBrandResultFile[]
}

export interface DeleteBrandLineProgressInfo {
  file_index?: number
  file_total?: number
  file_name?: string
  current_line?: number
  total_lines?: number
  current_country?: string
  current_asin?: string
  finished_files?: number
  phase?: string
}

export interface DeleteBrandLineProgress {
  has_progress: boolean
  info?: DeleteBrandLineProgressInfo
}

export interface DeleteBrandTaskItem {
  id: number
  taskNo?: string
  status?: string
  sourceFileCount?: number
  successFileCount?: number
  failedFileCount?: number
  errorMessage?: string
  createdAt?: string
  updatedAt?: string
  finishedAt?: string
  downloadUrl?: string
  downloadFilename?: string
}

export interface DeleteBrandTaskDetailVo {
  task: DeleteBrandTaskItem
  line_progress: DeleteBrandLineProgress
  items?: DeleteBrandResultItem[]
}

export interface DeleteBrandTaskBatchVo {
  items: DeleteBrandTaskDetailVo[]
  missingTaskIds: number[]
}

export type DeleteBrandMatchStatus = 'MATCHED' | 'PENDING' | 'CONFLICT' | 'INDEX_STALE'

export interface DeleteBrandResultItem {
  resultId?: number
  fileKey?: string
  sourceFilename: string
  shopName?: string
  matched?: boolean
  matchStatus?: DeleteBrandMatchStatus
  matchMessage?: string
  shopId?: string
  platform?: string
  openStoreUrl?: string
  outputFilename?: string
  downloadUrl?: string
  taskId?: number
  countryCount?: number
  countries?: DeleteBrandCountryGroup[]
  totalRows?: number
  truncated?: boolean
  previewRows?: DeleteBrandPreviewRow[]
  success: boolean
  error?: string
  taskStatus?: string
}

export interface DeleteBrandRunVo {
  total: number
  successCount: number
  failedCount: number
  items: DeleteBrandResultItem[]
}

export interface DeleteBrandHistoryVo {
  items: DeleteBrandResultItem[]
}

export interface DeleteBrandRunRequest {
  files: UploadedFileRef[]
  user_id: number
}

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: Omit<DedupeRunRequest, 'user_id'> | DedupeRunRequest) {
  return unwrapJavaResponse(post<JavaApiResponse<DedupeRunVo>, DedupeRunRequest>(`${JAVA_API_PREFIX}/dedupe/run`, {
    ...request,
    user_id: getCurrentUserId(),
  }))
}

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

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

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

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

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

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: Omit<ConvertRunRequest, 'user_id'> | ConvertRunRequest) {
  return unwrapJavaResponse(post<JavaApiResponse<ConvertRunVo>, ConvertRunRequest>(`${JAVA_API_PREFIX}/convert/run`, {
    ...request,
    user_id: getCurrentUserId(),
  }))
}

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

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

export function runDeleteBrand(request: Omit<DeleteBrandRunRequest, 'user_id'> | DeleteBrandRunRequest) {
  return unwrapJavaResponse(post<JavaApiResponse<DeleteBrandRunVo>, DeleteBrandRunRequest>(`${JAVA_API_PREFIX}/delete-brand/run`, {
    ...request,
    user_id: getCurrentUserId(),
  }))
}

export function getDeleteBrandHistory() {
  return unwrapJavaResponse(get<JavaApiResponse<DeleteBrandHistoryVo>>(`${JAVA_API_PREFIX}/delete-brand/history`, {
    params: { user_id: getCurrentUserId() },
  }))
}

export function deleteDeleteBrandHistory(resultId: number) {
  return unwrapJavaResponse(del<JavaApiResponse<null>>(`${JAVA_API_PREFIX}/delete-brand/history/${resultId}`, {
    params: { user_id: getCurrentUserId() },
  }))
}

export function getDeleteBrandTaskDetail(taskId: number) {
  return unwrapJavaResponse(get<JavaApiResponse<DeleteBrandTaskDetailVo>>(`${JAVA_API_PREFIX}/delete-brand/tasks/${taskId}`))
}

export function getDeleteBrandTaskDetails(taskIds: number[]) {
  return unwrapJavaResponse(post<JavaApiResponse<DeleteBrandTaskBatchVo>, { taskIds: number[] }>(`${JAVA_API_PREFIX}/delete-brand/tasks/batch`, { taskIds }))
}

export function getDeleteBrandTaskDownloadUrl(taskId: number) {
  return getJavaDownloadUrl(`/delete-brand/tasks/${taskId}/download`)
}

export function submitDeleteBrandResult(taskId: number, request: DeleteBrandSubmitResultRequest) {
  return unwrapJavaResponse(post<JavaApiResponse<null>, DeleteBrandSubmitResultRequest>(`${JAVA_API_PREFIX}/delete-brand/tasks/${taskId}/result`, request))
}

export function getJavaDownloadUrl(path: string) {
  let raw = path.startsWith('http://') || path.startsWith('https://') ? path : `${JAVA_API_PREFIX}${path}`
  // 核心修复：pywebview 的 save_file_from_url 需要完整带 schema 的 URL
  if (!raw.startsWith('http://') && !raw.startsWith('https://') && typeof window !== 'undefined') {
    raw = `${window.location.origin}${raw}`
  }
  const separator = raw.includes('?') ? '&' : '?'
  return `${raw}${separator}user_id=${encodeURIComponent(String(getCurrentUserId()))}`
}

export { JAVA_API_PREFIX, http }
