import 'package:flutter_test/flutter_test.dart';
import 'package:game_test/game/data/item_table.dart';
import 'package:game_test/game/enums.dart';

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

  test('ItemTable.getRandomItem returns items correctly', () async {
    await ItemTable.load();

    // Test 1: Get Tier 1 item (Common/Rare/etc logic applies, but we just check if it returns *something* valid)
    final item = ItemTable.getRandomItem(tier: ItemTier.tier1);
    expect(item, isNotNull);
    expect(item!.tier, equals(ItemTier.tier1));
    print("Random Tier 1 Item: ${item.name} (${item.rarity.name})");

    // Test 2: Get Tier 3 item
    final item3 = ItemTable.getRandomItem(tier: ItemTier.tier3);
    expect(item3, isNotNull);
    expect(item3!.tier, equals(ItemTier.tier3));
    print("Random Tier 3 Item: ${item3.name} (${item3.rarity.name})");
    
    // Test 3: Get specific slot (Shield) from Tier 1
    // We know 'pot_lid' and 'wooden_shield' are Tier 1 shields
    final shield = ItemTable.getRandomItem(tier: ItemTier.tier1, slot: EquipmentSlot.shield);
    expect(shield, isNotNull);
    expect(shield!.slot, equals(EquipmentSlot.shield));
    expect(shield.tier, equals(ItemTier.tier1));

    // Test 4: Verify Rarity weights (Statistical test)
    // We'll run 1000 times and expect roughly correct distribution. 
    // Tier 1 has Common and Rare items mostly.
    int magicCount = 0;
    int rareCount = 0;
    
    for (int i = 0; i < 1000; i++) {
        // Tier 1 items: 
        // Magic: Rusty Dagger, Torn Tunic, Pot Lid, Wooden Shield, Old Ring, Copper Ring
        // Rare: Jagged Dagger
        // So both exist.
        final randItem = ItemTable.getRandomItem(tier: ItemTier.tier1);
        if (randItem!.rarity == ItemRarity.magic) magicCount++;
        if (randItem.rarity == ItemRarity.rare) rareCount++;
    }
    
    print("Tier 1 Stats (1000 runs): Magic=$magicCount, Rare=$rareCount");
    expect(magicCount, greaterThan(rareCount)); // Should be significantly more magic items
  });
}
