/**
 * Admin API Service
 * All routes under /api/admin/* — proxied to Go backend (localhost:8080).
 * The global request baseURL points to AUTH service, so we override it here
 * with baseURL: '' to let the dev proxy handle /api/* routes.
 */
import { request } from '@umijs/max';

const BASE = '/api/admin';

/** Wrapper that forces requests through the dev proxy (Go backend) instead of AUTH_BASE_URL */
function adminRequest<T>(url: string, opts: Record<string, any> = {}) {
  return request<T>(url, { ...opts, baseURL: '', skipErrorHandler: true });
}

// ─── Generic response types ────────────────────────────────────────────────

interface ListRes<T> {
  success: boolean;
  data: T[];
  total: number;
  message?: string;
}

interface ItemRes<T> {
  success: boolean;
  data: T;
  message?: string;
}

// ─── Assets ───────────────────────────────────────────────────────────────

export function listAssets(params: Record<string, any>) {
  return adminRequest<ListRes<AdminAPI.Asset>>(`${BASE}/assets`, { method: 'GET', params });
}

export function createAsset(data: Partial<AdminAPI.Asset>) {
  return adminRequest<ItemRes<AdminAPI.Asset>>(`${BASE}/assets`, { method: 'POST', data });
}

export function updateAsset(id: number, data: Partial<AdminAPI.Asset>) {
  return adminRequest<ItemRes<AdminAPI.Asset>>(`${BASE}/assets/${id}`, { method: 'PUT', data });
}

export function deleteAsset(id: number) {
  return adminRequest<ItemRes<any>>(`${BASE}/assets/${id}`, { method: 'DELETE' });
}

// ─── Asset Custody ────────────────────────────────────────────────────────

export function listAssetCustody(params: Record<string, any>) {
  return adminRequest<ListRes<AdminAPI.AssetCustody>>(`${BASE}/asset-custody`, { method: 'GET', params });
}

export function createAssetCustody(data: Partial<AdminAPI.AssetCustody>) {
  return adminRequest<ItemRes<AdminAPI.AssetCustody>>(`${BASE}/asset-custody`, { method: 'POST', data });
}

export function updateAssetCustody(id: number, data: Partial<AdminAPI.AssetCustody>) {
  return adminRequest<ItemRes<AdminAPI.AssetCustody>>(`${BASE}/asset-custody/${id}`, { method: 'PUT', data });
}

export function deleteAssetCustody(id: number) {
  return adminRequest<ItemRes<any>>(`${BASE}/asset-custody/${id}`, { method: 'DELETE' });
}

// ─── Asset Audit Reports ──────────────────────────────────────────────────

export function listAuditReports(params: Record<string, any>) {
  return adminRequest<ListRes<AdminAPI.AssetAuditReport>>(`${BASE}/asset-audit-reports`, { method: 'GET', params });
}

export function createAuditReport(data: Partial<AdminAPI.AssetAuditReport>) {
  return adminRequest<ItemRes<AdminAPI.AssetAuditReport>>(`${BASE}/asset-audit-reports`, { method: 'POST', data });
}

export function updateAuditReport(id: number, data: Partial<AdminAPI.AssetAuditReport>) {
  return adminRequest<ItemRes<AdminAPI.AssetAuditReport>>(`${BASE}/asset-audit-reports/${id}`, { method: 'PUT', data });
}

export function deleteAuditReport(id: number) {
  return adminRequest<ItemRes<any>>(`${BASE}/asset-audit-reports/${id}`, { method: 'DELETE' });
}

// ─── Points Rules ─────────────────────────────────────────────────────────

export function listPointsRules(params: Record<string, any>) {
  return adminRequest<ListRes<AdminAPI.PointsRule>>(`${BASE}/points-rules`, { method: 'GET', params });
}

export function createPointsRule(data: Partial<AdminAPI.PointsRule>) {
  return adminRequest<ItemRes<AdminAPI.PointsRule>>(`${BASE}/points-rules`, { method: 'POST', data });
}

export function updatePointsRule(id: number, data: Partial<AdminAPI.PointsRule>) {
  return adminRequest<ItemRes<AdminAPI.PointsRule>>(`${BASE}/points-rules/${id}`, { method: 'PUT', data });
}

export function deletePointsRule(id: number) {
  return adminRequest<ItemRes<any>>(`${BASE}/points-rules/${id}`, { method: 'DELETE' });
}

// ─── Seasons ──────────────────────────────────────────────────────────────

export function listSeasons(params: Record<string, any>) {
  return adminRequest<ListRes<AdminAPI.Season>>(`${BASE}/seasons`, { method: 'GET', params });
}

export function createSeason(data: Partial<AdminAPI.Season>) {
  return adminRequest<ItemRes<AdminAPI.Season>>(`${BASE}/seasons`, { method: 'POST', data });
}

export function updateSeason(id: number, data: Partial<AdminAPI.Season>) {
  return adminRequest<ItemRes<AdminAPI.Season>>(`${BASE}/seasons/${id}`, { method: 'PUT', data });
}

export function deleteSeason(id: number) {
  return adminRequest<ItemRes<any>>(`${BASE}/seasons/${id}`, { method: 'DELETE' });
}

// ─── VIP Tiers ────────────────────────────────────────────────────────────

export function listVIPTiers(params: Record<string, any>) {
  return adminRequest<ListRes<AdminAPI.VIPTier>>(`${BASE}/vip-tiers`, { method: 'GET', params });
}

export function createVIPTier(data: Partial<AdminAPI.VIPTier>) {
  return adminRequest<ItemRes<AdminAPI.VIPTier>>(`${BASE}/vip-tiers`, { method: 'POST', data });
}

export function updateVIPTier(id: number, data: Partial<AdminAPI.VIPTier>) {
  return adminRequest<ItemRes<AdminAPI.VIPTier>>(`${BASE}/vip-tiers/${id}`, { method: 'PUT', data });
}

export function deleteVIPTier(id: number) {
  return adminRequest<ItemRes<any>>(`${BASE}/vip-tiers/${id}`, { method: 'DELETE' });
}

// ─── Users ────────────────────────────────────────────────────────────────

export function listUsers(params: Record<string, any>) {
  return adminRequest<ListRes<AdminAPI.User>>(`${BASE}/users`, { method: 'GET', params });
}

export function updateUser(wallet: string, data: Partial<AdminAPI.User>) {
  return adminRequest<ItemRes<AdminAPI.User>>(`${BASE}/users/${wallet}`, { method: 'PUT', data });
}

// ─── Invite Codes ─────────────────────────────────────────────────────────

export function listInviteCodes(params: Record<string, any>) {
  return adminRequest<ListRes<AdminAPI.InviteCode>>(`${BASE}/invite-codes`, { method: 'GET', params });
}

export function toggleInviteCode(id: number) {
  return adminRequest<ItemRes<AdminAPI.InviteCode>>(`${BASE}/invite-codes/${id}/toggle`, { method: 'PUT' });
}

export function updateInviteCode(id: number, data: Partial<AdminAPI.InviteCode>) {
  return adminRequest<ItemRes<AdminAPI.InviteCode>>(`${BASE}/invite-codes/${id}`, { method: 'PATCH', data });
}

// ─── Product Links ─────────────────────────────────────────────────────────

export function listProductLinks(params: Record<string, any>) {
  return adminRequest<ListRes<AdminAPI.ProductLink>>(`${BASE}/product-links`, { method: 'GET', params });
}

export function createProductLink(data: Partial<AdminAPI.ProductLink>) {
  return adminRequest<ItemRes<AdminAPI.ProductLink>>(`${BASE}/product-links`, { method: 'POST', data });
}

export function updateProductLink(id: number, data: Partial<AdminAPI.ProductLink>) {
  return adminRequest<ItemRes<AdminAPI.ProductLink>>(`${BASE}/product-links/${id}`, { method: 'PUT', data });
}

export function deleteProductLink(id: number) {
  return adminRequest<ItemRes<any>>(`${BASE}/product-links/${id}`, { method: 'DELETE' });
}

// ─── System Contracts ─────────────────────────────────────────────────────

export function listSystemContracts(params: Record<string, any>) {
  return adminRequest<ListRes<AdminAPI.SystemContract>>(`${BASE}/system-contracts`, { method: 'GET', params });
}

export function createSystemContract(data: Partial<AdminAPI.SystemContract>) {
  return adminRequest<ItemRes<AdminAPI.SystemContract>>(`${BASE}/system-contracts`, { method: 'POST', data });
}

export function updateSystemContract(id: number, data: Partial<AdminAPI.SystemContract>) {
  return adminRequest<ItemRes<AdminAPI.SystemContract>>(`${BASE}/system-contracts/${id}`, { method: 'PUT', data });
}

export function deleteSystemContract(id: number) {
  return adminRequest<ItemRes<any>>(`${BASE}/system-contracts/${id}`, { method: 'DELETE' });
}

// ─── Liquidation Bot ──────────────────────────────────────────────────────

export interface LiquidationBotStatus {
  running: boolean;
  liquidator_addr: string;
  chain_id: number;
  contract_addr: string;
  total_liquidations: number;
  last_check_time: string;
  last_block_checked: number;
  loop_delay_ms: number;
  lookback_blocks: number;
  error?: string;
}

export interface LiquidationRecord {
  id: number;
  chain_id: number;
  tx_hash: string;
  liquidator_addr: string;
  account_count: number;
  accounts: string;
  gas_used: number;
  block_number: number;
  status: string;
  error_message?: string;
  created_at: string;
}

export function getLiquidationStatus() {
  return adminRequest<ItemRes<LiquidationBotStatus>>(`${BASE}/liquidation/status`, { method: 'GET' });
}

export function startLiquidationBot() {
  return adminRequest<ItemRes<any>>(`${BASE}/liquidation/start`, { method: 'POST' });
}

export function stopLiquidationBot() {
  return adminRequest<ItemRes<any>>(`${BASE}/liquidation/stop`, { method: 'POST' });
}

export function listLiquidationRecords(params: Record<string, any>) {
  return adminRequest<ListRes<LiquidationRecord>>(`${BASE}/liquidation/records`, { method: 'GET', params });
}

// ─── Collateral Buyer Bot ─────────────────────────────────────────────────

export interface BuyerBotStatus {
  running: boolean;
  buyer_addr: string;
  chain_id: number;
  contract_addr: string;
  total_buys: number;
  last_check_time: string;
  last_block_checked: number;
  loop_delay_ms: number;
  lookback_blocks: number;
  max_amount: string;
  slippage_pct: number;
  error?: string;
}

export interface CollateralBuyRecord {
  id: number;
  chain_id: number;
  tx_hash: string;
  buyer_addr: string;
  asset_addr: string;
  asset_symbol: string;
  paid_amount: string;
  received_amount: string;
  gas_used: number;
  block_number: number;
  status: string;
  error_message?: string;
  created_at: string;
}

export function getBuyerBotStatus() {
  return adminRequest<ItemRes<BuyerBotStatus>>(`${BASE}/buyer/status`, { method: 'GET' });
}

export function startBuyerBot() {
  return adminRequest<ItemRes<any>>(`${BASE}/buyer/start`, { method: 'POST' });
}

export function stopBuyerBot() {
  return adminRequest<ItemRes<any>>(`${BASE}/buyer/stop`, { method: 'POST' });
}

export function listBuyRecords(params: Record<string, any>) {
  return adminRequest<ListRes<CollateralBuyRecord>>(`${BASE}/buyer/records`, { method: 'GET', params });
}
