import { get, post, postForm } from '@/utils/request'

export interface StakingConfig {
  id: number
  productName: string
  coinUnit: string
  minAmount: string
  maxAmount: string
  lockDays: number
  releaseType: number
  releasePeriod: number
  releaseRate: string
  description?: string
  status: number
  createTime?: string
  updateTime?: string
}

export interface AirdropPool {
  id: number
  title: string
  coinUnit: string
  totalAmount: string
  remainAmount: string
  claimType: number
  perAmount: string
  maxClaimPerUser: number
  startTime: string
  endTime: string
  status: number
}

/** IDO 预售固定认购价（USDT / 质押币） */
export const IDO_SUBSCRIPTION_PRICE_USDT = 0.6

/** 启用中的质押配置列表 */
export function fetchStakingConfigList() {
  return post<StakingConfig[]>('/uc/staking/config/list')
}

/** ID 最小且启用中的质押配置（IDO 预售默认） */
export function fetchMinOpenStakingConfig() {
  return post<StakingConfig>('/uc/staking/config/min-open')
}

/** Redis 现货价（ExchangeNew2:latestPrice，与质押闪兑同源） */
export function fetchStakingSpotPrice(coinUnit: string) {
  return get<{ coinUnit: string; price: string }>('/uc/staking/spot-price', { coinUnit })
}

/** staking_wallet 表余额（StakingWalletVo） */
export interface StakingWalletBalance {
  coinUnit: string
  availableBalance: string | number
  lockedBalance: string | number
  availableBalanceUsdt?: string | number
  lockedBalanceUsdt?: string | number
  /** 历史累计已释放总量（与 coinUnit 同单位，如 iBit） */
  releasedTotal?: string | number
}

/** 查询质押钱包指定币种可用/锁定余额（staking_wallet 表） */
export function fetchStakingWalletBalance(coinUnit: string) {
  return postForm<StakingWalletBalance>('/uc/staking/wallet', { coinUnit })
}

/** 发起质押（直接质押 iBit 等） */
export function submitStake(configId: number, amount: string) {
  return postForm<unknown>('/uc/staking/stake', { configId, amount })
}

/** IDO 预售：支付 USDT，后端按现价闪兑为质押币后质押 */
export function submitStakeByUsdt(configId: number, usdtAmount: string) {
  return postForm<unknown>('/uc/staking/stake', { configId, usdtAmount })
}

export interface StakingOrderItem {
  id: number
  coinUnit: string
  amount: string | number
  releasedAmount: string | number
  status: number
  stakingTime?: string
  unlockTime?: string
  lastReleaseTime?: string
  createTime?: string
}

/** POST /uc/staking/team — IDO 团队总览 */
export interface StakingTeamOverview {
  directNum: number
  teamNum: number
  teamPerformance: number
  totalRevenue: number
  lockTotal: number
  nodeLevel: string
}

/** POST /uc/staking/direct/list — 直推列表项 */
export interface StakingDirectMember {
  directAddress: string
  level: string
  buyAmount: number
  reward: number
}

/** 查询质押团队总览（IDO 页卡片、我的团队页头部） */
export function fetchStakingTeamOverview() {
  return postForm<StakingTeamOverview>('/uc/staking/team', {})
}

/** 分页查询直推列表 */
export function fetchStakingDirectList(pageNo = 1, pageSize = 20) {
  return postForm<SpringPage<StakingDirectMember>>('/uc/staking/direct/list', {
    pageNo,
    pageSize,
  })
}

/** 分页查询我的质押订单，status 支持单个状态或逗号分隔状态，如 "0,1" */
export function fetchStakingOrders(pageNo = 1, pageSize = 20, status?: string | number) {
  return postForm<SpringPage<StakingOrderItem>>('/uc/staking/orders', {
    pageNo,
    pageSize,
    ...(status !== undefined ? { status } : {}),
  })
}

/** 空投领取资格与任务进度 */
export interface AirdropEligibility {
  inviteCount: number
  requiredInviteCount: number
  inviteTaskCompleted: boolean
  hasActiveStaking: boolean
  hasPendingAirdrop: boolean
  /** 待领取空投总金额 */
  claimableAmount?: number | string
  /** 待领取币种（多种币种并存时可能为空） */
  claimableCoinUnit?: string | null
  eligible: boolean
  message?: string
}

/** 查询空投领取资格（邀请人数、活跃质押、待领取记录） */
export function fetchAirdropEligibility() {
  return post<AirdropEligibility>('/uc/airdrop/eligibility')
}

/** 一键提交名下全部待领取空投（进入审核） */
export function claimAirdrop() {
  return post<string>('/uc/airdrop/claim')
}

/** 空投领取/审核记录 */
export interface AirdropRecordItem {
  id: number
  stakingOrderId?: number
  coinUnit?: string
  amount: number | string
  /** 0待领取 1已发放 2审核中 3已拒绝 */
  status: number
  claimTime?: string
  auditTime?: string
  auditRemark?: string
  createTime?: string
}

export interface SpringPage<T> {
  content: T[]
  totalElements: number
  totalPages: number
  number: number
  size: number
  last?: boolean
}

/** 分页查询我的空投记录 */
export function fetchAirdropRecords(pageNo = 1, pageSize = 10) {
  return postForm<SpringPage<AirdropRecordItem>>('/uc/airdrop/records', { pageNo, pageSize })
}
