// DBIS Admin API Service
import { apiClient } from './client';
import { mockGlobalOverview, mockParticipants } from '@/utils/mockData';
import type {
  NetworkHealthStatus,
  SettlementThroughput,
  GRULiquidityMetrics,
  RiskFlags,
  SCBStatus,
  ParticipantInfo,
} from '@/types';
import type {
  CBDCFXDashboard,
  GASQPSDashboard,
  GRUDashboard,
  MetaverseEdgeDashboard,
  RiskComplianceDashboard,
} from '@/types/dashboard';

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

class DBISAdminAPI {
  // Global Overview
  async getGlobalOverview(): Promise<GlobalOverviewDashboard> {
    try {
      return await apiClient.get<GlobalOverviewDashboard>('/api/admin/dbis/dashboard/overview');
    } catch (error: any) {
      // If API is not available, return mock data for development
      if (error?.code === 'ERR_NETWORK' || error?.isNetworkError || error?.message?.includes('Network') || error?.message?.includes('API unavailable')) {
        console.warn('API not available, using mock data');
        return mockGlobalOverview as GlobalOverviewDashboard;
      }
      throw error;
    }
  }

  // Participants
  async getParticipants(): Promise<ParticipantInfo[]> {
    try {
      return await apiClient.get<ParticipantInfo[]>('/api/admin/dbis/participants');
    } catch (error: any) {
      // If API is not available, return mock data for development
      if (error?.code === 'ERR_NETWORK' || error?.isNetworkError || error?.message?.includes('Network') || error?.message?.includes('API unavailable')) {
        console.warn('API not available, using mock data for Participants');
        return mockParticipants;
      }
      throw error;
    }
  }

  // GAS & QPS
  async getGASQPSDashboard(): Promise<GASQPSDashboard> {
    try {
      return await apiClient.get<GASQPSDashboard>('/api/admin/dbis/gas-qps');
    } catch (error: any) {
      if (error?.code === 'ERR_NETWORK' || error?.isNetworkError || error?.message?.includes('Network') || error?.message?.includes('API unavailable')) {
        console.warn('API not available, using mock data');
        return {} as GASQPSDashboard;
      }
      throw error;
    }
  }

  // CBDC & FX
  async getCBDCFXDashboard(): Promise<CBDCFXDashboard> {
    try {
      return await apiClient.get<CBDCFXDashboard>('/api/admin/dbis/cbdc-fx');
    } catch (error: any) {
      if (error?.code === 'ERR_NETWORK' || error?.isNetworkError || error?.message?.includes('Network') || error?.message?.includes('API unavailable')) {
        console.warn('API not available, using mock data');
        return {} as CBDCFXDashboard;
      }
      throw error;
    }
  }

  // Metaverse & Edge
  async getMetaverseEdgeDashboard(): Promise<MetaverseEdgeDashboard> {
    try {
      return await apiClient.get<MetaverseEdgeDashboard>('/api/admin/dbis/metaverse-edge');
    } catch (error: any) {
      if (error?.code === 'ERR_NETWORK' || error?.isNetworkError || error?.message?.includes('Network') || error?.message?.includes('API unavailable')) {
        console.warn('API not available, using mock data');
        return {} as MetaverseEdgeDashboard;
      }
      throw error;
    }
  }

  // Risk & Compliance
  async getRiskComplianceDashboard(): Promise<RiskComplianceDashboard> {
    try {
      return await apiClient.get<RiskComplianceDashboard>('/api/admin/dbis/risk-compliance');
    } catch (error: any) {
      if (error?.code === 'ERR_NETWORK' || error?.isNetworkError || error?.message?.includes('Network') || error?.message?.includes('API unavailable')) {
        console.warn('API not available, using mock data');
        return {} as RiskComplianceDashboard;
      }
      throw error;
    }
  }

  // GRU Command
  async getGRUCommandDashboard(): Promise<GRUDashboard> {
    try {
      return await apiClient.get<GRUDashboard>('/api/admin/dbis/gru/command');
    } catch (error: any) {
      if (error?.code === 'ERR_NETWORK' || error?.isNetworkError || error?.message?.includes('Network') || error?.message?.includes('API unavailable')) {
        console.warn('API not available, using mock data');
        return {} as GRUDashboard;
      }
      throw error;
    }
  }

  // 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: {
        inputToken: params.inputToken,
        outputToken: params.outputToken,
        amount: params.amount,
      }
    } as any);
  }

  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();
