import { expect } from "@esm-bundle/chai";
import { Explorer } from "../../../src/units/Explorer.js";
import { Item } from "../../../src/items/Item.js";

// Mock Class Definitions
const CLASS_VANGUARD = {
  id: "CLASS_VANGUARD",
  name: "Vanguard",
  base_stats: { health: 120, attack: 12, defense: 8, speed: 8, magic: 0, willpower: 5, movement: 3, tech: 0 },
  growth_rates: { health: 10, attack: 1 },
  starting_equipment: ["ITEM_RUSTY_BLADE", "ITEM_SCRAP_PLATE"],
};

describe("Unit: Explorer - Starting Equipment", () => {
  let explorer;
  let mockItemRegistry;

  beforeEach(() => {
    explorer = new Explorer("p1", "Hero", "CLASS_VANGUARD", CLASS_VANGUARD);

    // Create mock item registry
    mockItemRegistry = {
      get: (defId) => {
        const items = {
          "ITEM_RUSTY_BLADE": new Item({
            id: "ITEM_RUSTY_BLADE",
            name: "Rusty Blade",
            type: "WEAPON",
            stats: { attack: 3 },
          }),
          "ITEM_SCRAP_PLATE": new Item({
            id: "ITEM_SCRAP_PLATE",
            name: "Scrap Plate",
            type: "ARMOR",
            stats: { defense: 3 },
          }),
        };
        return items[defId] || null;
      },
    };
  });

  describe("initializeStartingEquipment", () => {
    it("should equip starting equipment to appropriate slots", () => {
      explorer.initializeStartingEquipment(mockItemRegistry, CLASS_VANGUARD);

      // Weapon should be in mainHand
      expect(explorer.loadout.mainHand).to.exist;
      expect(explorer.loadout.mainHand.defId).to.equal("ITEM_RUSTY_BLADE");

      // Armor should be in body
      expect(explorer.loadout.body).to.exist;
      expect(explorer.loadout.body.defId).to.equal("ITEM_SCRAP_PLATE");
    });

    it("should create ItemInstance objects with unique UIDs", () => {
      explorer.initializeStartingEquipment(mockItemRegistry, CLASS_VANGUARD);

      expect(explorer.loadout.mainHand.uid).to.exist;
      expect(explorer.loadout.mainHand.uid).to.include("ITEM_RUSTY_BLADE");
      expect(explorer.loadout.mainHand.uid).to.include(explorer.id);
      expect(explorer.loadout.mainHand.defId).to.equal("ITEM_RUSTY_BLADE");
      expect(explorer.loadout.mainHand.quantity).to.equal(1);
    });

    it("should recalculate stats after equipping", () => {
      const initialAttack = explorer.baseStats.attack;
      
      explorer.initializeStartingEquipment(mockItemRegistry, CLASS_VANGUARD);

      // Stats should be recalculated (maxHealth should reflect equipment)
      expect(explorer.maxHealth).to.exist;
      // Attack should be increased by weapon stats (checked via maxHealth update)
    });

    it("should handle missing items gracefully", () => {
      const classDefWithMissingItem = {
        ...CLASS_VANGUARD,
        starting_equipment: ["ITEM_RUSTY_BLADE", "ITEM_NONEXISTENT"],
      };

      explorer.initializeStartingEquipment(mockItemRegistry, classDefWithMissingItem);

      // Should still equip the valid item
      expect(explorer.loadout.mainHand).to.exist;
      expect(explorer.loadout.mainHand.defId).to.equal("ITEM_RUSTY_BLADE");
    });

    it("should handle empty starting_equipment array", () => {
      const classDefNoEquipment = {
        ...CLASS_VANGUARD,
        starting_equipment: [],
      };

      explorer.initializeStartingEquipment(mockItemRegistry, classDefNoEquipment);

      // Should not throw error
      expect(explorer.loadout.mainHand).to.be.null;
      expect(explorer.loadout.body).to.be.null;
    });

    it("should handle missing starting_equipment property", () => {
      const classDefNoProperty = {
        ...CLASS_VANGUARD,
      };
      delete classDefNoProperty.starting_equipment;

      explorer.initializeStartingEquipment(mockItemRegistry, classDefNoProperty);

      // Should not throw error
      expect(explorer.loadout.mainHand).to.be.null;
    });
  });
});

