import { api } from './client'

export function fetchCurrentNews2(encounterId) {
  return api.getOptional(`/api/v1/encounters/${encounterId}/news2/current`)
}

export function fetchCurrentQsofa(encounterId) {
  return api.get(`/api/v1/encounters/${encounterId}/qsofa/current`)
}

export function fetchCurrentGcs(encounterId) {
  return api.getOptional(`/api/v1/encounters/${encounterId}/gcs`)
}

export function fetchCurrentSofa(encounterId) {
  return api.getOptional(`/api/v1/encounters/${encounterId}/sofa`)
}

export function fetchSepsisBundle(encounterId) {
  return api.getOptional(`/api/v1/encounters/${encounterId}/sepsis-bundle/current`)
}

export function fetchOrders(encounterId) {
  return api.get(`/api/v1/encounters/${encounterId}/orders`)
}

export async function submitGcsObservations(encounterId, eye, verbal, motor) {
  const recordedAt = new Date().toISOString()
  return api.post(`/api/v1/encounters/${encounterId}/observations`, {
    observations: [
      {
        observationCode: 'GCS_EYE',
        value: eye,
        unit: 'score',
        source: 'Manual',
        recordedAt,
      },
      {
        observationCode: 'GCS_VERBAL',
        value: verbal,
        unit: 'score',
        source: 'Manual',
        recordedAt,
      },
      {
        observationCode: 'GCS_MOTOR',
        value: motor,
        unit: 'score',
        source: 'Manual',
        recordedAt,
      },
    ],
  })
}

export async function fetchNews2History(encounterId, { limit = 100 } = {}) {
  const all = []
  let cursor = null
  do {
    const params = new URLSearchParams({ limit: String(limit) })
    if (cursor) params.set('cursor', cursor)
    const page = await api.get(`/api/v1/encounters/${encounterId}/news2/history?${params}`)
    all.push(...(page.items ?? []))
    cursor = page.hasMore ? page.nextCursor : null
  } while (cursor)
  return all
}

export async function fetchGcsHistory(encounterId, { limit = 100 } = {}) {
  const all = []
  let cursor = null
  do {
    const params = new URLSearchParams({ limit: String(limit) })
    if (cursor) params.set('cursor', cursor)
    const page = await api.get(`/api/v1/encounters/${encounterId}/gcs/history?${params}`)
    all.push(...(page.items ?? []))
    cursor = page.hasMore ? page.nextCursor : null
  } while (cursor)
  return all
}

export async function fetchQsofaHistory(encounterId, { limit = 100 } = {}) {
  const all = []
  let cursor = null
  do {
    const params = new URLSearchParams({ limit: String(limit) })
    if (cursor) params.set('cursor', cursor)
    const page = await api.get(`/api/v1/encounters/${encounterId}/qsofa/history?${params}`)
    all.push(...(page.items ?? []))
    cursor = page.hasMore ? page.nextCursor : null
  } while (cursor)
  return all
}

export async function fetchSofaHistory(encounterId, { limit = 100 } = {}) {
  const all = []
  let cursor = null
  do {
    const params = new URLSearchParams({ limit: String(limit) })
    if (cursor) params.set('cursor', cursor)
    const page = await api.get(`/api/v1/encounters/${encounterId}/sofa/history?${params}`)
    all.push(...(page.items ?? []))
    cursor = page.hasMore ? page.nextCursor : null
  } while (cursor)
  return all
}

export async function fetchMedications(encounterId, { pageSize = 50, since } = {}) {
  const all = []
  let page = 1
  let totalPages = 1
  do {
    const params = new URLSearchParams({ page: String(page), pageSize: String(pageSize) })
    if (since) params.set('since', since)
    const result = await api.get(`/api/v1/encounters/${encounterId}/medications?${params}`)
    all.push(...(result.items ?? []))
    totalPages = result.totalPages ?? 1
    page++
  } while (page <= totalPages)
  return all
}