import * as Sentry from "@sentry/nextjs";
import { DEFAULT_SERVER_ERROR_MESSAGE } from "next-safe-action";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import {
  AuthenticationError,
  AuthorizationError,
  INVALID_PASSWORD_RESET_TOKEN_ERROR_CODE,
  InvalidInputError,
  InvalidPasswordResetTokenError,
  OperationNotAllowedError,
  QueryExecutionError,
  ResourceNotFoundError,
  TooManyRequestsError,
  UniqueConstraintError,
  UnknownError,
  ValidationError,
  isExpectedError,
} from "@formbricks/types/errors";
import { RequestBodyTooLargeError } from "@/app/lib/api/request-body";
import { getSession } from "@/modules/auth/lib/session";
import { getAuditRequestMetadata } from "@/modules/ee/audit-logs/lib/request-metadata";

// Mock Sentry
vi.mock("@sentry/nextjs", () => ({
  captureException: vi.fn(),
}));

// Mock logger — use plain functions for chained calls so vi.resetAllMocks() doesn't break them
vi.mock("@formbricks/logger", () => ({
  logger: {
    withContext: () => ({ error: vi.fn() }),
    warn: vi.fn(),
  },
}));

// Mock session DAL
vi.mock("@/modules/auth/lib/session", () => ({
  getSession: vi.fn(),
}));

// Mock user service
vi.mock("@/lib/user/service", () => ({
  getUser: vi.fn(),
}));

// Mock metadata snapshots, independently of the IP capture preference.
vi.mock("@/modules/ee/audit-logs/lib/request-metadata", () => ({
  getAuditRequestMetadata: vi.fn().mockResolvedValue({ ipAddress: "unknown", userAgent: "Agent/1" }),
  UNKNOWN_AUDIT_REQUEST_METADATA: { ipAddress: "unknown", userAgent: "unknown" },
}));

// Mock client IP
vi.mock("@/lib/utils/client-ip", () => ({
  getClientIpFromHeaders: vi.fn(),
}));

// Mock constants
const { auditFlags } = vi.hoisted(() => ({ auditFlags: { enabled: false } }));
vi.mock("@/lib/constants", () => ({
  get AUDIT_LOG_ENABLED() {
    return auditFlags.enabled;
  },
  AUDIT_LOG_GET_USER_IP: false,
}));

// Mock audit log types
vi.mock("@/modules/ee/audit-logs/types/audit-log", () => ({
  UNKNOWN_DATA: "unknown",
}));

// ── shared helper tests (pure logic, no action client needed) ──────────

describe("isExpectedError (shared helper)", () => {
  test.each([
    { ErrorClass: AuthorizationError, args: ["Not authorized"] },
    { ErrorClass: AuthenticationError, args: ["Not authenticated"] },
    { ErrorClass: TooManyRequestsError, args: ["Rate limit exceeded"] },
    { ErrorClass: ResourceNotFoundError, args: ["Survey", "123"] },
    { ErrorClass: InvalidInputError, args: ["Invalid input"] },
    { ErrorClass: ValidationError, args: ["Invalid data"] },
    { ErrorClass: OperationNotAllowedError, args: ["Not allowed"] },
    { ErrorClass: QueryExecutionError, args: ["Cube query failed. Details: connect ECONNREFUSED"] },
    { ErrorClass: InvalidPasswordResetTokenError, args: [INVALID_PASSWORD_RESET_TOKEN_ERROR_CODE] },
    { ErrorClass: UniqueConstraintError, args: ["Already exists"] },
    { ErrorClass: RequestBodyTooLargeError, args: [2 * 1024 * 1024] },
  ])("returns true for $ErrorClass.name", ({ ErrorClass, args }) => {
    const error = new (ErrorClass as new (...args: unknown[]) => Error)(...args);
    expect(isExpectedError(error)).toBe(true);
  });

  test("returns true for serialised errors that only have a matching name", () => {
    const serialisedError = new Error("Auth failed");
    serialisedError.name = "AuthorizationError";
    expect(isExpectedError(serialisedError)).toBe(true);
  });

  test.each([
    { error: new Error("Something broke"), label: "Error" },
    { error: new TypeError("Cannot read properties"), label: "TypeError" },
    { error: new RangeError("Maximum call stack"), label: "RangeError" },
    { error: new UnknownError("Unknown"), label: "UnknownError" },
  ])("returns false for $label", ({ error }) => {
    expect(isExpectedError(error)).toBe(false);
  });
});

// ── integration tests against the real actionClient / authenticatedActionClient ──

describe("actionClient handleServerError", () => {
  // Lazily import so mocks are in place first
  let actionClient: (typeof import("./index"))["actionClient"];

  beforeEach(async () => {
    vi.clearAllMocks();
    const mod = await import("./index");
    actionClient = mod.actionClient;
  });

  afterEach(() => {
    vi.clearAllMocks();
  });

  // Helper: create and execute an action that throws the given error
  const executeThrowingAction = async (error: Error) => {
    const action = actionClient.action(async () => {
      throw error;
    });
    return action();
  };

  describe("expected errors should NOT be reported to Sentry", () => {
    test("AuthorizationError returns its message and is not sent to Sentry", async () => {
      const result = await executeThrowingAction(new AuthorizationError("Not authorized"));
      expect(result?.serverError).toBe("Not authorized");
      expect(Sentry.captureException).not.toHaveBeenCalled();
    });

    test("AuthenticationError returns its message and is not sent to Sentry", async () => {
      const result = await executeThrowingAction(new AuthenticationError("Not authenticated"));
      expect(result?.serverError).toBe("Not authenticated");
      expect(Sentry.captureException).not.toHaveBeenCalled();
    });

    test("TooManyRequestsError returns its message and is not sent to Sentry", async () => {
      const result = await executeThrowingAction(new TooManyRequestsError("Rate limit exceeded"));
      expect(result?.serverError).toBe("Rate limit exceeded");
      expect(Sentry.captureException).not.toHaveBeenCalled();
    });

    test("ResourceNotFoundError returns its message and is not sent to Sentry", async () => {
      const result = await executeThrowingAction(new ResourceNotFoundError("Survey", "123"));
      expect(result?.serverError).toContain("Survey");
      expect(Sentry.captureException).not.toHaveBeenCalled();
    });

    test("InvalidInputError returns its message and is not sent to Sentry", async () => {
      const result = await executeThrowingAction(new InvalidInputError("Invalid input"));
      expect(result?.serverError).toBe("Invalid input");
      expect(Sentry.captureException).not.toHaveBeenCalled();
    });

    test("ValidationError returns its message and is not sent to Sentry", async () => {
      const result = await executeThrowingAction(new ValidationError("Invalid data"));
      expect(result?.serverError).toBe("Invalid data");
      expect(Sentry.captureException).not.toHaveBeenCalled();
    });

    test("OperationNotAllowedError returns its message and is not sent to Sentry", async () => {
      const result = await executeThrowingAction(new OperationNotAllowedError("Not allowed"));
      expect(result?.serverError).toBe("Not allowed");
      expect(Sentry.captureException).not.toHaveBeenCalled();
    });

    test("QueryExecutionError returns its message and is not sent to Sentry", async () => {
      const result = await executeThrowingAction(
        new QueryExecutionError("Cube query failed. Details: connect ECONNREFUSED")
      );
      expect(result?.serverError).toBe("Cube query failed. Details: connect ECONNREFUSED");
      expect(Sentry.captureException).not.toHaveBeenCalled();
    });

    test("InvalidPasswordResetTokenError returns its message and is not sent to Sentry", async () => {
      const result = await executeThrowingAction(
        new InvalidPasswordResetTokenError(INVALID_PASSWORD_RESET_TOKEN_ERROR_CODE)
      );
      expect(result?.serverError).toBe(INVALID_PASSWORD_RESET_TOKEN_ERROR_CODE);
      expect(Sentry.captureException).not.toHaveBeenCalled();
    });

    test("UniqueConstraintError returns its message and is not sent to Sentry", async () => {
      const result = await executeThrowingAction(
        new UniqueConstraintError("Action with name foo already exists")
      );
      expect(result?.serverError).toBe("Action with name foo already exists");
      expect(Sentry.captureException).not.toHaveBeenCalled();
    });
  });

  describe("unexpected errors SHOULD be reported to Sentry", () => {
    test("generic Error is sent to Sentry and returns default message", async () => {
      const error = new Error("Something broke");
      const result = await executeThrowingAction(error);
      expect(result?.serverError).toBe(DEFAULT_SERVER_ERROR_MESSAGE);
      expect(Sentry.captureException).toHaveBeenCalledWith(
        error,
        expect.objectContaining({ extra: expect.any(Object) })
      );
    });

    test("an unauthenticated action's error is captured without a user", async () => {
      await executeThrowingAction(new Error("Something broke"));
      expect(vi.mocked(Sentry.captureException).mock.calls[0][1]).not.toHaveProperty("user");
    });

    test("TypeError is sent to Sentry and returns default message", async () => {
      const error = new TypeError("Cannot read properties of undefined");
      const result = await executeThrowingAction(error);
      expect(result?.serverError).toBe(DEFAULT_SERVER_ERROR_MESSAGE);
      expect(Sentry.captureException).toHaveBeenCalledWith(
        error,
        expect.objectContaining({ extra: expect.any(Object) })
      );
    });

    test("UnknownError is sent to Sentry (not an expected business-logic error)", async () => {
      const error = new UnknownError("Unknown error");
      const result = await executeThrowingAction(error);
      expect(result?.serverError).toBe(DEFAULT_SERVER_ERROR_MESSAGE);
      expect(Sentry.captureException).toHaveBeenCalledWith(
        error,
        expect.objectContaining({ extra: expect.any(Object) })
      );
    });
  });
});

describe("authenticatedActionClient", () => {
  let authenticatedActionClient: (typeof import("./index"))["authenticatedActionClient"];
  let getUser: (typeof import("@/lib/user/service"))["getUser"];

  beforeEach(async () => {
    vi.clearAllMocks();
    const mod = await import("./index");
    authenticatedActionClient = mod.authenticatedActionClient;
    const userService = await import("@/lib/user/service");
    getUser = userService.getUser;
  });

  afterEach(() => {
    vi.clearAllMocks();
  });

  test("throws AuthenticationError when there is no session", async () => {
    vi.mocked(getSession).mockResolvedValue(null);

    const action = authenticatedActionClient.action(async () => "ok");
    const result = await action();

    // handleServerError catches AuthenticationError and returns its message
    expect(result?.serverError).toBe("Not authenticated");
    expect(Sentry.captureException).not.toHaveBeenCalled();
  });

  test("throws AuthorizationError when user is not found", async () => {
    vi.mocked(getSession).mockResolvedValue({ user: { id: "user-1" }, expires: "2999-01-01T00:00:00.000Z" });
    vi.mocked(getUser, { partial: true }).mockResolvedValue(null);

    const action = authenticatedActionClient.action(async () => "ok");
    const result = await action();

    expect(result?.serverError).toBe("User not found");
    expect(Sentry.captureException).not.toHaveBeenCalled();
  });

  test("executes action successfully when session and user exist", async () => {
    vi.mocked(getSession).mockResolvedValue({ user: { id: "user-1" }, expires: "2999-01-01T00:00:00.000Z" });
    vi.mocked(getUser, { partial: true }).mockResolvedValue({ id: "user-1", name: "Test" });

    const action = authenticatedActionClient.action(async () => "success");
    const result = await action();

    expect(result?.data).toBe("success");
    expect(result?.serverError).toBeUndefined();
    expect(Sentry.captureException).not.toHaveBeenCalled();
  });

  test("attributes an unexpected error to the signed-in user by id only", async () => {
    vi.mocked(getSession).mockResolvedValue({ user: { id: "user-1" }, expires: "2999-01-01T00:00:00.000Z" });
    vi.mocked(getUser).mockResolvedValue({ id: "user-1", name: "Test", email: "test@example.com" } as Awaited<
      ReturnType<typeof getUser>
    >);
    const error = new Error("Something broke");

    const action = authenticatedActionClient.action(async () => {
      throw error;
    });
    const result = await action();

    expect(result?.serverError).toBe(DEFAULT_SERVER_ERROR_MESSAGE);
    expect(Sentry.captureException).toHaveBeenCalledWith(
      error,
      expect.objectContaining({ user: { id: "user-1" } })
    );
  });
});

describe("audit request metadata context", () => {
  afterEach(() => {
    auditFlags.enabled = false;
  });

  test("spreads the request metadata snapshot into the audit context", async () => {
    auditFlags.enabled = true;
    vi.mocked(getAuditRequestMetadata).mockResolvedValue({ ipAddress: "unknown", userAgent: "Agent/1" });
    const { actionClient } = await import("./index");
    const action = actionClient.action(async ({ ctx }) => ctx.auditLoggingCtx);
    const result = await action();
    expect(result?.data).toMatchObject({ ipAddress: "unknown", userAgent: "Agent/1" });
  });
});
