// Force a specific mitra OFFLINE via the dev-only
// /internal/_test/force-mitra-offline endpoint. Used by Maestro flows that
// need the home screen to render its OFFLINE variant on app launch (e.g.
// ts-mitra-1-02-home_offline_renders.yaml) without driving the toggle from
// the UI.
//
// Reads MITRA_ID from env (typically `${output.MITRA_ID}` from a prior
// seed_mitra.js run) and BACKEND_INTERNAL_URL.
const mitraId = MITRA_ID
const url = BACKEND_INTERNAL_URL || 'http://localhost:3001'
if (!mitraId) {
  throw new Error('MITRA_ID env not set — pass output.MITRA_ID from seed_mitra.js')
}
// Ensure a status row exists first — force-mitra-offline 404s if there's no
// row yet (a freshly-seeded mitra has none until they sign in once). Cheapest
// way is reset-all-mitras-online: idempotent, upserts every mitra to online,
// then we flip the one we care about offline immediately below.
const ensureOnline = http.post(`${url}/internal/_test/reset-all-mitras-online`, {
  headers: { 'Content-Type': 'application/json' },
  body: '{}',
})
if (ensureOnline.status !== 200) {
  throw new Error(`reset-all-mitras-online failed (${ensureOnline.status}): ${ensureOnline.body}`)
}
const resp = http.post(`${url}/internal/_test/force-mitra-offline`, {
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ mitra_id: mitraId }),
})
if (resp.status !== 200) {
  throw new Error(`force-mitra-offline failed (${resp.status}): ${resp.body}`)
}
