import request from '@/utils/request';

// 账户管理
export function getPlatformAccounts(params: any) {
  return request.get('/api/admin/finance/accounts/platform', { params });
}

export function getUserAccounts(params: any) {
  return request.get('/api/admin/finance/accounts/users', { params });
}

export function getMerchantAccounts(params: any) {
  return request.get('/api/admin/finance/accounts/merchants', { params });
}

export function getAccountDetail(type: string, id: number) {
  return request.get(`/api/admin/finance/accounts/${type}/${id}`);
}

export function getAccountsSummary() {
  return request.get('/api/admin/finance/accounts/summary');
}

// 交易流水
export function getPlatformTransactions(params: any) {
  return request.get('/api/admin/finance/transactions/platform', { params });
}

export function getUserTransactions(params: any) {
  return request.get('/api/admin/finance/transactions/users', { params });
}

export function getMerchantTransactions(params: any) {
  return request.get('/api/admin/finance/transactions/merchants', { params });
}

export function getTransactionDetail(id: number) {
  return request.get(`/api/admin/finance/transactions/${id}`);
}

// 提现管理
export function getUserWithdrawals(params: any) {
  return request.get('/api/admin/finance/withdrawals/users', { params });
}

export function getMerchantWithdrawals(params: any) {
  return request.get('/api/admin/finance/withdrawals/merchants', { params });
}

export function getPlatformWithdrawals(params: any) {
  return request.get('/api/admin/finance/withdrawals/platform', { params });
}

export function createPlatformWithdrawal(data: any) {
  return request.post('/api/admin/finance/withdrawals/platform', data);
}

export function approvePlatformWithdrawal(id: number) {
  return request.put(`/api/admin/finance/withdrawals/${id}/approve?type=platform`);
}

export function rejectPlatformWithdrawal(id: number, reason: string) {
  return request.put(`/api/admin/finance/withdrawals/${id}/reject?type=platform`, { rejectReason: reason });
}

export function confirmPlatformWithdrawal(id: number, transactionNo: string) {
  return request.put(`/api/admin/finance/withdrawals/${id}/confirm?type=platform`, { paymentNo: transactionNo });
}

export function approveUserWithdrawal(id: number) {
  return request.put(`/api/admin/finance/withdrawals/users/${id}/approve`);
}

export function rejectUserWithdrawal(id: number, reason: string) {
  return request.put(`/api/admin/finance/withdrawals/users/${id}/reject`, { rejectReason: reason });
}

export function confirmUserWithdrawal(id: number, transactionNo: string) {
  return request.put(`/api/admin/finance/withdrawals/users/${id}/confirm`, { paymentNo: transactionNo });
}

export function approveMerchantWithdrawal(id: number) {
  return request.put(`/api/admin/finance/withdrawals/merchants/${id}/approve`);
}

export function rejectMerchantWithdrawal(id: number, reason: string) {
  return request.put(`/api/admin/finance/withdrawals/merchants/${id}/reject`, { rejectReason: reason });
}

export function confirmMerchantWithdrawal(id: number, transactionNo: string) {
  return request.put(`/api/admin/finance/withdrawals/merchants/${id}/confirm`, { paymentNo: transactionNo });
}

// 平台银行卡管理
export function getBankCards(params: any) {
  return request.get('/api/admin/finance/bank-cards', { params });
}

export function getBankCardById(id: number) {
  return request.get(`/api/admin/finance/bank-cards/${id}`);
}

export function createBankCard(data: any) {
  return request.post('/api/admin/finance/bank-cards', data);
}

export function updateBankCard(id: number, data: any) {
  return request.put(`/api/admin/finance/bank-cards/${id}`, data);
}

export function deleteBankCard(id: number) {
  return request.delete(`/api/admin/finance/bank-cards/${id}`);
}

// 结算管理
export function getSettlements(params: any) {
  return request.get('/api/admin/finance/settlements', { params });
}

export function getSettlementDetail(id: number) {
  return request.get(`/api/admin/finance/settlements/${id}`);
}

export function getSettlementItems(id: number, params: any) {
  return request.get(`/api/admin/finance/settlements/${id}/items`, { params });
}

export function generateSettlement(merchantId: number, data: { startDate: string; endDate: string }) {
  return request.post(`/api/admin/finance/settlements/generate/${merchantId}`, data);
}

export function executeWeeklySettlement() {
  return request.post('/api/admin/finance/settlements/execute-weekly');
}

export function previewWeeklySettlement() {
  return request.get('/api/admin/finance/settlements/preview-weekly');
}

// 对账管理
export function getReconciliations(params: any) {
  return request.get('/api/admin/finance/reconciliations', { params });
}

export function getReconciliationDetail(id: number) {
  return request.get(`/api/admin/finance/reconciliations/${id}`);
}

export function triggerReconciliation(date: string) {
  return request.post('/api/admin/finance/reconciliations/trigger', { date });
}

export function getTransactionStats(params: any) {
  return request.get('/api/admin/finance/reconciliations/stats', { params });
}

export function checkBalanceConsistency() {
  return request.get('/api/admin/finance/reconciliations/check-balance');
}

// 财务报表
export function getFinancialOverview(params: any) {
  return request.get('/api/admin/finance/reports/overview', { params });
}

export function getFinancialTrend(params: any) {
  return request.get('/api/admin/finance/reports/trend', { params });
}

export function getDailyReport(params: any) {
  return request.get('/api/admin/finance/reports/daily', { params });
}

export function getWeeklyReport(params: any) {
  return request.get('/api/admin/finance/reports/weekly', { params });
}

export function getMonthlyReport(params: any) {
  return request.get('/api/admin/finance/reports/monthly', { params });
}

export function getMerchantReport(merchantId: number, params: any) {
  return request.get(`/api/admin/finance/reports/merchant/${merchantId}`, { params });
}

export function exportReport(type: string, params: any) {
  return request.get(`/api/admin/finance/reports/export/${type}`, { params, responseType: 'blob' });
}
