import { expect } from "@esm-bundle/chai";
import sinon from "sinon";
import { MissionManager } from "../../src/managers/MissionManager.js";

describe("Manager: MissionManager - SURVIVE Objective Bug", () => {
  let manager;
  let mockPersistence;

  beforeEach(() => {
    mockPersistence = {
      loadUnlocks: sinon.stub().resolves([]),
      saveUnlocks: sinon.stub().resolves(),
    };
    manager = new MissionManager(mockPersistence);
  });

  it("Should complete SURVIVE objective when turn limit is reached (using turn_limit property)", async () => {
    // Mock a mission with SURVIVE objective using turn_limit (like in the JSON files)
    manager.currentMissionDef = {
      id: "MISSION_TEST",
      objectives: {
        primary: [
          {
            type: "SURVIVE",
            turn_limit: 5, // Using turn_limit as found in JSON
            current: 0,
            complete: false,
          },
        ],
      },
      rewards: { guaranteed: {} },
    };

    manager.activeMissionId = "MISSION_TEST";
    manager.currentObjectives = manager.currentMissionDef.objectives.primary;
    manager.currentTurn = 0;

    // Simulate turns passing
    manager.updateTurn(5);
    manager.onGameEvent("TURN_END", { turn: 5 });

    // Should be complete
    expect(
      manager.currentObjectives[0].complete,
      "Objective should be complete after 5 turns"
    ).to.be.true;
  });

  it("Should unlock procedural missions when reward is granted", async () => {
    // Setup
    const unlockStr = "UNLOCK_PROCEDURAL_MISSIONS";
    manager.currentMissionDef = {
      rewards: {
        guaranteed: {
          unlocks: [unlockStr],
        },
      },
    };

    // Stub unlockClasses to verify it's called
    manager.unlockClasses = sinon.stub();
    // Stub refreshProceduralMissions
    manager.refreshProceduralMissions = sinon.stub();

    // Act
    manager.distributeRewards();

    // Assert
    expect(manager.proceduralMissionsUnlocked).to.be.true;
    expect(manager.unlockClasses.calledWith([unlockStr])).to.be.true;
    expect(manager.refreshProceduralMissions.called).to.be.true;
  });
});
