import { describe, it, expect, beforeEach } from "vitest";
import { createContextManager, EcapContext } from "./context-manager";

describe("PrimariaContextManager", () => {
  let contextManager: ReturnType<typeof createContextManager>;

  beforeEach(() => {
    contextManager = createContextManager();
    // Reset context before each test
    contextManager.initializeContext({});
  });

  describe("initializeContext", () => {
    it("should initialize context with provided data", () => {
      const context: EcapContext = {
        visi_id: "12345",
        signador: "Dr. Smith",
        campanya: "2024-Q1",
      };

      contextManager.initializeContext(context);

      expect(contextManager.getContext()).toEqual(context);
    });

    it("should handle partial context data", () => {
      const context: EcapContext = {
        visi_id: "12345",
      };

      contextManager.initializeContext(context);
      const result = contextManager.getContext();

      expect(result.visi_id).toBe("12345");
      expect(result.signador).toBeUndefined();
      expect(result.campanya).toBeUndefined();
    });
  });

  describe("getContext", () => {
    it("should return a read-only copy of context", () => {
      const context: EcapContext = {
        visi_id: "12345",
        signador: "Dr. Smith",
      };

      contextManager.initializeContext(context);
      const returnedContext = contextManager.getContext();

      expect(returnedContext).toEqual(context);
      expect(returnedContext).not.toBe(context); // Ensure it's a copy
    });

    it("should return empty object if not initialized", () => {
      const context = contextManager.getContext();
      expect(context).toEqual({});
    });
  });

  describe("context properties access", () => {
    it("should access visit ID through context", () => {
      contextManager.initializeContext({ visi_id: "12345" });
      const context = contextManager.getContext();
      expect(context.visi_id).toBe("12345");
    });

    it("should access signador through context", () => {
      contextManager.initializeContext({ signador: "Dr. Smith" });
      const context = contextManager.getContext();
      expect(context.signador).toBe("Dr. Smith");
    });

    it("should access campanya through context", () => {
      contextManager.initializeContext({ campanya: "2024-Q1" });
      const context = contextManager.getContext();
      expect(context.campanya).toBe("2024-Q1");
    });

    it("should return undefined for unset properties", () => {
      const context = contextManager.getContext();
      expect(context.visi_id).toBeUndefined();
      expect(context.signador).toBeUndefined();
      expect(context.campanya).toBeUndefined();
    });
  });
});
