import 'package:flutter_test/flutter_test.dart';
import 'package:game_test/game/enums.dart';
import 'package:game_test/game/models.dart';
import 'package:game_test/providers/battle_provider.dart';
import 'package:game_test/providers/shop_provider.dart';

void main() {
  late BattleProvider battleProvider;

  setUp(() {
    battleProvider = BattleProvider(shopProvider: ShopProvider());
    battleProvider.player = Character(
      name: 'Warrior',
      hp: 50,
      maxHp: 100,
      armor: 0,
      atk: 10,
      baseDefense: 5,
    );
  });

  test('healing potion restores HP and is consumed', () async {
    final potion = Item(
      id: 'potion_heal_small',
      name: 'Healing Potion',
      description: 'Restores HP.',
      atkBonus: 0,
      hpBonus: 20,
      slot: EquipmentSlot.consumable,
    );
    final healEvents = <HealEvent>[];
    final subscription = battleProvider.healStream.listen(healEvents.add);
    battleProvider.player.addToInventory(potion);

    battleProvider.useConsumable(potion);
    await Future<void>.delayed(Duration.zero);

    expect(battleProvider.player.hp, 70);
    expect(battleProvider.player.inventory, isNot(contains(potion)));
    expect(healEvents.single.amount, 20);

    await subscription.cancel();
  });

  test('armor potion grants armor and is consumed', () {
    final potion = Item(
      id: 'potion_armor_small',
      name: 'Iron Skin Potion',
      description: 'Grants armor.',
      atkBonus: 0,
      hpBonus: 0,
      armorBonus: 10,
      slot: EquipmentSlot.consumable,
    );
    battleProvider.player.addToInventory(potion);

    battleProvider.useConsumable(potion);

    expect(battleProvider.player.armor, 10);
    expect(battleProvider.player.inventory, isNot(contains(potion)));
  });

  test('strength potion applies attack buff and is consumed', () {
    final potion = Item(
      id: 'potion_strength_small',
      name: 'Strength Potion',
      description: 'Increases attack.',
      atkBonus: 0,
      hpBonus: 0,
      slot: EquipmentSlot.consumable,
      effects: [
        ItemEffect(
          type: StatusEffectType.attackUp,
          probability: 100,
          duration: 1,
          value: 5,
        ),
      ],
    );
    battleProvider.player.addToInventory(potion);

    battleProvider.useConsumable(potion);

    expect(battleProvider.player.hasStatus(StatusEffectType.attackUp), isTrue);
    expect(battleProvider.player.totalAtk, 15);
    expect(battleProvider.player.inventory, isNot(contains(potion)));
  });
}
