// Lobby structure placement on first player join

const DATA_KEY_DONE = 'AscendraLobbyPlaced'
const DATA_KEY_PHASE = 'AscendraLobbyPhase'
const DATA_KEY_TIMER = 'AscendraLobbyTimer'

const PHASE_IDLE          = 0
const PHASE_INITIAL_DELAY = 1
const PHASE_WAIT_CHUNKS   = 2
const PHASE_PLACE_LOBBY   = 3
const PHASE_PLACE_LOBBER  = 4
const PHASE_SPAWN_NPC     = 5

const INITIAL_DELAY_TICKS = 400 // ~20s nach erstem Join
const CHUNK_GEN_TICKS     = 60  // 3s warten bis forceload-Chunks generiert sind
const PHASE_GAP_TICKS     = 20  // 1s Abstand zwischen Place-Operationen

PlayerEvents.loggedIn(event => {
  const data = event.server.persistentData
  if (data.getBoolean(DATA_KEY_DONE)) return
  if (data.getInt(DATA_KEY_PHASE) !== PHASE_IDLE) return

  data.putInt(DATA_KEY_PHASE, PHASE_INITIAL_DELAY)
  data.putInt(DATA_KEY_TIMER, INITIAL_DELAY_TICKS)
})

ServerEvents.tick(event => {
  const data = event.server.persistentData
  if (data.getBoolean(DATA_KEY_DONE)) return

  const phase = data.getInt(DATA_KEY_PHASE)
  if (phase === PHASE_IDLE) return

  const timer = data.getInt(DATA_KEY_TIMER)
  if (timer > 0) {
    data.putInt(DATA_KEY_TIMER, timer - 1)
    return
  }

  const server = event.server

  switch (phase) {
    case PHASE_INITIAL_DELAY: {
      console.info('[KubeJS] Forceloading chunks...')
      // Lobby-Bereich (ca. 3x3 Chunks)
      server.runCommandSilent('execute in ascendra:lobby run forceload add -20 -20 20 20')
      // Lobber-Bereich auf 2x2 Chunks reduziert (statt 8x8)
      server.runCommandSilent('forceload add 320 320 352 352')
      data.putInt(DATA_KEY_PHASE, PHASE_WAIT_CHUNKS)
      data.putInt(DATA_KEY_TIMER, CHUNK_GEN_TICKS)
      break
    }
    case PHASE_WAIT_CHUNKS: {
      data.putInt(DATA_KEY_PHASE, PHASE_PLACE_LOBBY)
      data.putInt(DATA_KEY_TIMER, 0)
      break
    }
    case PHASE_PLACE_LOBBY: {
      console.info('[KubeJS] Placing lobby structure...')
      const result = server.runCommandSilent(
        'execute in ascendra:lobby run place template ascendra:lobby 0 63 0 none'
      )
      if (result > 0) {
        data.putInt(DATA_KEY_PHASE, PHASE_PLACE_LOBBER)
        data.putInt(DATA_KEY_TIMER, PHASE_GAP_TICKS)
      } else {
        console.error('[KubeJS] Failed to place lobby!')
        // Reset für nächsten Join-Versuch
        data.putInt(DATA_KEY_PHASE, PHASE_IDLE)
      }
      break
    }
    case PHASE_PLACE_LOBBER: {
      console.info('[KubeJS] Placing lobber structure...')
      server.runCommandSilent(
        'execute positioned 333 0 333 positioned over world_surface run place structure kubejs:lobber'
      )
      data.putInt(DATA_KEY_PHASE, PHASE_SPAWN_NPC)
      data.putInt(DATA_KEY_TIMER, PHASE_GAP_TICKS)
      break
    }
    case PHASE_SPAWN_NPC: {
      console.info('[KubeJS] Spawning shop NPC...')
      server.runCommandSilent(
        'spawnnpc ae9f0f39-2338-4b5f-aa25-0308cf01ed30 "Item The Shop" ascendra:lobby 7.5 64 20.5 180 0'
      )
      console.info('[KubeJS] Lobby setup complete')
      data.putBoolean(DATA_KEY_DONE, true)
      data.putInt(DATA_KEY_PHASE, PHASE_IDLE)
      break
    }
  }
})