import { request } from '@/http/request'
import type { SimpleRect, TaskArtifact } from './type'

interface ImageMattingParams {
  image_url: string
  task_id: string
  compress?: boolean
}
export function imageMatting(data: ImageMattingParams) {
  return request.post<TaskArtifact>(
    '/v1/image/remove_image_background',
    data,
  )
}

interface ImageOCRParams {
  image_url: string
  task_id: string
}
// 图片 OCR
export function imageOCR(data: ImageOCRParams) {
  return request.post('/v1/image/ocr_normal', data)
}


interface RecognizeImageParams {
  image_url: string
  task_id: string
  width: number
  height: number
}
// 识别图片主体返回数据结构
interface RecognizedImageElement {
  element: string
  type: string
  x: number
  y: number
  w: number
  h: number
}

type RecognizeImageResponse = ReadonlyArray<RecognizedImageElement>
// 识别图片主体
export function recognizeImage(data: RecognizeImageParams) {
  return request.post<RecognizeImageResponse>(
    '/v1/image/read_image',
    data,
  )
}


type ImageOCRTextUpdateItem = SimpleRect & {
  source: string
  target: string
  // @todo 临时兼容后端接口
  w: number
  h: number
}
interface UpdateImageOCRTextParams {
  texts: ImageOCRTextUpdateItem[]
  image_url: string
  task_id: string
  compress?: boolean
}
export function updateImageOCRText(data: UpdateImageOCRTextParams) {
  return request.post<TaskArtifact>(
    '/v1/image/gen_image_with_text_by_area',
    data,
  )
}


interface SegmentLayerParams {
  image_url: string
  task_id: string
  width: number
  height: number
}
// 分割图层 - 阶段 1: 提交任务
export function segmentLayer(data: SegmentLayerParams) {
  return request.post<string>('/v1/image/segment_layer', data)
}


interface SegmentLayerItem {
  line_text: string
  line_rect: SimpleRect
  image: TaskArtifact
}
interface SegmentLayerResponse {
  text_layer: SegmentLayerItem[]
  background: TaskArtifact
}
interface SegmentLayerTask {
  task_id: string
  status: 'PROCESSING' | 'FINISHED' | 'FAILED' | 'TIMEOUT'
  result?: SegmentLayerResponse
  error_message?: string
}
// 分割图层 - 阶段 2: 轮询结果
export function getSegmentLayerResult(execute_id: string) {
  return request.get<SegmentLayerTask>('/v1/image/segment_layer', {
    params: { execute_id },
  })
}


interface SaveDomiBoardContentData {
  data: Record<string, unknown>
  task_id: string
}
export function saveDomiBoardContent(data: SaveDomiBoardContentData) {
  return request.post('/v1/image/canvas', data)
}


export function getDomiBoardContent(task_id: string) {
  return request.get<Record<string, unknown>>('/v1/image/canvas', {
    params: { task_id },
  })
}


export function getShortUrl(task_id: string, file_path: string) {
  return request.post('/v1/oss/get_short_url', {
    task_id,
    file_paths: [file_path],
  })
}


interface RedrawImageParams {
  image_url: string
  prompt: string
  task_id: string
  compress?: boolean
}

export function redrawImage(data: RedrawImageParams) {
  return request.post<TaskArtifact>(
    '/v1/image/gen_image_by_area',
    data,
  )
}



interface CreateAndSyncImageResponse {
  upload_record_id: string
  sandbox_path: string
  url: string
}

export function createAndSyncImage(data: {
  task_id: string
  file_name: string
  file_type: string
  file_byte_size: number
  file_url: string
}) {
  return request.post<CreateAndSyncImageResponse>(
    '/v1/super_agent/file_upload_record/create_and_sync',
    data,
  )
}



// 创建文件上传记录
export function createFileUploadRecord(payload: {
  file_url: string
  file_type: string
  file_name: string
  file_byte_size: number
  conversation_id: string
}) {
  return request.post(
    '/v1/super_agent/file_upload_record/create',
    payload,
  )
}