// main — boot Phaser with the kit's scenes and config.
// Read assets/manifest.json (written by tools/gen-assets.mjs) to learn
// which art exists, so we never request a missing file (no 404s / no
// console errors). No manifest -> use the styled fallback shapes.
async function probeAssets() {
  window.GAME.availableAssets = [];
  try {
    const r = await fetch('assets/manifest.json', { cache: 'no-store' });
    if (r.ok) {
      const m = await r.json();
      if (Array.isArray(m.assets)) window.GAME.availableAssets = m.assets;
    }
  } catch (e) { /* no manifest -> fallback shapes */ }
}

window.addEventListener('load', async () => {
  await probeAssets();
  const config = {
    type: Phaser.AUTO,
    parent: 'game',
    width: window.GAME.WIDTH,
    height: window.GAME.HEIGHT,
    backgroundColor: window.GAME.BG_COLOR,
    // Cap pixelRatio at 1 for locked 60fps (never 2).
    resolution: 1,
    render: { pixelArt: false, antialias: true },
    scale: {
      mode: Phaser.Scale.FIT,
      autoCenter: Phaser.Scale.CENTER_BOTH,
      width: window.GAME.WIDTH,
      height: window.GAME.HEIGHT,
    },
    physics: {
      default: 'arcade',
      arcade: { gravity: { y: 0 }, debug: false },
    },
    scene: [
      window.BootScene,
      window.PreloadScene,
      window.MenuScene,
      window.GameScene,
      window.GameOverScene,
    ],
  };

  // Force devicePixelRatio handling to 1 (no 2x oversampling).
  config.scale.zoom = 1;

  window.__game = new Phaser.Game(config);
});
