import { request, type ApiResponse, type PagedResult } from '@/utils/request'

// ==================== 订单状态枚举 ====================

/** 订单状态枚举 */
export enum OrderStatus {
  Unpaid = 0,    // 待支付
  Paid = 1,      // 已支付
  Cancelled = 2  // 已取消
}

/** 订单状态标签映射 */
export const OrderStatusLabels: Record<number, string> = {
  [OrderStatus.Unpaid]: '待支付',
  [OrderStatus.Paid]: '已支付',
  [OrderStatus.Cancelled]: '已取消',
}

/** 发货状态枚举 */
export enum ShippingStatus {
  Unpaid = 0,    // 待支付
  Pending = 1,   // 待发货
  Shipped = 2,   // 已发货
  Received = 3,  // 已签收
  Cancelled = 4  // 已取消
}

/** 发货状态标签映射 */
export const ShippingStatusLabels: Record<number, string> = {
  [ShippingStatus.Unpaid]: '待支付',
  [ShippingStatus.Pending]: '待发货',
  [ShippingStatus.Shipped]: '已发货',
  [ShippingStatus.Received]: '已签收',
  [ShippingStatus.Cancelled]: '已取消',
}

/** 订单项状态枚举 */
export enum OrderItemStatus {
  Pending = 0,   // 待处理
  Recovered = 1, // 已回收
  Shipped = 2    // 已发货
}

/** 订单项状态标签映射 */
export const OrderItemStatusLabels: Record<number, string> = {
  [OrderItemStatus.Pending]: '待处理',
  [OrderItemStatus.Recovered]: '已回收',
  [OrderItemStatus.Shipped]: '已发货',
}

/** 支付方式枚举 */
export enum PayType {
  Unpaid = 0,    // 未支付
  WeChat = 1,    // 微信支付
  Alipay = 2     // 支付宝
}

/** 支付方式标签映射 */
export const PayTypeLabels: Record<number, string> = {
  [PayType.Unpaid]: '未支付',
  [PayType.WeChat]: '微信支付',
  [PayType.Alipay]: '支付宝',
}

// ==================== 订单列表相关类型定义 ====================

/** 订单列表查询参数 */
export interface OrderListRequest {
  page: number
  pageSize: number
  userId?: number
  mobile?: string
  orderNum?: string
  startDate?: string
  endDate?: string
  status?: number
  orderType?: number
}

/** 订单列表响应项 */
export interface OrderListResponse {
  id: number
  orderNum: string
  userId: number
  userNickname?: string
  userMobile?: string
  userUid?: string
  goodsId: number
  goodsTitle?: string
  goodsImgUrl?: string
  orderType: number
  orderTotal: number
  discount: number
  discountTotal: number
  weChatPayment: number
  balancePayment: number
  integralPayment: number
  scorePayment: number
  couponPayment?: number
  num: number
  prizeNum: number
  status: number
  statusName: string
  payType: number
  payTypeName: string
  createdAt: string
  payTime?: string
}

// ==================== 订单详情相关类型定义 ====================

/** 订单奖品项DTO */
export interface OrderPrizeItemDto {
  id: number
  status: number
  statusName: string
  recoveryNum?: string
  sendNum?: string
  luckNo: number
  createdAt: string
}

/** 订单奖品分组DTO */
export interface OrderPrizeGroupDto {
  prizeCode?: string
  title?: string
  imgUrl?: string
  price: number
  recoveryMoney: number
  count: number
  goodsType: number
  shangId: number
  items: OrderPrizeItemDto[]
}

/** 订单详情响应 */
export interface OrderDetailResponse extends OrderListResponse {
  prizeGroups: OrderPrizeGroupDto[]
}

// ==================== 订单导出相关类型定义 ====================

/** 订单导出请求 */
export interface OrderExportRequest {
  userId?: number
  mobile?: string
  orderNum?: string
  startDate?: string
  endDate?: string
  status?: number
  orderType?: number
}

// ==================== 发货订单相关类型定义 ====================

/** 发货订单列表查询参数 */
export interface ShippingOrderListRequest {
  page: number
  pageSize: number
  userId?: number
  mobile?: string
  sendNum?: string
  startDate?: string
  endDate?: string
  status?: number
}

/** 发货奖品DTO */
export interface ShippingPrizeDto {
  id: number
  prizeCode?: string
  title?: string
  imgUrl?: string
  price: number
  goodsType: number
}

/** 发货订单响应 */
export interface ShippingOrderResponse {
  id: number
  userId: number
  userNickname?: string
  userMobile?: string
  userUid?: string
  sendNum: string
  freight: number
  totalPrice: number
  status: number
  statusName: string
  count: number
  name?: string
  receiverMobile?: string
  address?: string
  message?: string
  courierNumber?: string
  courierName?: string
  courierCode?: string
  deliveryStatus: number
  deliveryStatusName: string
  createdAt: string
  payTime?: string
  sendTime?: string
  receiveTime?: string
  prizes: ShippingPrizeDto[]
}

/** 发货请求 */
export interface ShipOrderRequest {
  courierName: string
  courierNumber: string
  courierCode?: string
}

/** 发货订单导出请求 */
export interface ShippingExportRequest {
  userId?: number
  mobile?: string
  sendNum?: string
  startDate?: string
  endDate?: string
  status?: number
}

/** 发货订单统计响应 */
export interface ShippingStatsResponse {
  totalCount: number
  totalValue: number
  pageValue: number
}

// ==================== 回收订单相关类型定义 ====================

/** 回收奖品DTO */
export interface RecoveryPrizeDto {
  id: number
  prizeCode?: string
  title?: string
  imgUrl?: string
  price: number
  recoveryMoney: number
  goodsType: number
}

/** 回收订单响应 */
export interface RecoveryOrderResponse {
  id: number
  userId: number
  userNickname?: string
  userMobile?: string
  userUid?: string
  recoveryNum: string
  money: number
  count: number
  createdAt: string
  prizes: RecoveryPrizeDto[]
}

/** 回收订单导出请求 */
export interface RecoveryExportRequest {
  userId?: number
  mobile?: string
  startDate?: string
  endDate?: string
}

// ==================== 综合订单相关类型定义 ====================

/** 账号类型枚举 */
export enum AccountType {
  Normal = 0,    // 正常
  Promotion = 1, // 推广
  Test = 2       // 测试
}

/** 账号类型标签映射 */
export const AccountTypeLabels: Record<number, string> = {
  [AccountType.Normal]: '正常',
  [AccountType.Promotion]: '推广',
  [AccountType.Test]: '测试',
}

/** 账号状态枚举 */
export enum AccountStatus {
  Normal = 0,  // 正常
  Banned = 1   // 封号
}

/** 账号状态标签映射 */
export const AccountStatusLabels: Record<number, string> = {
  [AccountStatus.Normal]: '正常',
  [AccountStatus.Banned]: '封号',
}

/** 综合订单列表查询参数 */
export interface ComprehensiveOrderListRequest {
  page: number
  pageSize: number
  userId?: number
  mobile?: string
  orderNum?: string
  startDate?: string
  endDate?: string
  status?: number
  accountType?: number
  accountStatus?: number
}

/** 综合订单导出请求 */
export interface ComprehensiveOrderExportRequest {
  userId?: number
  mobile?: string
  orderNum?: string
  startDate?: string
  endDate?: string
  status?: number
  accountType?: number
  accountStatus?: number
}

// ==================== API 基础路径 ====================

const ORDER_BASE_URL = '/admin/business/orders'

// ==================== 购买订单 API ====================

/**
 * 获取订单列表
 * @param params 查询参数
 * @returns 分页订单列表
 */
export function getOrderList(params: OrderListRequest): Promise<ApiResponse<PagedResult<OrderListResponse>>> {
  return request({
    url: ORDER_BASE_URL,
    method: 'get',
    params
  })
}

/**
 * 获取订单详情
 * @param id 订单ID
 * @returns 订单详情
 */
export function getOrderDetail(id: number): Promise<ApiResponse<OrderDetailResponse>> {
  return request({
    url: `${ORDER_BASE_URL}/${id}`,
    method: 'get'
  })
}

/**
 * 导出订单
 * @param params 导出参数
 * @returns CSV文件Blob
 */
export function exportOrders(params: OrderExportRequest): Promise<Blob> {
  return request({
    url: `${ORDER_BASE_URL}/export`,
    method: 'get',
    params,
    responseType: 'blob'
  }) as unknown as Promise<Blob>
}

// ==================== 卡单订单 API ====================

/**
 * 获取卡单订单列表
 * @param params 查询参数
 * @returns 分页卡单订单列表
 */
export function getStuckOrders(params: OrderListRequest): Promise<ApiResponse<PagedResult<OrderListResponse>>> {
  return request({
    url: `${ORDER_BASE_URL}/stuck`,
    method: 'get',
    params
  })
}


// ==================== 发货订单 API ====================

/**
 * 获取发货订单列表
 * @param params 查询参数
 * @returns 分页发货订单列表
 */
export function getShippingOrders(params: ShippingOrderListRequest): Promise<ApiResponse<PagedResult<ShippingOrderResponse>>> {
  return request({
    url: `${ORDER_BASE_URL}/shipping`,
    method: 'get',
    params
  })
}

/**
 * 获取发货订单详情
 * @param id 发货记录ID
 * @returns 发货订单详情
 */
export function getShippingOrderDetail(id: number): Promise<ApiResponse<ShippingOrderResponse>> {
  return request({
    url: `${ORDER_BASE_URL}/shipping/${id}`,
    method: 'get'
  })
}

/**
 * 发货操作
 * @param id 发货记录ID
 * @param data 发货信息
 * @returns 操作结果
 */
export function shipOrder(id: number, data: ShipOrderRequest): Promise<ApiResponse<string>> {
  return request({
    url: `${ORDER_BASE_URL}/${id}/ship`,
    method: 'put',
    data
  })
}

/**
 * 取消发货订单
 * @param id 发货记录ID
 * @returns 操作结果
 */
export function cancelShippingOrder(id: number): Promise<ApiResponse<string>> {
  return request({
    url: `${ORDER_BASE_URL}/${id}/ship`,
    method: 'delete'
  })
}

/**
 * 导出发货订单
 * @param params 导出参数
 * @returns CSV文件Blob
 */
export function exportShippingOrders(params: ShippingExportRequest): Promise<Blob> {
  return request({
    url: `${ORDER_BASE_URL}/shipping/export`,
    method: 'get',
    params,
    responseType: 'blob'
  }) as unknown as Promise<Blob>
}

/**
 * 获取发货订单统计信息
 * @param params 查询参数
 * @returns 统计信息
 */
export function getShippingStats(params: ShippingOrderListRequest): Promise<ApiResponse<ShippingStatsResponse>> {
  return request({
    url: `${ORDER_BASE_URL}/shipping/stats`,
    method: 'get',
    params
  })
}

// ==================== 回收订单 API ====================

/**
 * 获取回收订单列表
 * @param params 查询参数
 * @returns 分页回收订单列表
 */
export function getRecoveryOrders(params: OrderListRequest): Promise<ApiResponse<PagedResult<RecoveryOrderResponse>>> {
  return request({
    url: `${ORDER_BASE_URL}/recovery`,
    method: 'get',
    params
  })
}

/**
 * 导出回收订单
 * @param params 导出参数
 * @returns CSV文件Blob
 */
export function exportRecoveryOrders(params: RecoveryExportRequest): Promise<Blob> {
  return request({
    url: `${ORDER_BASE_URL}/recovery/export`,
    method: 'get',
    params,
    responseType: 'blob'
  }) as unknown as Promise<Blob>
}

// ==================== 综合订单 API ====================

/**
 * 获取综合订单列表
 * @param params 查询参数
 * @returns 分页订单列表
 */
export function getComprehensiveOrders(params: ComprehensiveOrderListRequest): Promise<ApiResponse<PagedResult<OrderListResponse>>> {
  return request({
    url: `${ORDER_BASE_URL}/list`,
    method: 'get',
    params
  })
}

/**
 * 导出综合订单
 * @param params 导出参数
 * @returns CSV文件Blob
 */
export function exportComprehensiveOrders(params: ComprehensiveOrderExportRequest): Promise<Blob> {
  return request({
    url: `${ORDER_BASE_URL}/list/export`,
    method: 'get',
    params,
    responseType: 'blob'
  }) as unknown as Promise<Blob>
}
