import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart'; // Import SharedPreferences
import 'package:game_test/providers/battle_provider.dart';
import 'package:game_test/providers/shop_provider.dart';
import 'package:game_test/game/enums.dart';
import 'package:game_test/game/data/item_table.dart';

void main() {
  group('BattleProvider Armor Reset Test', () {
    late BattleProvider battleProvider;
    late ShopProvider shopProvider;

    setUp(() {
      // Fix Binding has not yet been initialized error
      TestWidgetsFlutterBinding.ensureInitialized();
      // Mock SharedPreferences
      SharedPreferences.setMockInitialValues({});

      // Mock ItemTable data to prevent RangeError in initializeBattle
      // initializeBattle accesses indices up to 5 for weapons and 3 for shields
      ItemTable.weapons = List.generate(
        10,
        (index) => ItemTemplate(
          id: "weapon_$index",
          name: "Weapon $index",
          description: "Test Weapon",
          atkBonus: 10,
          hpBonus: 0,
          armorBonus: 0,
          slot: EquipmentSlot.weapon,
          effects: [],
          price: 10,
        ),
      );
      ItemTable.shields = List.generate(
        10,
        (index) => ItemTemplate(
          id: "shield_$index",
          name: "Shield $index",
          description: "Test Shield",
          atkBonus: 0,
          hpBonus: 0,
          armorBonus: 10,
          slot: EquipmentSlot.shield,
          effects: [],
          price: 10,
        ),
      );
      // Initialize other lists to empty to avoid null pointer if accessed loosely
      ItemTable.armors = [];
      ItemTable.accessories = [];

      shopProvider = ShopProvider();
      battleProvider = BattleProvider(shopProvider: shopProvider);
      battleProvider.initializeBattle(); // Initialize player and stage 1
    });

    test('Armor should be reset to 0 when proceeding to next stage', () {
      // 1. Setup initial state
      battleProvider.player.armor = 50;
      expect(
        battleProvider.player.armor,
        50,
        reason: "Player armor should be set to 50 initially.",
      );

      // 2. Simulate proceeding to next stage
      // Using proceedToNextStage which calls _prepareNextStage internally
      battleProvider.proceedToNextStage();

      // 3. Verify armor is reset
      expect(battleProvider.stage, 2, reason: "Stage should advance to 2.");
      expect(
        battleProvider.player.armor,
        0,
        reason: "Player armor should be reset to 0 in the new stage.",
      );
    });

    test('Armor should be reset to 0 when re-initializing battle', () {
      // 1. Setup initial state
      battleProvider.player.armor = 20;
      expect(battleProvider.player.armor, 20);

      // 2. Re-initialize
      battleProvider.initializeBattle();

      // 3. Verify armor is reset
      expect(battleProvider.stage, 1);
      expect(
        battleProvider.player.armor,
        0,
        reason: "Player armor should be reset to 0 on initialization.",
      );
    });
  });
}
