import request from './request'

export interface GroupCourse {
  id: number
  courseName: string
  coachId: number
  courseType: number
  typeName?: string
  startTime: string
  endTime: string
  maxMembers: number
  currentMembers: number
  status: number
  actualStartTime?: string | null
  actualEndTime?: string | null
  location: string
  coverImage: string
  description: string
  pointCardAmount?: number
  storedValueAmount?: number
  createdAt: string
  updatedAt: string
  deletedAt?: string | null
}

export interface CoursePageParams {
  page: number
  size: number
  sort?: string
  order?: string
  keyword?: string
}

export interface CourseSearchParams {
  courseName?: string
  courseType?: number
  startDate?: string
  endDate?: string
  timePeriod?: string
  priceSort?: string
  remainingMost?: boolean
  page?: number
  size?: number
}

export interface PageResult<T> {
  content: T[]
  totalPages: number
  totalElements: number
  currentPage: number
  pageSize: number
  first: boolean
  last: boolean
}

export interface ApiResult<T> {
  success: boolean
  message: string
  data: T
}

export function getCourseList(includeDeleted = false): Promise<GroupCourse[]> {
  return request.get('/api/groupCourse/list', { params: { includeDeleted } })
}

export function getCoursePage(
  params: CoursePageParams,
  includeDeleted = false,
): Promise<{ data: GroupCourse[]; totalPages: number; totalElements: number; page: number; size: number }> {
  return request.post('/api/groupCourse/page', params, {
    params: { includeDeleted },
  })
}

export function searchCourses(
  params: CourseSearchParams,
): Promise<ApiResult<PageResult<GroupCourse>>> {
  return request.post('/api/groupCourse/search', params)
}

export function getCourseById(id: number): Promise<GroupCourse> {
  return request.get(`/api/groupCourse/${id}`)
}

export function getCourseDetail(id: number): Promise<GroupCourse> {
  return request.get(`/api/groupCourse/${id}/detail`)
}

export function createCourse(data: Partial<GroupCourse>): Promise<ApiResult<GroupCourse>> {
  return request.post('/api/groupCourse', data)
}

export function updateCourse(id: number, data: Partial<GroupCourse>): Promise<ApiResult<GroupCourse>> {
  return request.put(`/api/groupCourse/${id}`, data)
}

export function cancelCourse(id: number): Promise<ApiResult<{ id: number; status: number }>> {
  return request.post(`/api/groupCourse/${id}/cancel`)
}

export function deleteCourse(id: number): Promise<ApiResult<null>> {
  return request.delete(`/api/groupCourse/${id}`)
}

export function getCourseTypes(includeDeleted = false): Promise<any[]> {
  return request.get('/api/groupCourse/types', { params: { includeDeleted } })
}

// --- 团课类型管理 ---
export interface CourseType {
  id: number
  typeName: string
  baseDifficulty?: number
  description?: string
  category?: string
  createdAt?: string
  updatedAt?: string
}

export function getTypeList(includeDeleted = false, sortField?: string, sortDirection?: string): Promise<CourseType[]> {
  const params: Record<string, any> = { includeDeleted }
  if (sortField && sortDirection) {
    params.sortField = sortField
    params.sortDirection = sortDirection
  }
  return request.get('/api/groupCourse/types', { params })
}

export function getTypeById(id: number): Promise<CourseType> {
  return request.get(`/api/groupCourse/types/${id}`)
}

export function createType(data: Partial<CourseType>): Promise<ApiResult<CourseType>> {
  return request.post('/api/groupCourse/types', data)
}

export function updateType(id: number, data: Partial<CourseType>): Promise<ApiResult<CourseType>> {
  return request.put(`/api/groupCourse/types/${id}`, data)
}

export function deleteType(id: number, password: string): Promise<ApiResult<null>> {
  return request.post(`/api/groupCourse/types/${id}/delete`, { password })
}

// --- 团课标签管理 ---
export interface CourseLabel {
  id: number
  labelName: string
  color?: string
  description?: string
}

export function getAllLabels(): Promise<CourseLabel[]> {
  return request.get('/api/groupCourse/labels')
}

export function createLabel(data: { labelName: string; color?: string }): Promise<ApiResult<CourseLabel>> {
  return request.post('/api/groupCourse/labels', data)
}

export function getLabelsByTypeId(typeId: number): Promise<CourseLabel[]> {
  return request.get(`/api/groupCourse/types/${typeId}/labels`)
}

export function addLabelsToType(typeId: number, labelIds: number[]): Promise<ApiResult<null>> {
  return request.post(`/api/groupCourse/types/${typeId}/labels`, { labelIds })
}

export function removeLabelFromType(typeId: number, labelId: number): Promise<ApiResult<null>> {
  return request.delete(`/api/groupCourse/types/${typeId}/labels/${labelId}`)
}

export function removeLabelsFromType(typeId: number, labelIds: number[]): Promise<ApiResult<null>> {
  return request.post(`/api/groupCourse/types/${typeId}/labels/delete-batch`, { labelIds })
}

// --- 课程状态映射（从后端统一获取，避免硬编码）---
export interface CourseStatusItem {
  value: number
  dbValue: string
  label: string
  cssClass: string
}

export function fetchCourseStatuses(): Promise<ApiResult<CourseStatusItem[]>> {
  return request.get('/api/groupCourse/statuses')
}