/**
 * 스케줄 API
 */
import { fetchApi, fetchAuthApi } from '@/api/client';
import { getTodayKST } from '@/utils';

/**
 * API 응답을 프론트엔드 형식으로 변환
 * - category 객체 → category_id, category_name, category_color 플랫화
 * - members 배열 → member_names 문자열
 */
function transformSchedule(schedule) {
  const category = schedule.category || {};

  // members 배열을 문자열로 (기존 코드 호환성)
  const memberNames = Array.isArray(schedule.members)
    ? schedule.members.join(',')
    : '';

  return {
    ...schedule,
    category_id: category.id,
    category_name: category.name,
    category_color: category.color,
    member_names: memberNames,
  };
}

// ==================== 공개 API ====================

/**
 * 스케줄 목록 조회 (월별)
 */
export async function getSchedules(year, month) {
  const data = await fetchApi(`/schedules?year=${year}&month=${month}`);
  return (data.schedules || []).map(transformSchedule);
}

/**
 * 다가오는 스케줄 조회
 */
export async function getUpcomingSchedules(limit = 3) {
  const today = getTodayKST();
  const data = await fetchApi(`/schedules?startDate=${today}&limit=${limit}`);
  return (data.schedules || []).map(transformSchedule);
}

/**
 * 스케줄 검색 (Meilisearch)
 */
export async function searchSchedules(query, { offset = 0, limit = 20 } = {}) {
  const data = await fetchApi(
    `/schedules?search=${encodeURIComponent(query)}&offset=${offset}&limit=${limit}`
  );
  return {
    ...data,
    schedules: (data.schedules || []).map(transformSchedule),
  };
}

/**
 * 스케줄 상세 조회
 */
export async function getSchedule(id) {
  return fetchApi(`/schedules/${id}`);
}

/**
 * X 프로필 정보 조회
 */
export async function getXProfile(username) {
  return fetchApi(`/schedules/x-profile/${encodeURIComponent(username)}`);
}

/**
 * 카테고리 목록 조회
 */
export async function getCategories() {
  return fetchApi('/schedules/categories');
}

// ==================== 어드민 API ====================

/**
 * [Admin] 스케줄 검색
 */
export async function adminSearchSchedules(query) {
  return fetchAuthApi(`/admin/schedules/search?q=${encodeURIComponent(query)}`);
}

/**
 * [Admin] 스케줄 상세 조회
 */
export async function adminGetSchedule(id) {
  return fetchAuthApi(`/admin/schedules/${id}`);
}

/**
 * [Admin] 스케줄 삭제
 */
export async function deleteSchedule(id) {
  return fetchAuthApi(`/schedules/${id}`, { method: 'DELETE' });
}

// ==================== 카테고리 어드민 API ====================

/**
 * [Admin] 카테고리 생성
 */
export async function createCategory(data) {
  return fetchAuthApi('/admin/schedule-categories', {
    method: 'POST',
    body: JSON.stringify(data),
  });
}

/**
 * [Admin] 카테고리 수정
 */
export async function updateCategory(id, data) {
  return fetchAuthApi(`/admin/schedule-categories/${id}`, {
    method: 'PUT',
    body: JSON.stringify(data),
  });
}

/**
 * [Admin] 카테고리 삭제
 */
export async function deleteCategory(id) {
  return fetchAuthApi(`/admin/schedule-categories/${id}`, { method: 'DELETE' });
}

/**
 * [Admin] 카테고리 순서 변경
 */
export async function reorderCategories(orders) {
  return fetchAuthApi('/admin/schedule-categories-order', {
    method: 'PUT',
    body: JSON.stringify({ orders }),
  });
}
