import { beforeEach, describe, expect, test, vi } from "vitest";
import { Config } from "@/lib/common/config";
import { emitDebugSurveyEvent, isDebugEnabled } from "@/lib/common/debug";
import { Logger } from "@/lib/common/logger";
import { getSurveyExclusionReason } from "@/lib/common/utils";
import { trackAction, trackCodeAction, trackNoCodeAction } from "@/lib/survey/action";
import { SurveyStore } from "@/lib/survey/store";
import { triggerSurvey } from "@/lib/survey/widget";

vi.mock("@/lib/common/config", () => ({
  Config: {
    getInstance: vi.fn(() => ({
      get: vi.fn(),
    })),
  },
}));

vi.mock("@/lib/survey/store", () => ({
  SurveyStore: {
    getInstance: vi.fn(() => ({
      setSurvey: vi.fn(),
    })),
  },
}));

vi.mock("@/lib/common/logger", () => ({
  Logger: {
    getInstance: vi.fn(() => {
      return {
        debug: vi.fn(),
      };
    }),
  },
}));

vi.mock("@/lib/common/utils", () => ({
  shouldDisplayBasedOnPercentage: vi.fn(),
  getSurveyExclusionReason: vi.fn(),
}));

vi.mock("@/lib/common/debug", () => ({
  isDebugEnabled: vi.fn(() => false),
  emitDebugSurveyEvent: vi.fn(),
}));

vi.mock("@/lib/survey/widget", () => ({
  triggerSurvey: vi.fn(),
}));

describe("survey/action.ts", () => {
  const mockSurvey = {
    id: "survey_001",
    name: "Test Survey",
    displayPercentage: 50,
    triggers: [
      {
        actionClass: { name: "testAction" },
      },
    ],
  };

  const mockConfig = {
    get: vi.fn(),
  };

  const mockSurveyStore = {
    setSurvey: vi.fn(),
  };

  const mockLogger = {
    debug: vi.fn(),
  };

  beforeEach(() => {
    vi.clearAllMocks();

    const getInstanceRn = vi.spyOn(Config, "getInstance");
    const getInstanceSurveyStore = vi.spyOn(SurveyStore, "getInstance");
    const getInstanceLogger = vi.spyOn(Logger, "getInstance");

    // Mock instances
    getInstanceRn.mockReturnValue(mockConfig as unknown as Config);
    getInstanceSurveyStore.mockReturnValue(mockSurveyStore as unknown as SurveyStore);
    getInstanceLogger.mockReturnValue(mockLogger as unknown as Logger);
  });

  describe("trackAction", () => {
    test("logs debug message with action name", async () => {
      mockConfig.get.mockReturnValue({ filteredSurveys: [] });

      const result = await trackAction("testAction");

      expect(result.ok).toBe(true);
      expect(mockLogger.debug).toHaveBeenCalledWith('Formbricks: Action "testAction" tracked');
    });

    test("logs debug message with alias if provided", async () => {
      mockConfig.get.mockReturnValue({ filteredSurveys: [] });

      const result = await trackAction("testAction", "aliasName");

      expect(result.ok).toBe(true);
      expect(mockLogger.debug).toHaveBeenCalledWith('Formbricks: Action "aliasName" tracked');
    });

    test("in debug mode, reports surveys on this trigger that setup already dropped, with the reason", async () => {
      const dropped = { ...mockSurvey, id: "survey_dropped" };
      const otherTrigger = {
        ...mockSurvey,
        id: "survey_other",
        triggers: [{ actionClass: { name: "other" } }],
      };
      mockConfig.get.mockReturnValue({
        filteredSurveys: [mockSurvey],
        workspace: { data: { surveys: [mockSurvey, dropped, otherTrigger] } },
        user: { data: {} },
      });
      vi.mocked(isDebugEnabled).mockReturnValue(true);
      vi.mocked(getSurveyExclusionReason).mockReturnValue("cooldown");

      await trackAction("testAction");

      expect(emitDebugSurveyEvent).toHaveBeenCalledTimes(1);
      expect(emitDebugSurveyEvent).toHaveBeenCalledWith({
        surveyId: "survey_dropped",
        action: "testAction",
        stage: "trigger",
        outcome: "excluded",
        exclusion: "cooldown",
      });
      vi.mocked(isDebugEnabled).mockReturnValue(false);
    });

    test("triggers survey if action matches survey trigger", async () => {
      mockConfig.get.mockReturnValue({
        filteredSurveys: [mockSurvey],
      });

      const result = await trackAction("testAction", undefined);

      expect(result.ok).toBe(true);
      expect(triggerSurvey).toHaveBeenCalledWith(mockSurvey, "testAction", undefined);
    });

    test("emits formbricks_action_tracked for every tracked action, even without a matching survey", async () => {
      // ENG-1846: funnel analytics wants the misses too — the emit sits on the shared trackAction
      // path, above the survey-matching loop.
      delete (window as { dataLayer?: unknown }).dataLayer;
      mockConfig.get.mockReturnValue({ filteredSurveys: [] });

      const result = await trackAction("testAction", "aliasedCode");

      expect(result.ok).toBe(true);
      expect(window.dataLayer).toEqual([
        {
          event: "formbricks_action_tracked",
          formbricks: {
            workspaceId: null,
            surveyId: null,
            responseId: null,
            finished: null,
            action: "aliasedCode",
          },
        },
      ]);
    });

    test("handles multiple matching surveys", async () => {
      const mockSurveys = [
        {
          id: "survey_1",
          triggers: [{ actionClass: { name: "testAction" } }],
        },
        {
          id: "survey_2",
          triggers: [{ actionClass: { name: "testAction" } }],
        },
      ];

      mockConfig.get.mockReturnValue({
        filteredSurveys: mockSurveys,
      });

      const result = await trackAction("testAction");

      expect(result.ok).toBe(true);
      expect(triggerSurvey).toHaveBeenCalledTimes(2);
    });

    test("logs when no active surveys", async () => {
      mockConfig.get.mockReturnValue({ filteredSurveys: [] });

      const result = await trackAction("testAction");

      expect(result.ok).toBe(true);
      expect(mockLogger.debug).toHaveBeenCalledWith("No active surveys to display");
    });
  });

  describe("trackCodeAction", () => {
    test("returns error for unknown action code", async () => {
      mockConfig.get.mockReturnValue({
        workspace: {
          data: {
            actionClasses: [{ type: "code", key: "known_code", name: "Known Action" }],
          },
        },
      });

      const result = await trackCodeAction("unknown_code");

      expect(result.ok).toBe(false);
      if (!result.ok) {
        expect(result.error.code).toBe("invalid_code");
        expect(result.error.message).toContain("unknown_code");
        expect(result.error.message).toContain("is unknown");
      }
    });

    test("tracks action when code is valid", async () => {
      const actionClass = { type: "code", key: "valid_code", name: "Valid Action" };

      mockConfig.get.mockReturnValue({
        workspace: {
          data: {
            actionClasses: [actionClass],
          },
        },
      });

      mockConfig.get.mockReturnValue({
        workspace: {
          data: {
            actionClasses: [actionClass],
          },
        },
        filteredSurveys: [],
      });

      const result = await trackCodeAction("valid_code");

      expect(result.ok).toBe(true);
      expect(mockLogger.debug).toHaveBeenCalledWith('Formbricks: Action "valid_code" tracked');
    });
  });

  describe("trackNoCodeAction", () => {
    test("calls trackAction with provided name", async () => {
      mockConfig.get.mockReturnValue({ filteredSurveys: [] });

      const result = await trackNoCodeAction("noCodeAction");

      expect(result.ok).toBe(true);
      expect(mockLogger.debug).toHaveBeenCalledWith('Formbricks: Action "noCodeAction" tracked');
    });
  });
});
