import { http } from "@/utils/http";

export type SettlementStatusValue =
  | "NONE"
  | "SETTLED"
  | "UNSETTLED"
  | "PARTIAL";
export type CollaborationFileType = "GOODS_IMAGE" | "ATTACHMENT";

export interface SettlementStatusDTO {
  status: SettlementStatusValue;
  label: string;
}

export interface CollaborationRecordListCommand extends BasePageQuery {
  brand?: string;
  goods?: string;
  cooperationPlatform?: string;
  creatorId?: number;
  purchaseBeginTime?: string;
  purchaseEndTime?: string;
}

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 CollaborationRecordPageResponse {
  recordId: number;
  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;
  creatorId?: number;
  creatorName?: string;
  tasksNum: number;
  completedTasksNum: number;
  purchaseSettlementStatus: SettlementStatusDTO;
  deliverySettlementStatus: SettlementStatusDTO;
  remunerationSettlementStatus: SettlementStatusDTO;
  createTime: string;
}

export interface CollaborationRecordDetailResponse
  extends CollaborationRecordPageResponse {
  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 CollaborationOptionResponse {
  type: string;
  label: string;
  values: string[];
}

export interface CollaborationMonthlyStatisticsResponse {
  month: number;
  purchasePrice: number;
  expenditureAmount: number;
  settledRemuneration: number;
  settledTotal: number;
}

export interface UploadResponse {
  url: string;
  fileName: string;
  newFileName: string;
  originalFilename: string;
}

export const getCollaborationRecordListApi = (
  params: CollaborationRecordListCommand
) => {
  return http.request<ResponseData<PageDTO<CollaborationRecordPageResponse>>>(
    "get",
    "/collaboration/record/list",
    { params }
  );
};

export const getCollaborationRecordInfoApi = (recordId: number) => {
  return http.request<ResponseData<CollaborationRecordDetailResponse>>(
    "get",
    `/collaboration/record/${recordId}`
  );
};

export const addCollaborationRecordApi = (
  data: AddCollaborationRecordCommand
) => {
  return http.request<ResponseData<void>>("post", "/collaboration/record", {
    data
  });
};

export const updateCollaborationRecordApi = (
  data: UpdateCollaborationRecordCommand
) => {
  return http.request<ResponseData<void>>("put", "/collaboration/record", {
    data
  });
};

export const deleteCollaborationRecordApi = (data: Array<number>) => {
  return http.request<ResponseData<void>>("delete", "/collaboration/record", {
    params: {
      ids: data.toString()
    }
  });
};

export const getCollaborationOptionsApi = () => {
  return http.request<ResponseData<CollaborationOptionResponse[]>>(
    "get",
    "/collaboration/record/options"
  );
};

export interface CollaborationMonthlyStatisticsCommand {
  year: number;
  creatorId?: number;
}

export const getCollaborationMonthlyStatisticsApi = (
  params: CollaborationMonthlyStatisticsCommand
) => {
  return http.request<ResponseData<CollaborationMonthlyStatisticsResponse[]>>(
    "get",
    "/collaboration/record/monthly-statistics",
    {
      params
    }
  );
};

export const uploadCollaborationFileApi = (data: FormData) => {
  return http.request<ResponseData<UploadResponse>>(
    "post",
    "/file/upload",
    { data },
    {
      headers: {
        "Content-Type": "multipart/form-data"
      }
    }
  );
};
