import { useApi } from './useApi'
import type {
  ApiScheduleCategory,
  ApiScheduleEvent,
  ApiSchedulePerson,
  ScheduleCategoryPayload,
  ScheduleEventPayload,
} from '@/types/schedule'

// 일정 도메인 API Composable — 인터셉터가 success/data 를 언래핑
export function useSchedule() {
  const api = useApi()

  // 카테고리
  async function listCategories(): Promise<ApiScheduleCategory[]> {
    return (await api.get('/schedule/categories')) as unknown as ApiScheduleCategory[]
  }
  async function createCategory(
    payload: ScheduleCategoryPayload,
  ): Promise<ApiScheduleCategory> {
    return (await api.post('/schedule/categories', payload)) as unknown as ApiScheduleCategory
  }
  async function updateCategory(
    id: string,
    payload: Partial<ScheduleCategoryPayload>,
  ): Promise<ApiScheduleCategory> {
    return (await api.patch(`/schedule/categories/${id}`, payload)) as unknown as ApiScheduleCategory
  }
  async function deleteCategory(id: string): Promise<void> {
    await api.delete(`/schedule/categories/${id}`)
  }

  // 참석자 풀
  async function listPeople(): Promise<ApiSchedulePerson[]> {
    return (await api.get('/schedule/people')) as unknown as ApiSchedulePerson[]
  }

  // 일정
  async function listEvents(
    start: string,
    end: string,
  ): Promise<ApiScheduleEvent[]> {
    return (await api.get('/schedule/events', {
      params: { start, end },
    })) as unknown as ApiScheduleEvent[]
  }
  async function createEvent(
    payload: ScheduleEventPayload,
  ): Promise<ApiScheduleEvent> {
    return (await api.post('/schedule/events', payload)) as unknown as ApiScheduleEvent
  }
  async function updateEvent(
    id: string,
    payload: Partial<ScheduleEventPayload>,
  ): Promise<ApiScheduleEvent> {
    return (await api.patch(`/schedule/events/${id}`, payload)) as unknown as ApiScheduleEvent
  }
  async function deleteEvent(id: string): Promise<void> {
    await api.delete(`/schedule/events/${id}`)
  }

  return {
    listCategories,
    createCategory,
    updateCategory,
    deleteCategory,
    listPeople,
    listEvents,
    createEvent,
    updateEvent,
    deleteEvent,
  }
}
