import axios from 'axios';
import { API_BASE_URL } from './apiConfig';

export interface StrategyStats {
  strategyId: number;
  strategyName: string;
  strategyType: string;
  avgReturnRate: number;
  avgWinRate: number;
  avgMaxDrawdown: number;
  avgSharpeRatio: number;
  analysisCount: number;
  totalTrades: number;
  bestReturn?: number;
  worstReturn?: number;
}

export interface StrategyPerformance {
  id: number;
  strategyId: number;
  strategyName: string;
  strategyType: string;
  market: string;
  timeUnit: string;
  analysisPeriodStart: string;
  analysisPeriodEnd: string;
  initialCapital: number;
  finalCapital: number;
  netProfit: number;
  returnRate: number;
  totalTrades: number;
  buyCount: number;
  sellCount: number;
  winningTrades: number;
  losingTrades: number;
  winRate: number;
  maxDrawdown: number;
  sharpeRatio: number;
  analysisDate: string;
}

export interface DataCollectionSummary {
  totalProcessedFiles: number;
  totalRecords: number;
  successCount: number;
  failCount: number;
  skipCount: number;
  lastProcessedAt?: string;
  marketCounts: Record<string, number>;
  timeUnitCounts: Record<string, number>;
}

export interface DashboardSummary {
  totalMarkets: number;
  totalTimeUnits: number;
  totalCandleRecords: number;
  totalProcessedFiles: number;
  strategyStats: StrategyStats[];
  topReturnStrategy?: StrategyPerformance;
  topWinRateStrategy?: StrategyPerformance;
  topSharpeStrategy?: StrategyPerformance;
  recentAnalyses: StrategyPerformance[];
  dataCollectionSummary: DataCollectionSummary;
}

export interface TimeUnitDataStatus {
  timeUnit: string;
  recordCount: number;
  fileCount: number;
  minDate: string | null;
  maxDate: string | null;
  daysCovered: number;
}

export interface MarketDataStatus {
  market: string;
  timeUnitStatus: Record<string, TimeUnitDataStatus>;
  totalRecords: number;
  totalFiles: number;
  oldestDate: string | null;
  newestDate: string | null;
}

export const dashboardApi = {
  getSummary: async (): Promise<DashboardSummary> => {
    const response = await axios.get(`${API_BASE_URL}/dashboard/summary`);
    return response.data;
  },

  getStrategyStats: async (): Promise<StrategyStats[]> => {
    const response = await axios.get(`${API_BASE_URL}/dashboard/strategy-stats`);
    return response.data;
  },

  getStrategyPerformances: async (strategyId: number): Promise<StrategyPerformance[]> => {
    const response = await axios.get(`${API_BASE_URL}/dashboard/performance/${strategyId}`);
    return response.data;
  },

  getPerformancesByMarket: async (market: string): Promise<StrategyPerformance[]> => {
    const response = await axios.get(`${API_BASE_URL}/dashboard/performance/market/${market}`);
    return response.data;
  },

  getAvailableMarkets: async (): Promise<string[]> => {
    const response = await axios.get(`${API_BASE_URL}/dashboard/markets`);
    return response.data;
  },

  getAvailableTimeUnits: async (market: string): Promise<string[]> => {
    const response = await axios.get(`${API_BASE_URL}/dashboard/time-units/${market}`);
    return response.data;
  },

  getMarketDataStatus: async (): Promise<MarketDataStatus[]> => {
    const response = await axios.get(`${API_BASE_URL}/dashboard/data-status`);
    return response.data;
  },

  getMarketDataStatusByMarket: async (market: string): Promise<MarketDataStatus> => {
    const response = await axios.get(`${API_BASE_URL}/dashboard/data-status/${market}`);
    return response.data;
  },

  getTimeUnitDataStatus: async (): Promise<Record<string, any>> => {
    const response = await axios.get(`${API_BASE_URL}/dashboard/data-status/by-timeunit`);
    return response.data;
  },
};

