import request from '@/utils/request'
import type { ApiResult, PageResult } from '@/api'
import type { Enterprise, EnterpriseMember, EnterpriseMemberParam, Order, Bill, BillParam, App, AppParam } from '@/types/developer'

// ==================== 企业/租户相关 ====================

/**
 * 获取企业信息
 */
export async function getEnterpriseInfo() {
  const res = await request.get<ApiResult<Enterprise>>('/system/tenant/info')
  if (res.code === 0) return res.data
  return Promise.reject(new Error(res.message))
}

/**
 * 更新企业信息
 */
export async function updateEnterpriseInfo(data: Partial<Enterprise>) {
  const res = await request.put<ApiResult<unknown>>('/system/tenant', data)
  if (res.code === 0) return res.message
  return Promise.reject(new Error(res.message))
}

// ==================== 企业成员/用户相关 ====================

/**
 * 获取企业成员列表
 */
export async function pageEnterpriseMember(params?: EnterpriseMemberParam) {
  const res = await request.get<ApiResult<PageResult<EnterpriseMember>>>('/system/user/page', params)
  if (res.code === 0) return res.data
  return Promise.reject(new Error(res.message))
}

/**
 * 获取企业成员列表（不分页）
 */
export async function listEnterpriseMember(params?: EnterpriseMemberParam) {
  const res = await request.get<ApiResult<EnterpriseMember[]>>('/system/user', params)
  if (res.code === 0 && res.data) return res.data
  return Promise.reject(new Error(res.message))
}

/**
 * 邀请企业成员
 */
export async function inviteEnterpriseMember(enterpriseId: number, data: Partial<EnterpriseMember>) {
  const res = await request.post<ApiResult<unknown>>(`/app/developer/invite/${enterpriseId}/invite`, data)
  if (res.code === 0) return res.message
  return Promise.reject(new Error(res.message))
}

/**
 * 更新企业成员
 */
export async function updateEnterpriseMember(data: Partial<EnterpriseMember>) {
  const res = await request.put<ApiResult<unknown>>(`/system/user`, data)
  if (res.code === 0) return res.message
  return Promise.reject(new Error(res.message))
}

/**
 * 移除企业成员
 */
export async function removeEnterpriseMember(memberId: number) {
  const res = await request.del<ApiResult<unknown>>(`/system/user/${memberId}`)
  if (res.code === 0) return res.message
  return Promise.reject(new Error(res.message))
}

// ==================== 订单相关 ====================

/**
 * 获取订单列表
 */
export async function pageOrder(params?: { page?: number; limit?: number; status?: number }) {
  const res = await request.get<ApiResult<PageResult<Order>>>('/system/order/page', params)
  if (res.code === 0) return res.data
  return Promise.reject(new Error(res.message))
}

/**
 * 获取订单详情
 */
export async function getOrder(id: number) {
  const res = await request.get<ApiResult<Order>>(`/system/order/${id}`)
  if (res.code === 0 && res.data) return res.data
  return Promise.reject(new Error(res.message))
}

/**
 * 创建订单
 */
export async function createOrder(data: any) {
  const res = await request.post<ApiResult<unknown>>('/system/order', data)
  if (res.code === 0) return res.message
  return Promise.reject(new Error(res.message))
}

// ==================== 充值/账单相关 ====================

/**
 * 获取账单列表
 */
export async function pageBill(params?: BillParam) {
  const res = await request.get<ApiResult<PageResult<Bill>>>('/sys/recharge-order/page', params)
  if (res.code === 0) return res.data
  return Promise.reject(new Error(res.message))
}

/**
 * 获取账单概览
 */
export async function getBillOverview() {
  const res = await request.get<ApiResult<{ balance: number; totalConsume: number; totalRecharge: number }>>('/sys/user-balance-log/overview')
  if (res.code === 0) return res.data
  return Promise.reject(new Error(res.message))
}

// ==================== 企业应用相关 ====================

/**
 * 获取企业应用列表
 */
export async function pageEnterpriseApp(params?: AppParam) {
  const res = await request.get<ApiResult<PageResult<App>>>('/app/product/page', { params })
  if (res.code === 0) return res.data
  return Promise.reject(new Error(res.message))
}

/**
 * 获取企业应用详情
 */
export async function getEnterpriseApp(id: number) {
  const res = await request.get<ApiResult<App>>(`/app/product/${id}`)
  if (res.code === 0 && res.data) return res.data
  return Promise.reject(new Error(res.message))
}

/**
 * 购买应用
 */
export async function purchaseApp(productId: number) {
  const res = await request.post<ApiResult<unknown>>('/api/system/order/createOrder', { productId })
  if (res.code === 0) return res.message
  return Promise.reject(new Error(res.message))
}
