import apiClient from '../api-client';

/**
 * 대시보드 필터 DTO
 */
export interface DashboardFilterDto {
  anlysStatus?: string;
  reproType?: string;
  geneGrades?: string[];
  genomeGrades?: string[];
  reproGrades?: string[];
  targetGenes?: string[];
  minScore?: number;
  limit?: number; // 결과 개수 제한 (예: Top 3, Top 5)
  regionNm?: string; // 지역명 (예: 보은군)
}

/**
 * 개별 소 순위 정보
 */
export interface CattleRankingDto {
  cowNo: string;           // 개체 번호
  cowName: string;         // 개체 이름
  genomeScore: number;     // 유전체 점수
  rank: number;            // 보은군 내 순위
  totalCattle: number;     // 보은군 전체 소 수
  percentile: number;      // 상위 몇 %
}

/**
 * 농장 내 소 순위 목록
 */
export interface FarmCattleRankingsDto {
  farmNo: number;
  farmName: string;
  regionName: string;
  totalCattle: number;      // 보은군 전체 소 수
  farmCattleCount: number;  // 농장 내 소 수
  rankings: CattleRankingDto[];
  statistics: {
    bestRank: number;       // 최고 순위
    averageRank: number;    // 평균 순위
    topPercentCount: number; // 상위 10% 개체 수
  };
}

/**
 * 대시보드 관련 API
 */
export const dashboardApi = {
  /**
   * GET /dashboard/summary/:farmNo - 농장 현황 요약
   */
  getFarmSummary: async (farmNo: number, filter?: DashboardFilterDto): Promise<any> => {
    return await apiClient.get(`/dashboard/summary/${farmNo}`, {
      params: filter,
    });
  },

  /**
   * GET /dashboard/analysis-completion/:farmNo - 분석 완료 현황
   */
  getAnalysisCompletion: async (farmNo: number, filter?: DashboardFilterDto): Promise<any> => {
    return await apiClient.get(`/dashboard/analysis-completion/${farmNo}`, {
      params: filter,
    });
  },

  /**
   * GET /dashboard/evaluation/:farmNo - 농장 종합 평가
   */
  getFarmEvaluation: async (farmNo: number, filter?: DashboardFilterDto): Promise<any> => {
    return await apiClient.get(`/dashboard/evaluation/${farmNo}`, {
      params: filter,
    });
  },

  /**
   * GET /dashboard/region-comparison/:farmNo - 보은군 비교 분석
   */
  getRegionComparison: async (farmNo: number, filter?: DashboardFilterDto): Promise<any> => {
    return await apiClient.get(`/dashboard/region-comparison/${farmNo}`, {
      params: filter,
    });
  },

  /**
   * GET /dashboard/cow-distribution/:farmNo - 개체 분포 분석
   */
  getCowDistribution: async (farmNo: number, filter?: DashboardFilterDto): Promise<any> => {
    return await apiClient.get(`/dashboard/cow-distribution/${farmNo}`, {
      params: filter,
    });
  },

  /**
   * GET /dashboard/kpn-aggregation/:farmNo - KPN 추천 집계
   */
  getKpnRecommendationAggregation: async (farmNo: number, filter?: DashboardFilterDto): Promise<any> => {
    return await apiClient.get(`/dashboard/kpn-aggregation/${farmNo}`, {
      params: filter,
    });
  },

  /**
   * GET /dashboard/farm-kpn-inventory/:farmNo - 농장 보유 KPN 목록
   */
  getFarmKpnInventory: async (farmNo: number): Promise<any> => {
    return await apiClient.get(`/dashboard/farm-kpn-inventory/${farmNo}`);
  },

  /**
   * GET /dashboard/analysis-years/:farmNo - 농장 분석 이력 연도 목록
   */
  getAnalysisYears: async (farmNo: number): Promise<number[]> => {
    return await apiClient.get(`/dashboard/analysis-years/${farmNo}`);
  },

  /**
   * GET /dashboard/analysis-years/:farmNo/latest - 최신 분석 연도 조회
   */
  getLatestAnalysisYear: async (farmNo: number): Promise<number> => {
    return await apiClient.get(`/dashboard/analysis-years/${farmNo}/latest`);
  },

  /**
   * GET /dashboard/year-comparison/:farmNo - 3개년 비교 분석
   */
  getYearComparison: async (farmNo: number): Promise<any> => {
    return await apiClient.get(`/dashboard/year-comparison/${farmNo}`);
  },

  /**
   * GET /dashboard/gene-status/:farmNo - 유전자 보유 현황 분석
   */
  getGeneStatus: async (farmNo: number, filter?: DashboardFilterDto): Promise<any> => {
    return await apiClient.get(`/dashboard/gene-status/${farmNo}`, {
      params: filter,
    });
  },

  /**
   * GET /dashboard/repro-efficiency/:farmNo - 번식 효율성 분석
   */
  getReproEfficiency: async (farmNo: number, filter?: DashboardFilterDto): Promise<any> => {
    return await apiClient.get(`/dashboard/repro-efficiency/${farmNo}`, {
      params: filter,
    });
  },

  /**
   * GET /dashboard/excellent-cows/:farmNo - 우수개체 추천
   */
  getExcellentCows: async (farmNo: number, filter?: DashboardFilterDto): Promise<any> => {
    return await apiClient.get(`/dashboard/excellent-cows/${farmNo}`, {
      params: filter,
    });
  },

  /**
   * GET /dashboard/cull-cows/:farmNo - 도태개체 추천
   */
  getCullCows: async (farmNo: number, filter?: DashboardFilterDto): Promise<any> => {
    return await apiClient.get(`/dashboard/cull-cows/${farmNo}`, {
      params: filter,
    });
  },

  /**
   * GET /dashboard/cattle-ranking/:farmNo - 보은군 내 소 개별 순위 조회
   */
  getCattleRankingInRegion: async (farmNo: number, filter?: DashboardFilterDto): Promise<FarmCattleRankingsDto> => {
    return await apiClient.get(`/dashboard/cattle-ranking/${farmNo}`, {
      params: filter,
    });
  },
};
