// Priority: 1
/**
 * ════════════════════════════════════════════════════════════════
 *
 *                   █████╗  ██████╗ ███████╗
 *                  ██╔══██╗██╔═══██╗██╔════╝
 *                  ███████║██║   ██║█████╗
 *                  ██╔══██║██║   ██║██╔══╝
 *                  ██║  ██║╚██████╔╝██║
 *                  ╚═╝  ╚═╝ ╚═════╝ ╚═╝
 *
 *                       Age of Fate
 *                   A Minecraft Modpack
 *                      By KnightDexx
 * ════════════════════════════════════════════════════════════════
 *
 * ┌─────────────────────────────────────────────────────────┐
 * │             FISHING LOOT BALANCE SYSTEM                 │
 * ├─────────────────────────────────────────────────────────┤
 * │ @file        05_fishing_balance.js                      │
 * │ @copyright   KnightDexx's Modded Minecraft World        │
 * │ @modpack     Age of Fate (AOF)                          │
 * │ @version     2.1 - 1.21.1 NeoForge Port                │
 * │                                                          │
 * │ PORTED: 1.20.1 → 1.21.1 NeoForge                       │
 * │                                                          │
 * │ BREAKING CHANGES FIXED:                                  │
 * │   1. LootJS API: addLootTableModifier → modifyLootTables│
 * │   2. Item.of('...').withChance() was REMOVED in         │
 * │      LootJS for 1.21. The addWeightedLoot() API now     │
 * │      uses LootEntry.of(item).weight(n) for weighting.   │
 * │      OLD: Item.of('aquaculture:cod').withChance(15)     │
 * │      NEW: LootEntry.of('aquaculture:cod').withWeight(15)    │
 * └─────────────────────────────────────────────────────────┘
 *
**/

// ═══════════════════════════════════════════════════════════
// FISHING LOOT BALANCE - REMOVE OP ITEMS
// ═══════════════════════════════════════════════════════════

// ═══════════════════════════════════════════════════════════
// LOCAL CONFIG (self-contained — no global dependency)
// LootJS.modifiers() fires at a different time than script
// load order, so global.AOF_LOOT is not reliably available.
// ═══════════════════════════════════════════════════════════

const FISHING_CONFIG = {
  ENABLED: true, // Set to false to disable fishing loot balance

  USE_CHEST_BLACKLIST: true,

  BLACKLIST: {
    MODS: [
      'allthemodium',
      'allthecompressed'
    ],
    ITEMS: [
      'minecraft:enchanted_golden_apple',
      'minecraft:netherite_ingot',
      'minecraft:netherite_scrap',
      'minecraft:elytra',
      'minecraft:shulker_box'
    ],
    PATTERNS: [
      /.*:.*_pickaxe/,
      /.*:.*_axe/,
      /.*:.*_shovel/,
      /.*:.*_hoe/,
      /.*:.*_sword/,
      /.*:.*_helmet/,
      /.*:.*_chestplate/,
      /.*:.*_leggings/,
      /.*:.*_boots/
    ]
  },

  FISHING: {
    USE_CHEST_BLACKLIST: true,
    EXTRA_BLACKLIST: [
      'aquaculture:neptunium_axe',
      'aquaculture:neptunium_pickaxe',
      'aquaculture:neptunium_shovel',
      'aquaculture:neptunium_hoe',
      'aquaculture:neptunium_sword',
      'aquaculture:neptunium_helmet',
      'aquaculture:neptunium_chestplate',
      'aquaculture:neptunium_leggings',
      'aquaculture:neptunium_boots',
      'aquaculture:neptunium_bow',
      'aquaculture:neptunium_fishing_rod',
      'aquaculture:neptunium_fillet_knife'
    ]
  }
}

LootJS.modifiers((event) => {

  if (!FISHING_CONFIG.ENABLED) {
    console.info('[AOF] Fishing Balance: DISABLED (skipping)')
    return
  }

  console.info('═══════════════════════════════════════════════════════')
  console.info('[AOF] Fishing Loot Balance System v2.1')
  console.info('[AOF] Removing OP items from ALL fishing loot...')
  console.info('[AOF] Neptune gear now drops from water creatures!')
  console.info('═══════════════════════════════════════════════════════')

  const config = FISHING_CONFIG

  const fishingTables = [
    /minecraft:gameplay\/fishing\/.*/,
    /aquaculture:gameplay\/fishing\/.*/
  ]

  // ───────────────────────────────────────────────────────────
  // REMOVE BLACKLISTED MODS
  // PORTED: addLootTableModifier → modifyLootTables
  // ───────────────────────────────────────────────────────────

  if (config.FISHING.USE_CHEST_BLACKLIST) {
    config.BLACKLIST.MODS.forEach(mod => {

      fishingTables.forEach(table => {
        event.addTableModifier(table)
          .removeLoot(new RegExp(`${mod}:.*`))
      })
      console.info(`[AOF] ✓ Removed all items from: ${mod}`)
    })
  }

  // ───────────────────────────────────────────────────────────
  // REMOVE SPECIFIC BLACKLISTED ITEMS
  // ───────────────────────────────────────────────────────────

  if (config.FISHING.USE_CHEST_BLACKLIST) {
    config.BLACKLIST.ITEMS.forEach(item => {
      fishingTables.forEach(table => {
        event.addTableModifier(table)
          .removeLoot(item)
      })
      console.info(`[AOF] ✓ Removed item: ${item}`)
    })
  }

  // ───────────────────────────────────────────────────────────
  // REMOVE PATTERN-MATCHED ITEMS (Tools/Weapons/Armor)
  // ───────────────────────────────────────────────────────────

  if (config.FISHING.USE_CHEST_BLACKLIST) {
    config.BLACKLIST.PATTERNS.forEach(pattern => {
      fishingTables.forEach(table => {
        event.addTableModifier(table)
          .removeLoot(pattern)
      })
    })
    console.info('[AOF] ✓ Removed all tools/weapons/armor patterns')
  }

  // ───────────────────────────────────────────────────────────
  // AQUACULTURE 2 - EXTRA BLACKLIST (Neptunium Gear)
  // ───────────────────────────────────────────────────────────

  if (config.FISHING.EXTRA_BLACKLIST) {
    config.FISHING.EXTRA_BLACKLIST.forEach(item => {
      fishingTables.forEach(table => {
        event.addTableModifier(table)
          .removeLoot(item)
      })
    })
    console.info('[AOF] ✓ Removed Aquaculture 2 neptunium gear from fishing')
  }

  // ───────────────────────────────────────────────────────────
  // REMOVE NEPTUNE'S BOUNTY FROM FISHING
  // ───────────────────────────────────────────────────────────

  fishingTables.forEach(table => {
    event.addTableModifier(table)
      .removeLoot('aquaculture:neptunes_bounty')
  })
  console.info("[AOF] ✓ Removed Neptune's Bounty from fishing")
  console.info('[AOF]   (Now drops from water creatures instead!)')

  // ───────────────────────────────────────────────────────────
  // REMOVE VANILLA TREASURE FROM NEPTUNIUM FISHING
  // ───────────────────────────────────────────────────────────

  event.addTableModifier('aquaculture:gameplay/fishing/neptunium')
    .removeLoot('minecraft:bow')
    .removeLoot('minecraft:fishing_rod')
    .removeLoot('minecraft:enchanted_book')
    .removeLoot('minecraft:name_tag')
    .removeLoot('minecraft:nautilus_shell')
    .removeLoot('minecraft:saddle')

  console.info('[AOF] ✓ Removed vanilla treasure from neptunium fishing')

  console.info('═══════════════════════════════════════════════════════')
  console.info('[AOF] Fishing Balance Complete!')
  console.info("[AOF] ✓ Neptune's Bounty removed from fishing")
  console.info('[AOF] ✓ Neptunium gear removed from fishing')
  console.info('[AOF] ✓ Vanilla treasure disabled')
  console.info('[AOF] 🎣 Fishing = Basic materials only')
  console.info('[AOF] 🐟 Water Creatures = Neptune progression')
  console.info('═══════════════════════════════════════════════════════')

})

// ═══════════════════════════════════════════════════════════
// ADD CUSTOM AQ FISH POOL
// addWeightedLoot() does not exist in LootJS 1.21.
// Correct API: LootJS.lootTables() + pool.addEntry(LootEntry.of(...).weight(n))
// ═══════════════════════════════════════════════════════════

if (FISHING_CONFIG.ENABLED) {
  LootJS.lootTables(event => {
    const fishTable = event.getLootTable('aquaculture:gameplay/fishing/fish')
    if (!fishTable) {
      console.warn('[AOF] Could not find aquaculture fish loot table - skipping custom pool')
      return
    }
    fishTable.createPool(pool => {
      pool.rolls(1)
      pool.addEntry(LootEntry.of('aquaculture:atlantic_cod').withWeight(15))
      pool.addEntry(LootEntry.of('aquaculture:blackfish').withWeight(40))
      pool.addEntry(LootEntry.of('aquaculture:pacific_halibut').withWeight(10))
      pool.addEntry(LootEntry.of('aquaculture:atlantic_halibut').withWeight(8))
      pool.addEntry(LootEntry.of('aquaculture:atlantic_herring').withWeight(60))
      pool.addEntry(LootEntry.of('aquaculture:pink_salmon').withWeight(20))
      pool.addEntry(LootEntry.of('aquaculture:pollock').withWeight(25))
      pool.addEntry(LootEntry.of('aquaculture:rainbow_trout').withWeight(20))
      pool.addEntry(LootEntry.of('aquaculture:bayad').withWeight(20))
      pool.addEntry(LootEntry.of('aquaculture:boulti').withWeight(30))
      pool.addEntry(LootEntry.of('aquaculture:capitaine').withWeight(7))
      pool.addEntry(LootEntry.of('aquaculture:synodontis').withWeight(60))
      pool.addEntry(LootEntry.of('aquaculture:smallmouth_bass').withWeight(30))
      pool.addEntry(LootEntry.of('aquaculture:bluegill').withWeight(60))
      pool.addEntry(LootEntry.of('aquaculture:brown_trout').withWeight(30))
      pool.addEntry(LootEntry.of('aquaculture:carp').withWeight(30))
      pool.addEntry(LootEntry.of('aquaculture:catfish').withWeight(10))
      pool.addEntry(LootEntry.of('aquaculture:gar').withWeight(20))
      pool.addEntry(LootEntry.of('aquaculture:minnow').withWeight(55))
      pool.addEntry(LootEntry.of('aquaculture:muskellunge').withWeight(10))
      pool.addEntry(LootEntry.of('aquaculture:perch').withWeight(60))
      pool.addEntry(LootEntry.of('aquaculture:arapaima').withWeight(8))
      pool.addEntry(LootEntry.of('aquaculture:piranha').withWeight(40))
      pool.addEntry(LootEntry.of('aquaculture:tambaqui').withWeight(20))
      pool.addEntry(LootEntry.of('aquaculture:brown_shrooma').withWeight(50))
      pool.addEntry(LootEntry.of('aquaculture:red_shrooma').withWeight(50))
      pool.addEntry(LootEntry.of('aquaculture:jellyfish').withWeight(30))
      pool.addEntry(LootEntry.of('aquaculture:red_grouper').withWeight(20))
      pool.addEntry(LootEntry.of('aquaculture:tuna').withWeight(10))
      pool.addEntry(LootEntry.of('aquaculture:leech').withWeight(30))
      pool.addEntry(LootEntry.of('aquaculture:arrau_turtle').withWeight(5))
      pool.addEntry(LootEntry.of('aquaculture:box_turtle').withWeight(5))
      pool.addEntry(LootEntry.of('aquaculture:starshell_turtle').withWeight(5))
    })
    console.info('[AOF] ✓ Custom AQ fish pool injected via LootJS.lootTables()')
  })
}
