import request from "@/utils/request.js"

export function getGroupCourseList(params = {}, options = {}) {
  return request.get('/groupCourse/list', params, options)
}

export function getGroupCoursePage(params = {}, options = { cache: true, cacheTime: 5 * 60 * 1000 }) {
  const { page = 0, size = 10, sort = 'id', order = 'asc', keyword } = params
  return request.post('/groupCourse/page', { page, size, sort, order, keyword }, options)
}

export function getGroupCourseById(id, options = { cache: true, cacheTime: 15 * 60 * 1000 }) {
  return request.get(`/groupCourse/${id}`, {}, options)
}

export function getGroupCourseDetail(id, options = { cache: true, cacheTime: 15 * 60 * 1000 }) {
  return request.get(`/groupCourse/${id}/detail`, {}, options)
}

export function createGroupCourse(params) {
  return request.post('/groupCourse', params)
}

export function updateGroupCourse(id, params) {
  return request.put(`/groupCourse/${id}`, params)
}

export function cancelGroupCourse(id) {
  return request.post(`/groupCourse/${id}/cancel`)
}

export function deleteGroupCourse(id) {
  return request.delete(`/groupCourse/${id}`)
}

export function getGroupCourseTypes(params = {}, options = { cache: true, cacheTime: 10 * 60 * 1000 }) {
  return request.get('/groupCourse/types', params, options)
}

export function getGroupCourseTypeById(id, options = { cache: true, cacheTime: 10 * 60 * 1000 }) {
  return request.get(`/groupCourse/types/${id}`, {}, options)
}

export function getTypeLabels(typeId, options = { cache: true, cacheTime: 5 * 60 * 1000 }) {
  return request.get(`/groupCourse/types/${typeId}/labels`, {}, options)
}

export function searchGroupCourse(params = {}, options = {}) {
  const { 
    courseName, 
    courseType, 
    startDate, 
    endDate, 
    timePeriod, 
    priceSort, 
    remainingMost,
    page = 0, 
    size = 10 
  } = params
  
  const requestBody = { page, size }
  
  if (courseName) requestBody.courseName = courseName
  if (courseType) requestBody.courseType = courseType
  if (startDate) requestBody.startDate = formatDateTime(startDate)
  if (endDate) requestBody.endDate = formatDateTime(endDate, true)
  if (timePeriod) requestBody.timePeriod = timePeriod
  if (priceSort) requestBody.priceSort = priceSort
  if (remainingMost !== undefined) requestBody.remainingMost = remainingMost
  
  return request.post('/groupCourse/search', requestBody, options)
}

function formatDateTime(dateStr, isEnd = false) {
  if (!dateStr) return dateStr
  if (dateStr.includes('T')) return dateStr
  return isEnd 
    ? `${dateStr}T23:59:59` 
    : `${dateStr}T00:00:00`
}

export function bookGroupCourse(params) {
  return request.post('/groupCourse/book', params)
}

export function cancelBooking(bookingId, params) {
  return request.post(`/groupCourse/booking/${bookingId}/cancel`, params)
}

export function getMemberBookings(memberId, options = {}) {
  return request.get(`/groupCourse/bookings/member/${memberId}`, {}, options)
}

export function getActiveRecommendCourses(options = { cache: true, cacheTime: 5 * 60 * 1000 }) {
  return request.get('/groupCourse/recommend/active', {}, options)
}

export function getGroupCourseRecommendList(params = {}, options = { cache: true, cacheTime: 5 * 60 * 1000 }) {
  return request.get('/groupCourse/recommend/list', params, options)
}

export default {
  getGroupCourseList,
  getGroupCoursePage,
  searchGroupCourse,
  getGroupCourseById,
  getGroupCourseDetail,
  createGroupCourse,
  updateGroupCourse,
  cancelGroupCourse,
  deleteGroupCourse,
  getGroupCourseTypes,
  getGroupCourseTypeById,
  getTypeLabels,
  bookGroupCourse,
  cancelBooking,
  getMemberBookings,
  getActiveRecommendCourses,
  getGroupCourseRecommendList
}