// 订单API客户端
export const orderClient = {
  // 模拟客户端方法，实际使用时替换为真实的RPC客户端
  create: { $post: async () => ({}) },
  update: { ':id': { $put: async () => ({}) } },
  delete: { ':id': { $delete: async () => ({}) } },
  list: { $get: async () => ({}) },
  detail: { ':id': { $get: async () => ({}) } },
  activate: { ':orderId': { $post: async () => ({}) } },
  close: { ':orderId': { $post: async () => ({}) } },
  ':orderId': { persons: { batch: { $post: async () => ({}) } } },
  assets: {
    create: { $post: async () => ({}) },
    query: { $get: async () => ({}) },
    delete: { ':id': { $delete: async () => ({}) } },
  },
}

// 扩展的统计API方法
export class OrderStatisticsClient {
  private baseUrl: string

  constructor(baseUrl: string = '/api/v1/yongren') {
    this.baseUrl = baseUrl
  }

  // 获取打卡统计数据
  async getCheckinStatistics(companyId: number) {
    const response = await fetch(`${this.baseUrl}/order/checkin-statistics?companyId=${companyId}`)
    if (!response.ok) {
      throw new Error(`获取打卡统计数据失败: ${response.statusText}`)
    }
    return response.json()
  }

  // 获取视频统计数据
  async getVideoStatistics(companyId: number, assetType?: string) {
    let url = `${this.baseUrl}/order/video-statistics?companyId=${companyId}`
    if (assetType) {
      url += `&assetType=${assetType}`
    }
    const response = await fetch(url)
    if (!response.ok) {
      throw new Error(`获取视频统计数据失败: ${response.statusText}`)
    }
    return response.json()
  }

  // 获取企业订单列表
  async getCompanyOrders(params: {
    companyId: number
    orderName?: string
    orderStatus?: string
    startDate?: string
    endDate?: string
    page?: number
    limit?: number
    sortBy?: string
    sortOrder?: string
  }) {
    const queryParams = new URLSearchParams()
    Object.entries(params).forEach(([key, value]) => {
      if (value !== undefined) {
        queryParams.append(key, String(value))
      }
    })

    const response = await fetch(`${this.baseUrl}/order/company-orders?${queryParams}`)
    if (!response.ok) {
      throw new Error(`获取企业订单列表失败: ${response.statusText}`)
    }
    return response.json()
  }

  // 获取企业视频列表
  async getCompanyVideos(params: {
    companyId: number
    assetType?: string
    page?: number
    pageSize?: number
    sortBy?: string
    sortOrder?: string
  }) {
    const queryParams = new URLSearchParams()
    Object.entries(params).forEach(([key, value]) => {
      if (value !== undefined) {
        queryParams.append(key, String(value))
      }
    })

    const response = await fetch(`${this.baseUrl}/order/company-videos?${queryParams}`)
    if (!response.ok) {
      throw new Error(`获取企业视频列表失败: ${response.statusText}`)
    }
    return response.json()
  }

  // 批量下载视频
  async batchDownloadVideos(request: {
    downloadScope: 'company' | 'person'
    companyId?: number
    personId?: number
    assetTypes?: string[]
    fileIds?: number[]
  }) {
    const response = await fetch(`${this.baseUrl}/order/batch-download`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(request),
    })
    if (!response.ok) {
      throw new Error(`批量下载视频失败: ${response.statusText}`)
    }
    return response.json()
  }
}

// 创建默认实例
export const orderStatisticsClient = new OrderStatisticsClient()