import { apiClient } from '@/lib/api/client';

// 待解锁算力记录（来自 mining-service 的 PendingContributionMining 表）
export interface PendingContribution {
  id: string;
  sourceAdoptionId: string;
  sourceAccountSequence: string;
  wouldBeAccountSequence: string;
  contributionType: string;
  amount: string;
  reason: string;
  effectiveDate: string;
  expireDate: string;
  isExpired: boolean;
  lastSyncedAt: string | null;
  createdAt: string;
}

// 待解锁算力列表响应
export interface PendingContributionsResponse {
  contributions: PendingContribution[];
  total: number;
  page: number;
  pageSize: number;
}

// 待解锁算力汇总统计
export interface PendingContributionsSummary {
  byType: Array<{
    contributionType: string;
    totalAmount: string;
    count: number;
  }>;
  total: {
    totalAmount: string;
    count: number;
  };
  totalMinedToHeadquarters: string;
}

// 待解锁算力的挖矿记录
export interface PendingMiningRecord {
  id: string;
  pendingContributionId?: string;
  miningMinute: string;
  sourceAccountSequence?: string;
  wouldBeAccountSequence?: string;
  contributionType?: string;
  contributionAmount: string;
  networkTotalContribution: string;
  contributionRatio: string;
  secondDistribution: string;
  minedAmount: string;
  allocatedTo: string;
  reason?: string;
  createdAt: string;
}

// 挖矿记录响应
export interface PendingMiningRecordsResponse {
  records: PendingMiningRecord[];
  total: number;
  page: number;
  pageSize: number;
}

export const pendingContributionsApi = {
  // 获取待解锁算力列表
  getList: async (
    page: number = 1,
    pageSize: number = 20,
    contributionType?: string
  ): Promise<PendingContributionsResponse> => {
    const params = new URLSearchParams();
    params.append('page', page.toString());
    params.append('pageSize', pageSize.toString());
    if (contributionType) {
      params.append('contributionType', contributionType);
    }
    const response = await apiClient.get(
      `/pending-contributions?${params.toString()}`
    );
    return response.data.data;
  },

  // 获取汇总统计
  getSummary: async (): Promise<PendingContributionsSummary> => {
    const response = await apiClient.get('/pending-contributions/summary');
    return response.data.data;
  },

  // 获取所有待解锁算力的挖矿记录
  getAllMiningRecords: async (
    page: number = 1,
    pageSize: number = 20
  ): Promise<PendingMiningRecordsResponse> => {
    const response = await apiClient.get(
      `/pending-contributions/mining-records?page=${page}&pageSize=${pageSize}`
    );
    return response.data.data;
  },

  // 获取某条待解锁算力的挖矿记录
  getMiningRecords: async (
    id: string,
    page: number = 1,
    pageSize: number = 20
  ): Promise<PendingMiningRecordsResponse> => {
    const response = await apiClient.get(
      `/pending-contributions/${id}/records?page=${page}&pageSize=${pageSize}`
    );
    return response.data.data;
  },
};
