/**
 * PostHog Configuration Tests
 *
 * Tests that the PostHog wrapper exports the expected API surface.
 */

jest.mock("posthog-js", () => {
  const mock = {
    init: jest.fn(),
    capture: jest.fn(),
    identify: jest.fn(),
    reset: jest.fn(),
    startSessionRecording: jest.fn(),
    stopSessionRecording: jest.fn(),
    isFeatureEnabled: jest.fn(),
  };
  return { __esModule: true, default: mock, ...mock };
});

// eslint-disable-next-line @typescript-eslint/no-var-requires
const mockPostHog = require("posthog-js").default;

import {
  initPostHog,
  trackEvent,
  identifyUser,
  resetSession,
  trackHighValueEvent,
  startSessionRecording,
  stopSessionRecording,
} from "@/lib/posthog";

describe("PostHog Configuration", () => {
  const originalEnv = process.env;

  beforeEach(() => {
    jest.clearAllMocks();
    process.env = { ...originalEnv };
  });

  afterAll(() => {
    process.env = originalEnv;
  });

  describe("Exported API surface", () => {
    it("should export initPostHog function", () => {
      expect(typeof initPostHog).toBe("function");
    });

    it("should export trackEvent function", () => {
      expect(typeof trackEvent).toBe("function");
    });

    it("should export identifyUser function", () => {
      expect(typeof identifyUser).toBe("function");
    });

    it("should export resetSession function", () => {
      expect(typeof resetSession).toBe("function");
    });

    it("should export trackHighValueEvent function", () => {
      expect(typeof trackHighValueEvent).toBe("function");
    });

    it("should export startSessionRecording function", () => {
      expect(typeof startSessionRecording).toBe("function");
    });

    it("should export stopSessionRecording function", () => {
      expect(typeof stopSessionRecording).toBe("function");
    });
  });

  describe("initPostHog", () => {
    it("should warn when NEXT_PUBLIC_POSTHOG_KEY is not set", () => {
      delete process.env.NEXT_PUBLIC_POSTHOG_KEY;
      const consoleSpy = jest.spyOn(console, "warn").mockImplementation();

      initPostHog();

      expect(consoleSpy).toHaveBeenCalledWith(
        expect.stringContaining("PostHog API key not configured"),
      );
      consoleSpy.mockRestore();
    });

    it("should call posthog.init when API key is set", () => {
      process.env.NEXT_PUBLIC_POSTHOG_KEY = "phc_test_key";

      initPostHog();

      expect(mockPostHog.init).toHaveBeenCalledWith(
        "phc_test_key",
        expect.objectContaining({
          enable_heatmaps: true,
          enable_recording_console_log: false,
          respect_dnt: true,
        }),
      );
    });

    it("should use custom api_host when set", () => {
      process.env.NEXT_PUBLIC_POSTHOG_KEY = "phc_test_key";
      process.env.NEXT_PUBLIC_POSTHOG_HOST = "https://custom-host.example.com";

      initPostHog();

      expect(mockPostHog.init).toHaveBeenCalledWith(
        "phc_test_key",
        expect.objectContaining({
          api_host: "https://custom-host.example.com",
        }),
      );
    });

    it("should configure session recording with PII masking", () => {
      process.env.NEXT_PUBLIC_POSTHOG_KEY = "phc_test_key";

      initPostHog();

      const config = mockPostHog.init.mock.calls[0][1];
      expect(config.session_recording).toBeDefined();
      expect(config.session_recording.maskAllInputs).toBe(true);
      expect(config.session_recording.recordCrossOriginIframes).toBe(false);
    });
  });

  describe("Event Tracking", () => {
    it("should call posthog.capture for trackEvent", () => {
      trackEvent("test_event", { key: "value" });
      expect(mockPostHog.capture).toHaveBeenCalledWith(
        "test_event",
        expect.objectContaining({ key: "value" }),
      );
    });

    it("should call posthog.capture for trackHighValueEvent", () => {
      trackHighValueEvent("purchase_complete", { amount: 100 });
      expect(mockPostHog.capture).toHaveBeenCalledWith(
        "purchase_complete",
        expect.objectContaining({
          amount: 100,
          high_value: true,
        }),
      );
    });
  });

  describe("User Identity", () => {
    it("should call posthog.identify", () => {
      identifyUser("user-123", { email: "test@example.com" });
      expect(mockPostHog.identify).toHaveBeenCalledWith(
        "user-123",
        expect.objectContaining({ email: "test@example.com" }),
      );
    });

    it("should call posthog.reset on resetSession", () => {
      resetSession();
      expect(mockPostHog.reset).toHaveBeenCalled();
    });
  });

  describe("Recording Controls", () => {
    it("should call posthog.startSessionRecording", () => {
      startSessionRecording();
      expect(mockPostHog.startSessionRecording).toHaveBeenCalled();
    });

    it("should call posthog.stopSessionRecording", () => {
      stopSessionRecording();
      expect(mockPostHog.stopSessionRecording).toHaveBeenCalled();
    });
  });
});
