import { expect } from "@esm-bundle/chai";
import { ItemRegistry, itemRegistry } from "../../src/managers/ItemRegistry.js";

describe("Manager: ItemRegistry", () => {
  let registry;

  beforeEach(() => {
    // Create a new instance for each test to avoid state pollution
    registry = new ItemRegistry();
  });

  describe("loadAll", () => {
    it("should load items from tier1_gear.json", async () => {
      await registry.loadAll();

      expect(registry.items.size).to.be.greaterThan(0);
    });

    it("should create Item instances for each item definition", async () => {
      await registry.loadAll();

      const item = registry.get("ITEM_RUSTY_BLADE");
      expect(item).to.exist;
      expect(item.id).to.equal("ITEM_RUSTY_BLADE");
      expect(item.name).to.equal("Rusty Infantry Blade");
      expect(item.type).to.equal("WEAPON");
    });

    it("should handle multiple calls to loadAll without duplicate loading", async () => {
      const promise1 = registry.loadAll();
      const promise2 = registry.loadAll();

      await Promise.all([promise1, promise2]);

      // Should only load once
      expect(registry.items.size).to.be.greaterThan(0);
    });

    it("should load items with stats", async () => {
      await registry.loadAll();

      const item = registry.get("ITEM_RUSTY_BLADE");
      expect(item.stats).to.exist;
      expect(item.stats.attack).to.equal(3);
    });

    it("should load items with requirements", async () => {
      await registry.loadAll();

      // Check if any items have requirements (may not exist in tier1_gear)
      const allItems = registry.getAll();
      // At least verify the structure is correct
      expect(allItems.length).to.be.greaterThan(0);
    });
  });

  describe("get", () => {
    it("should return item by ID after loading", async () => {
      await registry.loadAll();

      const item = registry.get("ITEM_RUSTY_BLADE");
      expect(item).to.exist;
      expect(item.id).to.equal("ITEM_RUSTY_BLADE");
    });

    it("should return undefined for non-existent item", async () => {
      await registry.loadAll();

      const item = registry.get("ITEM_NONEXISTENT");
      expect(item).to.be.undefined;
    });
  });

  describe("getAll", () => {
    it("should return array of all items", async () => {
      await registry.loadAll();

      const allItems = registry.getAll();
      expect(allItems).to.be.an("array");
      expect(allItems.length).to.equal(registry.items.size);
    });

    it("should return empty array before loading", () => {
      const allItems = registry.getAll();
      expect(allItems).to.be.an("array");
      expect(allItems.length).to.equal(0);
    });
  });

  describe("singleton instance", () => {
    it("should export singleton instance", () => {
      expect(itemRegistry).to.exist;
      expect(itemRegistry).to.be.instanceOf(ItemRegistry);
    });

    it("should share state across imports", async () => {
      // Load items in singleton
      await itemRegistry.loadAll();

      // Should have items
      expect(itemRegistry.items.size).to.be.greaterThan(0);
    });
  });
});

