import { readFileSync, writeFileSync } from 'node:fs'
import { DatabaseSync } from 'node:sqlite'
import { catalogPayload } from '../server/catalog.mjs'
import { getProfile } from '../server/game-api.mjs'

const database = new DatabaseSync(':memory:')

try {
  database.exec(readFileSync('db/schema.sql', 'utf8'))
  database.exec(readFileSync('db/seed.sql', 'utf8'))
  const catalog = catalogPayload(getProfile(database, 1))
  writeFileSync(
    'src/offline-starter-profile.json',
    `${JSON.stringify(catalog.profile, null, 2)}\n`,
  )
  writeFileSync(
    'src/offline-catalog-meta.ts',
    `export const bundledCatalogHash = '${catalog.hash}'\n`,
  )
  console.log('Offline starter profile exported from SQLite.')
} finally {
  database.close()
}
