// DBIS Admin API Service
import { apiClient } from './client';
import type {
  NetworkHealthStatus,
  SettlementThroughput,
  GRULiquidityMetrics,
  RiskFlags,
  SCBStatus,
  ParticipantInfo,
} from '@/types';

export interface GlobalOverviewDashboard {
  networkHealth: NetworkHealthStatus[];
  settlementThroughput: SettlementThroughput;
  gruLiquidity: GRULiquidityMetrics;
  riskFlags: RiskFlags;
  scbStatus: SCBStatus[];
}

export interface JurisdictionSettings {
  scbId: string;
  allowedAssetClasses: string[];
  corridorRules: Array<{
    targetSCB: string;
    caps: number;
    allowedSettlementAssets: string[];
  }>;
  regulatoryProfiles: {
    amlStrictness: 'low' | 'medium' | 'high';
    sanctionsLists: string[];
    reportingFrequency: string;
  };
}

class DBISAdminAPI {
  // Global Overview
  async getGlobalOverview(): Promise<GlobalOverviewDashboard> {
    return apiClient.get<GlobalOverviewDashboard>('/api/admin/dbis/dashboard/overview');
  }

  // Participants
  async getParticipants(): Promise<ParticipantInfo[]> {
    return apiClient.get<ParticipantInfo[]>('/api/admin/dbis/participants');
  }

  async getParticipantDetails(scbId: string): Promise<ParticipantInfo> {
    return apiClient.get<ParticipantInfo>(`/api/admin/dbis/participants/${scbId}`);
  }

  async getJurisdictionSettings(scbId: string): Promise<JurisdictionSettings> {
    return apiClient.get<JurisdictionSettings>(`/api/admin/dbis/participants/${scbId}/jurisdiction`);
  }

  async getCorridors() {
    return apiClient.get('/api/admin/dbis/corridors');
  }

  // GRU Command
  async getGRUCommandDashboard() {
    return apiClient.get('/api/admin/dbis/gru/command');
  }

  async createGRUIssuanceProposal(data: any) {
    return apiClient.post('/api/admin/dbis/gru/issuance/proposal', data);
  }

  async lockUnlockGRUClass(data: any) {
    return apiClient.post('/api/admin/dbis/gru/lock', data);
  }

  async setCircuitBreakers(data: any) {
    return apiClient.post('/api/admin/dbis/gru/circuit-breakers', data);
  }

  async manageBondIssuanceWindow(data: any) {
    return apiClient.post('/api/admin/dbis/gru/bonds/window', data);
  }

  async triggerEmergencyBuyback(bondId: string, amount: number) {
    return apiClient.post('/api/admin/dbis/gru/bonds/buyback', { bondId, amount });
  }

  // GAS & QPS
  async getGASQPSDashboard() {
    return apiClient.get('/api/admin/dbis/gas-qps');
  }

  // CBDC & FX
  async getCBDCFXDashboard() {
    return apiClient.get('/api/admin/dbis/cbdc-fx');
  }

  // Metaverse & Edge
  async getMetaverseEdgeDashboard() {
    return apiClient.get('/api/admin/dbis/metaverse-edge');
  }

  // Risk & Compliance
  async getRiskComplianceDashboard() {
    return apiClient.get('/api/admin/dbis/risk-compliance');
  }

  // Corridor Controls
  async adjustCorridorCaps(data: any) {
    return apiClient.post('/api/admin/dbis/corridors/caps', data);
  }

  async throttleCorridor(data: any) {
    return apiClient.post('/api/admin/dbis/corridors/throttle', data);
  }

  async enableDisableCorridor(data: any) {
    return apiClient.post('/api/admin/dbis/corridors/enable-disable', data);
  }

  // Network Controls
  async quiesceSubsystem(data: any) {
    return apiClient.post('/api/admin/dbis/network/quiesce', data);
  }

  async activateKillSwitch(data: any) {
    return apiClient.post('/api/admin/dbis/network/kill-switch', data);
  }

  async escalateIncident(data: any) {
    return apiClient.post('/api/admin/dbis/network/escalate', data);
  }
}

  // Liquidity Engine methods
  async getLiquidityDecisionMap() {
    return apiClient.get('/api/admin/liquidity/decision-map');
  }

  async updateLiquidityDecisionMap(decisionMap: any) {
    return apiClient.put('/api/admin/liquidity/decision-map', decisionMap);
  }

  async getLiquidityQuotes(params: { inputToken: string; outputToken: string; amount: string }) {
    return apiClient.get('/api/admin/liquidity/quotes', { params });
  }

  async getLiquidityRoutingStats() {
    return apiClient.get('/api/admin/liquidity/routing-stats');
  }

  async simulateRoute(params: { inputToken: string; outputToken: string; amount: string }) {
    return apiClient.post('/api/admin/liquidity/simulate-route', params);
  }
}

export const dbisAdminApi = new DBISAdminAPI();

