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

/**
 * 교배 조합 저장 관련 API
 */

export interface BreedSave {
  pkSaveNo: number;
  fkUserNo: number;
  fkCowNo: string;
  fkKpnNo: string;
  saveMemo?: string;
  delYn: 'Y' | 'N';
  regDt: Date;
  updtDt: Date;
  scheduledDate?: string; // 교배 예정일 (선택)
  completed?: boolean; // 교배 완료 여부 (선택)
  completedDate?: string; // 교배 완료일 (선택)
  cow?: any;
  kpn?: any;
  user?: any;
}

export interface CreateBreedSaveDto {
  fkUserNo: number;
  fkCowNo: string;
  fkKpnNo: string;
  saveMemo?: string;
  scheduledDate?: string;
}

export interface UpdateBreedSaveDto {
  saveMemo?: string;
  scheduledDate?: string;
  completed?: boolean;
  completedDate?: string;
}

export interface FilterBreedSaveDto {
  fkUserNo?: number;
  fkCowNo?: string;
  fkKpnNo?: string;
  startDate?: string;
  endDate?: string;
  page?: number;
  limit?: number;
  sortBy?: string;
  sortOrder?: 'ASC' | 'DESC';
}

export const breedApi = {
  /**
   * POST /breed - 교배 조합 저장
   */
  create: async (data: CreateBreedSaveDto): Promise<BreedSave> => {
    return await apiClient.post('/breed', data) as unknown as BreedSave;
  },

  /**
   * GET /breed - 교배 조합 목록 조회 (필터링 + 페이징)
   */
  findAll: async (filter?: FilterBreedSaveDto): Promise<{
    data: BreedSave[];
    total: number;
    page: number;
    limit: number;
  }> => {
    return await apiClient.get('/breed', { params: filter });
  },

  /**
   * GET /breed/search - 교배 조합 검색
   *
   * @param keyword - 검색어 (개체번호, KPN번호, 메모)
   * @param userNo - 사용자 번호 (선택)
   * @param limit - 결과 제한 (기본 20)
   */
  search: async (keyword: string, userNo?: number, limit: number = 20): Promise<BreedSave[]> => {
    return await apiClient.get('/breed/search', {
      params: { keyword, userNo, limit },
    });
  },

  /**
   * GET /breed/:id - 교배 조합 단건 조회
   */
  findOne: async (id: number): Promise<BreedSave> => {
    return await apiClient.get(`/breed/${id}`);
  },

  /**
   * GET /breed/cow/:cowNo - 암소별 교배 조합 조회
   */
  findByCow: async (cowNo: string): Promise<BreedSave[]> => {
    return await apiClient.get(`/breed/cow/${cowNo}`);
  },

  /**
   * GET /breed/kpn/:kpnNo - KPN별 교배 조합 조회
   */
  findByKpn: async (kpnNo: string): Promise<BreedSave[]> => {
    return await apiClient.get(`/breed/kpn/${kpnNo}`);
  },

  /**
   * GET /breed/user/:userNo - 사용자별 교배 조합 조회
   */
  findByUser: async (userNo: number): Promise<BreedSave[]> => {
    return await apiClient.get(`/breed/user/${userNo}`);
  },

  /**
   * GET /breed/date-range/:startDate/:endDate - 날짜 범위로 조회
   */
  findByDateRange: async (startDate: string, endDate: string): Promise<BreedSave[]> => {
    return await apiClient.get(`/breed/date-range/${startDate}/${endDate}`);
  },

  /**
   * PATCH /breed/:id - 교배 조합 수정
   */
  update: async (id: number, data: UpdateBreedSaveDto): Promise<BreedSave> => {
    return await apiClient.patch(`/breed/${id}`, data);
  },

  /**
   * DELETE /breed/:id - 교배 조합 삭제 (소프트 삭제)
   */
  remove: async (id: number): Promise<void> => {
    await apiClient.delete(`/breed/${id}`);
  },
};
