import { requestDeleteJson, requestGetJson, requestPostJson } from '@/shared/api/http'

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 BrandExpandFolderItem {
  absolutePath: string
  relativePath: string
}

export interface BrandExpandFolderResponse {
  success: boolean
  items?: BrandExpandFolderItem[]
  error?: string
}

export interface BrandTaskResultPaths {
  zip_url?: string
}

export interface BrandTaskItem {
  id: number | string
  status?: 'pending' | 'running' | 'success' | 'failed' | 'cancelled' | string
  desc?: string
  file_paths?: string[]
  created_at?: string
  progress_total?: number
  progress_current?: number
  result_paths?: BrandTaskResultPaths
}

export interface BrandTaskListResponse {
  success: boolean
  items?: BrandTaskItem[]
  error?: string
}

export interface BrandTaskDetailResponse {
  success: boolean
  task?: BrandTaskItem
  error?: string
}

export interface BrandTaskMutationResponse {
  success: boolean
  task_id?: string
  error?: string
}

const API_PREFIX = ''

export function getBrandTaskEventsUrl(taskId: string | number) {
  return `${API_PREFIX}/api/brand/tasks/${taskId}/events?user_id=${encodeURIComponent(String(getCurrentUserId()))}`
}

export function getBrandTaskDownloadUrl(taskId: string | number) {
  return `${API_PREFIX}/api/brand/tasks/${taskId}/download?user_id=${encodeURIComponent(String(getCurrentUserId()))}`
}

export function getBrandTemplateXlsxUrl() {
  return `${API_PREFIX}/static/品牌文档格式_模板.xlsx`
}

export function getBrandTemplateZipUrl() {
  return `${API_PREFIX}/static/模板2-以文件夹方式上传.zip`
}

export function expandBrandFolder(folder: string) {
  return requestPostJson<BrandExpandFolderResponse>(`${API_PREFIX}/api/brand/expand-folder`, { folder })
}

export function expandBrandFolderRecursive(folder: string) {
  return requestPostJson<BrandExpandFolderResponse>(`${API_PREFIX}/api/brand/expand-folder-recursive`, { folder })
}

export function runBrandNow(paths: string[], strategy: string) {
  return requestPostJson<BrandTaskMutationResponse>(`${API_PREFIX}/api/brand/run`, { paths, strategy })
}

export function createBrandTask(paths: string[], strategy: string) {
  return requestPostJson<BrandTaskMutationResponse>(`${API_PREFIX}/api/brand/tasks?userId=${encodeURIComponent(String(getCurrentUserId()))}`, { paths, strategy })
}

export function getBrandTasks() {
  return requestGetJson<BrandTaskListResponse>(`${API_PREFIX}/api/brand/tasks?userId=${encodeURIComponent(String(getCurrentUserId()))}`)
}

export function getBrandTask(taskId: string | number) {
  return requestGetJson<BrandTaskDetailResponse>(`${API_PREFIX}/api/brand/tasks/${taskId}?userId=${encodeURIComponent(String(getCurrentUserId()))}`)
}

export function cancelBrandTask(taskId: string | number) {
  return requestPostJson<BrandTaskMutationResponse>(`${API_PREFIX}/api/brand/tasks/${taskId}/cancel?userId=${encodeURIComponent(String(getCurrentUserId()))}`)
}

export function deleteBrandTask(taskId: string | number) {
  return requestDeleteJson<BrandTaskMutationResponse>(`${API_PREFIX}/api/brand/tasks/${taskId}?userId=${encodeURIComponent(String(getCurrentUserId()))}`)
}

export function createBrandTaskEvents(taskId: string | number) {
  return new EventSource(getBrandTaskEventsUrl(taskId))
}
