;(() => {
  const root = global.cloudIslandKitchenMenu
  if (!root || typeof root.registerLogic !== 'function' || typeof root.registerSubmit !== 'function') return

  // 目前 Kitchen 提交规则：只检测是否持有，不消耗物品
  const REQUIRED = 1
  const DEBUG = !!(root.config && root.config.debug)

  const TASKS = [
    { logicKey: 'menu_beef_stew', itemId: 'farmersdelight:beef_stew', doneKey: 'kitchen_submit_beef_stew_done_round_' },
    { logicKey: 'menu_bone_broth', itemId: 'farmersdelight:bone_broth', doneKey: 'kitchen_submit_bone_broth_done_round_' },
    { logicKey: 'menu_chicken_soup', itemId: 'farmersdelight:chicken_soup', doneKey: 'kitchen_submit_chicken_soup_done_round_' },
    { logicKey: 'menu_dumplings', itemId: 'farmersdelight:dumplings', doneKey: 'kitchen_submit_dumplings_done_round_' },
    { logicKey: 'menu_cabbage_rolls', itemId: 'farmersdelight:cabbage_rolls', doneKey: 'kitchen_submit_cabbage_rolls_done_round_' },
    { logicKey: 'menu_fish_stew', itemId: 'farmersdelight:fish_stew', doneKey: 'kitchen_submit_fish_stew_done_round_' },
    { logicKey: 'menu_vegetable_noodles', itemId: 'farmersdelight:vegetable_noodles', doneKey: 'kitchen_submit_vegetable_noodles_done_round_' },
    { logicKey: 'menu_stuffed_pumpkin', itemId: 'farmersdelight:stuffed_pumpkin_block', doneKey: 'kitchen_submit_stuffed_pumpkin_done_round_' },
    { logicKey: 'menu_pumpkin_soup', itemId: 'farmersdelight:pumpkin_soup', doneKey: 'kitchen_submit_pumpkin_soup_done_round_' },
    { logicKey: 'menu_baked_cod_stew', itemId: 'farmersdelight:baked_cod_stew', doneKey: 'kitchen_submit_baked_cod_stew_done_round_' },
    { logicKey: 'menu_fried_rice', itemId: 'farmersdelight:fried_rice', doneKey: 'kitchen_submit_fried_rice_done_round_' },
    { logicKey: 'menu_mushroom_rice', itemId: 'farmersdelight:mushroom_rice', doneKey: 'kitchen_submit_mushroom_rice_done_round_' },
    { logicKey: 'menu_glow_berry_custard', itemId: 'farmersdelight:glow_berry_custard', doneKey: 'kitchen_submit_glow_berry_custard_done_round_' },
    { logicKey: 'menu_noodle_soup', itemId: 'farmersdelight:noodle_soup', doneKey: 'kitchen_submit_noodle_soup_done_round_' },
    { logicKey: 'menu_pasta_with_meatballs', itemId: 'farmersdelight:pasta_with_meatballs', doneKey: 'kitchen_submit_pasta_with_meatballs_done_round_' },
    { logicKey: 'menu_smoked_ham', itemId: 'farmersdelight:smoked_ham', doneKey: 'kitchen_submit_smoked_ham_done_round_' },
    { logicKey: 'menu_ratatouille', itemId: 'farmersdelight:ratatouille', doneKey: 'kitchen_submit_ratatouille_done_round_' },
    { logicKey: 'menu_pasta_with_mutton_chop', itemId: 'farmersdelight:pasta_with_mutton_chop', doneKey: 'kitchen_submit_pasta_with_mutton_chop_done_round_' },
    { logicKey: 'menu_squid_ink_pasta', itemId: 'farmersdelight:squid_ink_pasta', doneKey: 'kitchen_submit_squid_ink_pasta_done_round_' },
  ]

  function getTaskId() {
    const cfg = root.config || {}
    return String(cfg.rotationTaskId || '')
  }

  TASKS.forEach(function(task) {
    root.registerLogic(task.logicKey, player => {
      const taskId = getTaskId()
      if (!taskId) return false

      const data = player.persistentData
      const round = data.getLong(`kitchen_rot_round_${taskId}`)
      const doneRound = data.getLong(`${task.doneKey}${taskId}`)
      return round > 0 && doneRound === round
    })

    root.registerSubmit(task.logicKey, player => {
      const taskId = getTaskId()
      if (!taskId) {
        if (DEBUG) player.tell(`§8[KitchenDebug] ${task.logicKey}: 0 taskId§r`)
        return 'failed'
      }

      const data = player.persistentData
      const round = data.getLong(`kitchen_rot_round_${taskId}`)
      const doneRoundKey = `${task.doneKey}${taskId}`

      if (DEBUG) player.tell(`§8[KitchenDebug] ${task.logicKey}: round=${round}, done=${data.getLong(doneRoundKey)}§r`)
      if (round > 0 && data.getLong(doneRoundKey) === round) {
        if (DEBUG) player.tell(`§8[KitchenDebug] ${task.logicKey}: already done this round§r`)
        return 'failed'
      }

      const server = player.server
      if (!server) return 'failed'
      // 用 clear ... 0 仅做数量检测（不会清除背包）
      const count = Number(server.runCommandSilent(`clear ${player.username} ${task.itemId} 0`))
      if (DEBUG) player.tell(`§8[KitchenDebug] ${task.logicKey}: count=${count}§r`)
      if (!isFinite(count) || count < REQUIRED) return 'failed'

      data.putLong(doneRoundKey, round)
      if (DEBUG) player.tell(`§8[KitchenDebug] ${task.logicKey}: submit success§r`)
      return 'submitted'
    })
  })
})()
