import { request, uploadFile } from '@/utils/http'
import type { PageDTO } from './types'

export type SettlementStatusValue = 'NONE' | 'SETTLED' | 'UNSETTLED' | 'PARTIAL'
export type TaskStatusValue = 'COMPLETED' | 'INCOMPLETE'
export type CollaborationFileType = 'GOODS_IMAGE' | 'ATTACHMENT'

export interface SettlementStatusDTO {
  status: SettlementStatusValue
  label: string
}

export interface CollaborationRecordQuery {
  pageNum?: number
  pageSize?: number
  brand?: string
  goods?: string
  cooperationPlatform?: string
  settlementStatus?: SettlementStatusValue
  taskStatus?: TaskStatusValue
}

export interface CollaborationTaskCommand {
  releaseDate?: string
}

export interface CollaborationExpenditureCommand {
  spendDate?: string
  amount?: number
  purpose?: string
}

export interface CollaborationSettlementCommand {
  settleDate?: string
  method?: string
  income?: number
  purpose?: string
}

export interface CollaborationFileCommand {
  fileType: CollaborationFileType
  url: string
  fileName?: string
  newFileName?: string
  originalFilename?: string
}

export interface AddCollaborationRecordCommand {
  brand: string
  goods: string
  cooperationPlatform?: string
  imageReturnNum: number
  retainedMethod?: string
  cooperatedMethod?: string
  purchaseMethod?: string
  purchasePrice?: number
  purchaseDate?: string
  purchasePlatform?: string
  deadline?: string
  remuneration?: number
  completeDate?: string
  requirements?: string
  remark?: string
  tasks: CollaborationTaskCommand[]
  expenditures: CollaborationExpenditureCommand[]
  settlements: CollaborationSettlementCommand[]
  files: CollaborationFileCommand[]
}

export interface UpdateCollaborationRecordCommand extends AddCollaborationRecordCommand {
  recordId: number
}

export interface CollaborationRecordDTO extends AddCollaborationRecordCommand {
  recordId: number
  tasksNum: number
  completedTasksNum: number
  purchaseSettlementStatus: SettlementStatusDTO
  deliverySettlementStatus: SettlementStatusDTO
  remunerationSettlementStatus: SettlementStatusDTO
  createTime: string
}

export interface CollaborationRecordDetailDTO extends CollaborationRecordDTO {
  tasks: Array<CollaborationTaskCommand & { taskId?: number; sortOrder?: number }>
  expenditures: Array<CollaborationExpenditureCommand & { expenditureId?: number }>
  settlements: Array<CollaborationSettlementCommand & { settlementId?: number }>
  files: Array<CollaborationFileCommand & { fileId?: number; sortOrder?: number }>
}

export interface CollaborationOptionDTO {
  type: string
  label: string
  values: string[]
}

export interface CollaborationMonthlyStatisticsDTO {
  month: number
  purchasePrice: number
  expenditureAmount: number
  settledRemuneration: number
  settledTotal: number
}

export interface UploadFileDTO {
  url: string
  fileName: string
  newFileName: string
  originalFilename: string
}

export function getCollaborationRecordListApi(params: CollaborationRecordQuery) {
  return request<PageDTO<CollaborationRecordDTO>>({
    method: 'GET',
    url: '/app/collaboration/record/list',
    params
  })
}

export function getCollaborationRecordInfoApi(recordId: number) {
  return request<CollaborationRecordDetailDTO>({
    method: 'GET',
    url: `/app/collaboration/record/${recordId}`
  })
}

export function addCollaborationRecordApi(data: AddCollaborationRecordCommand) {
  return request<void>({ method: 'POST', url: '/app/collaboration/record', data })
}

export function updateCollaborationRecordApi(data: UpdateCollaborationRecordCommand) {
  return request<void>({ method: 'PUT', url: '/app/collaboration/record', data })
}

export function deleteCollaborationRecordApi(recordId: number) {
  return request<void>({
    method: 'DELETE',
    url: '/app/collaboration/record',
    params: { ids: recordId }
  })
}

export function getCollaborationOptionsApi() {
  return request<CollaborationOptionDTO[]>({
    method: 'GET',
    url: '/app/collaboration/record/options'
  })
}

export function getCollaborationMonthlyStatisticsApi(year: number) {
  return request<CollaborationMonthlyStatisticsDTO[]>({
    method: 'GET',
    url: '/app/collaboration/record/monthly-statistics',
    params: { year }
  })
}

export function uploadCollaborationFileApi(filePath: string, fileName?: string) {
  return uploadFile<UploadFileDTO>({
    url: '/file/upload',
    filePath,
    fileName
  })
}

