import 'dart:math';
import '../enums.dart';
import '../models.dart';
import '../data.dart';
import '../config.dart';

class StageManager {
  /// Determines the stage type based on the stage number and configured intervals.
  static StageType getStageTypeFor(int stageNumber) {
    if (stageNumber % GameConfig.eliteStageInterval == 0) {
      return StageType.elite;
    } else if (stageNumber % GameConfig.shopStageInterval == 0) {
      return StageType.shop;
    } else if (stageNumber % GameConfig.restStageInterval == 0) {
      return StageType.rest;
    }
    return StageType.battle;
  }

  /// Generates a Character instance for the enemy in the current stage.
  static Character generateEnemy(int stage, StageType type) {
    bool isElite = type == StageType.elite;
    
    // For non-battle stages, return dummy characters (Merchants/Campfires)
    if (type == StageType.shop) {
      return Character(name: "Merchant", maxHp: 9999, armor: 0, atk: 0);
    } else if (type == StageType.rest) {
      return Character(name: "Campfire", maxHp: 9999, armor: 0, atk: 0);
    }

    // Normal or Elite Battle
    EnemyTemplate template = EnemyTable.getRandomEnemy(
      stage: stage,
      isElite: isElite,
    );
    return template.createCharacter(stage: stage);
  }

  /// Generates 3 item reward options based on the current stage and tier.
  static List<Item> generateRewardOptions(int stage, StageType currentStageType) {
    ItemTier currentTier = _getTierForStage(stage);
    List<Item> rewardOptions = [];

    bool isElite = currentStageType == StageType.elite;
    bool isTier1 = currentTier == ItemTier.tier1;

    for (int i = 0; i < 3; i++) {
      ItemRarity? minRarity;
      ItemRarity? maxRarity;

      if (isElite && i == 0) {
        if (isTier1) {
          minRarity = ItemRarity.rare;
          maxRarity = ItemRarity.rare;
        } else {
          minRarity = ItemRarity.legendary;
        }
      } else {
        if (isTier1) {
          maxRarity = ItemRarity.magic;
        }
      }

      ItemTemplate? item = ItemTable.getRandomItem(
        tier: currentTier,
        minRarity: minRarity,
        maxRarity: maxRarity,
      );

      if (item != null) {
        rewardOptions.add(item.createItem(stage: stage));
      }
    }

    rewardOptions.add(_createSkipRewardItem());
    return rewardOptions;
  }

  /// Calculates the gold reward for defeating an enemy.
  static int calculateGoldReward(int stage, Random random) {
    return GameConfig.baseGoldReward +
        (stage * GameConfig.goldRewardPerStage) +
        random.nextInt(GameConfig.goldRewardVariance);
  }

  static ItemTier _getTierForStage(int stage) {
    if (stage > GameConfig.tier2StageMax) {
      return ItemTier.tier3;
    } else if (stage > GameConfig.tier1StageMax) {
      return ItemTier.tier2;
    }
    return ItemTier.tier1;
  }

  static Item _createSkipRewardItem() {
    return Item(
      id: "reward_skip",
      name: "Skip Reward",
      description: "Take nothing and move on.",
      atkBonus: 0,
      hpBonus: 0,
      slot: EquipmentSlot.accessory,
    );
  }
}
