import { api } from './client'

function toQuery(params) {
  const q = new URLSearchParams()
  for (const [key, value] of Object.entries(params)) {
    if (value !== null && value !== undefined && value !== '') {
      q.set(key, String(value))
    }
  }
  const s = q.toString()
  return s ? `?${s}` : ''
}

export function fetchAuditLogs({
  entityType,
  entityId,
  userId,
  action,
  from,
  to,
  page = 1,
  pageSize = 50,
} = {}) {
  return api.get(`/api/v1/audit-logs${toQuery({
    entityType,
    entityId,
    userId,
    action,
    from,
    to,
    page,
    pageSize,
  })}`)
}

export function fetchPhiAccessLogs({
  patientId,
  userId,
  accessType,
  from,
  to,
  page = 1,
  pageSize = 50,
} = {}) {
  return api.get(`/api/v1/phi-access-logs${toQuery({
    patientId,
    userId,
    accessType,
    from,
    to,
    page,
    pageSize,
  })}`)
}
