import request from '@/utils/request'

/** 近 7 日日期条（含当天） */
export function listBookingDates() {
  return request({
    url: '/app/bookingService/dates',
    method: 'GET'
  })
}

/** 可预约资源列表 */
export function listBookingResources(params) {
  return request({
    url: '/app/bookingService/resource/list',
    method: 'GET',
    params
  })
}

/** 专家历史评价 */
export function listExpertBookingReviews(expertResourceId, params) {
  return request({
    url: `/app/bookingService/expert/${expertResourceId}/reviews`,
    method: 'GET',
    params
  })
}

/** 提交预约 */
export function submitBookingAppointment(data) {
  return request({
    url: '/app/bookingService/appointment',
    method: 'POST',
    header: { repeatSubmit: false },
    data
  })
}

/** 所选日期是否已有有效预约（当前登录用户） */
export function checkAppointmentBooked(params) {
  return request({
    url: '/app/bookingService/appointment/booked',
    method: 'GET',
    params
  })
}

/** 服务方类型：1 兽医 / 2 机构 / 3 专家 */
export const BOOKING_PROVIDER_TYPE = {
  VET: 1,
  ORG: 2,
  EXPERT: 3
}

export function bookingResourceCacheKey(resourceType, resourceId) {
  return `bs_resource_${resourceType}_${resourceId}`
}

export function saveBookingResourceCache(resourceType, resourceId, row) {
  if (!resourceType || !resourceId || !row) return
  try {
    uni.setStorageSync(bookingResourceCacheKey(resourceType, resourceId), JSON.stringify(row))
  } catch (e) {
    /* noop */
  }
}

export function loadBookingResourceCache(resourceType, resourceId) {
  if (!resourceType || !resourceId) return null
  try {
    const raw = uni.getStorageSync(bookingResourceCacheKey(resourceType, resourceId))
    if (!raw) return null
    return typeof raw === 'string' ? JSON.parse(raw) : raw
  } catch (e) {
    return null
  }
}
