/**
 * T0 (v0.8) — manual provisioning CLI. `node bin/provision.ts <subdomain>` mints an account
 * in the flat store and prints the raw agent/client tokens ONCE (they are never stored raw).
 * This is the throwaway MVP admin path; T11 promotes it to an authenticated HTTP API.
 */
import { createFlatStore } from '../src/flat-store.js'

async function main(): Promise<void> {
  const subdomain = process.argv[2]
  if (subdomain === undefined || subdomain.trim() === '') {
    process.stderr.write('usage: provision <subdomain>\n')
    process.exitCode = 2
    return
  }
  const store = createFlatStore()
  const { accountId, agentToken, clientToken } = await store.provisionFlat(subdomain.trim())
  // Raw tokens printed ONCE for the operator to copy; they are not recoverable afterwards.
  process.stdout.write(
    JSON.stringify({ accountId, subdomain: subdomain.trim(), agentToken, clientToken }, null, 2) + '\n',
  )
}

main().catch((err: unknown) => {
  process.stderr.write(`provision failed: ${err instanceof Error ? err.message : String(err)}\n`)
  process.exitCode = 1
})
