import request from '@/utils/request'

/** 可问诊兽医分页列表 */
export function listOnlineConsultVets(params) {
  return request({
    url: '/app/onlineConsult/vet/list',
    method: 'GET',
    params
  })
}

/** 打开或复用与指定兽医的问诊会话 */
export function openOnlineConsultSession(data) {
  return request({
    url: '/app/onlineConsult/session/open',
    method: 'POST',
    header: { repeatSubmit: false },
    data
  })
}

/** 历史消息 */
export function listOnlineConsultMessages(sessionId, params) {
  return request({
    url: `/app/onlineConsult/session/${sessionId}/messages`,
    method: 'GET',
    params
  })
}

/** 发送消息 */
export function sendOnlineConsultMessage(sessionId, data) {
  return request({
    url: `/app/onlineConsult/session/${sessionId}/message`,
    method: 'POST',
    header: { repeatSubmit: false },
    data
  })
}

/** 隐藏会话（问诊人侧删除列表项） */
export function hideOnlineConsultSession(sessionId) {
  return request({
    url: `/app/onlineConsult/session/${sessionId}/hide`,
    method: 'POST',
    header: { repeatSubmit: false }
  })
}

/** 问诊人消息 Tab：兽医问诊会话列表（M3） */
export function listAskerConsultSessions(params) {
  return request({
    url: '/app/onlineConsult/session/list',
    method: 'GET',
    params
  })
}

/** 问诊人删除会话（对兽医隐藏，M4） */
export function hideAskerConsultSession(sessionId) {
  return request({
    url: `/app/consult/vet/session/${sessionId}/hide`,
    method: 'POST',
    header: { repeatSubmit: false }
  })
}

/** 列表项资料缓存 key（无单独详情接口，供 vet-profile 读取） */
export function vetProfileCacheKey(vetResourceId) {
  return `oc_vet_profile_${vetResourceId}`
}

export function saveVetProfileCache(vetResourceId, row) {
  if (!vetResourceId || !row) return
  try {
    uni.setStorageSync(vetProfileCacheKey(vetResourceId), JSON.stringify(row))
  } catch (e) {
    /* noop */
  }
}

export function loadVetProfileCache(vetResourceId) {
  if (!vetResourceId) return null
  try {
    const raw = uni.getStorageSync(vetProfileCacheKey(vetResourceId))
    if (!raw) return null
    return typeof raw === 'string' ? JSON.parse(raw) : raw
  } catch (e) {
    return null
  }
}
