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

/**
 * 농장(Farm) 관련 API
 */

export interface FarmDto {
  pkFarmNo: number;
  fkUserNo: number;
  farmCode: string;
  farmName: string;
  farmAddress: string;
  farmBizNo: string;
  farmGovNo?: string;
  delYn: 'Y' | 'N';
  regDt: Date;
  updtDt: Date;
}

export interface FarmPaternityDto {
  pkFarmPaternityNo: number;
  fkFarmAnlysNo: number;
  receiptDate: Date;
  farmOwnerName: string;
  individualNo: string;
  kpnNo: string;
  motherIndividualNo: string;
  hairRootQuality: string;
  remarks: string;
  fatherMatch: string; // '일치', '불일치', '정보없음'
  motherMatch: string; // '일치', '불일치', '정보없음'
  reportDate: Date;
}

export interface FarmAnalysisRequestDto {
  pkFarmAnlysNo: number;
  fkFileNo: number;
  fkFarmNo: number;
  farmAnlysNm: string;
  anlysReqDt: Date;
  region: string;
  city: string;
  anlysReqCnt: number;
  farmAnlysCnt: number;
  matchCnt: number;
  mismatchCnt: number;
  failCnt: number;
  noHistCnt: number;
  matchRate: number;
  msAnlysCnt: number;
  anlysRmrk: string;
  delYn: 'Y' | 'N';
  regDt: Date;
  updtDt: Date;
  paternities?: FarmPaternityDto[]; // 친자확인 목록
}

export const farmApi = {
  /**
   * GET /farm - 현재 로그인한 사용자의 농장 목록 조회
   */
  findAll: async (): Promise<FarmDto[]> => {
    return await apiClient.get('/farm');
  },

  /**
   * GET /farm/:id - 농장 상세 조회
   */
  findOne: async (farmNo: number): Promise<FarmDto> => {
    return await apiClient.get(`/farm/${farmNo}`);
  },

  /**
   * POST /farm - 농장 생성
   */
  create: async (data: {
    userNo: number;
    farmName: string;
    farmAddress: string;
    farmBizNo: string;
    farmGovNo?: string;
  }): Promise<FarmDto> => {
    return await apiClient.post('/farm', data);
  },

  /**
   * PATCH /farm/:id - 농장 수정
   */
  update: async (
    farmNo: number,
    data: {
      farmName?: string;
      farmAddress?: string;
      farmBizNo?: string;
      farmGovNo?: string;
    }
  ): Promise<FarmDto> => {
    return await apiClient.patch(`/farm/${farmNo}`, data);
  },

  /**
   * DELETE /farm/:id - 농장 삭제 (소프트 삭제)
   */
  remove: async (farmNo: number): Promise<void> => {
    await apiClient.delete(`/farm/${farmNo}`);
  },

  /**
   * GET /farm/:farmNo/analysis-latest - 농장 최신 분석 의뢰 정보 조회
   */
  getLatestAnalysisRequest: async (farmNo: number): Promise<FarmAnalysisRequestDto | null> => {
    return await apiClient.get(`/farm/${farmNo}/analysis-latest`);
  },

  /**
   * GET /farm/:farmNo/analysis-all - 농장 전체 분석 의뢰 목록 조회
   */
  getAllAnalysisRequests: async (farmNo: number): Promise<FarmAnalysisRequestDto[]> => {
    return await apiClient.get(`/farm/${farmNo}/analysis-all`);
  },
};
