/**
 * 어드민 일정 관리 API
 */
import { fetchAdminApi, fetchAdminFormData } from "../index";

// 일정 목록 조회 (월별)
export async function getSchedules(year, month) {
  const data = await fetchAdminApi(`/api/schedules?year=${year}&month=${month}`);

  // 날짜별 그룹화된 응답을 플랫 배열로 변환
  const schedules = [];
  for (const [date, dayData] of Object.entries(data)) {
    for (const schedule of dayData.schedules) {
      const category = schedule.category || {};
      schedules.push({
        ...schedule,
        date,
        category_id: category.id,
        category_name: category.name,
        category_color: category.color,
      });
    }
  }
  return schedules;
}

// 일정 검색 (Meilisearch)
export async function searchSchedules(query) {
  return fetchAdminApi(
    `/api/admin/schedules/search?q=${encodeURIComponent(query)}`
  );
}

// 일정 상세 조회
export async function getSchedule(id) {
  return fetchAdminApi(`/api/admin/schedules/${id}`);
}

// 일정 생성
export async function createSchedule(formData) {
  return fetchAdminFormData("/api/admin/schedules", formData, "POST");
}

// 일정 수정
export async function updateSchedule(id, formData) {
  return fetchAdminFormData(`/api/admin/schedules/${id}`, formData, "PUT");
}

// 일정 삭제
export async function deleteSchedule(id) {
  return fetchAdminApi(`/api/admin/schedules/${id}`, { method: "DELETE" });
}
