import request from '@/utils/request'
import type { ApiResult, PageResult } from '@/api'
import type {
  ShopBooking,
  ShopBookingParam,
  ShopBookingParams,
  BookingStats,
} from './model'

/**
 * 分页查询预约订单
 */
export async function pageShopBooking(params: ShopBookingParam) {
  const res = await request.get<ApiResult<PageResult<ShopBooking>>>(
    '/shop/shop-booking/page',
    params
  )
  if (res.code === 0) {
    return res.data
  }
  return Promise.reject(new Error(res.message))
}

/**
 * 查询预约订单列表
 */
export async function listShopBooking(params?: ShopBookingParam) {
  const res = await request.get<ApiResult<ShopBooking[]>>(
    '/shop/shop-booking',
    params
  )
  if (res.code === 0 && res.data) {
    return res.data
  }
  return Promise.reject(new Error(res.message))
}

/**
 * 根据ID查询预约订单
 */
export async function getShopBooking(id: string) {
  const res = await request.get<ApiResult<ShopBooking>>(
    '/shop/shop-booking/' + id
  )
  if (res.code === 0 && res.data) {
    return res.data
  }
  return Promise.reject(new Error(res.message))
}

/**
 * 创建预约订单
 */
export async function createShopBooking(data: ShopBookingParams) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-booking',
    data
  )
  if (res.code === 0) {
    return res.message
  }
  return Promise.reject(new Error(res.message))
}

/**
 * 修改预约订单
 */
export async function updateShopBooking(data: ShopBookingParams) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-booking',
    data
  )
  if (res.code === 0) {
    return res.message
  }
  return Promise.reject(new Error(res.message))
}

/**
 * 取消预约订单
 */
export async function cancelShopBooking(id?: string) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-booking/' + id
  )
  if (res.code === 0) {
    return res.message
  }
  return Promise.reject(new Error(res.message))
}

/**
 * 改签预约订单
 */
export async function rescheduleShopBooking(data: {
  bookingId: string
  newDate: string
  newTime: string
}) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-booking/reschedule',
    data
  )
  if (res.code === 0) {
    return res.message
  }
  return Promise.reject(new Error(res.message))
}

/**
 * 获取预约统计
 */
export async function getBookingStats() {
  const res = await request.get<ApiResult<BookingStats>>(
    '/shop/shop-booking/stats'
  )
  if (res.code === 0 && res.data) {
    return res.data
  }
  return Promise.reject(new Error(res.message))
}
