import { beforeEach, describe, expect, test, vi } from "vitest";
import { rateLimitConfigs } from "@/modules/core/rate-limit/rate-limit-configs";
import { createTrialPaymentCheckoutAction, startHobbyAction, startProTrialAction } from "./actions";

const mocks = vi.hoisted(() => ({
  assertCan: vi.fn(),
  getOrganization: vi.fn(),
  getOrganizationIdFromWorkspaceId: vi.fn(),
  getWorkspace: vi.fn(),
  createProTrialSubscription: vi.fn(),
  ensureCloudStripeSetupForOrganization: vi.fn(),
  ensureStripeCustomerForOrganization: vi.fn(),
  reconcileCloudStripeSubscriptionsForOrganization: vi.fn(),
  syncOrganizationBillingFromStripe: vi.fn(),
  capturePostHogEvent: vi.fn(),
  addOptimisticBillingFeature: vi.fn(),
  createCustomerPortalSession: vi.fn(),
  createSetupCheckoutSession: vi.fn(),
  isSubscriptionCancelled: vi.fn(),
  stripeCustomerSessionsCreate: vi.fn(),
  getProTrialDays: vi.fn(),
  applyRateLimit: vi.fn(),
}));

vi.mock("@/lib/utils/action-client", () => ({
  authenticatedActionClient: {
    inputSchema: vi.fn(() => ({
      action: vi.fn((fn) => fn),
    })),
  },
}));
// The mocked client hands back the bare handler, so each action below is called with `{ ctx, parsedInput }`,
// which its public signature does not describe.

vi.mock("@/lib/constants", () => ({
  WEBAPP_URL: "https://app.formbricks.com",
  POSTHOG_KEY: undefined,
}));

vi.mock("@/lib/posthog", () => ({
  capturePostHogEvent: mocks.capturePostHogEvent,
  groupIdentifyPostHog: vi.fn(),
}));

vi.mock("@/lib/authorization", () => ({
  assertCan: mocks.assertCan,
}));

vi.mock("@/lib/organization/service", () => ({
  getOrganization: mocks.getOrganization,
}));

vi.mock("@/lib/utils/helper", () => ({
  getOrganizationIdFromWorkspaceId: mocks.getOrganizationIdFromWorkspaceId,
}));

vi.mock("@/lib/workspace/service", () => ({
  getWorkspace: mocks.getWorkspace,
}));

vi.mock("@/modules/ee/audit-logs/lib/handler", () => ({
  withAuditLogging: vi.fn((_eventName, _objectType, fn) => fn),
}));

vi.mock("@/modules/core/rate-limit/helpers", () => ({
  applyRateLimit: mocks.applyRateLimit,
}));

vi.mock("@/modules/ee/billing/api/lib/create-customer-portal-session", () => ({
  createCustomerPortalSession: mocks.createCustomerPortalSession,
}));

vi.mock("@/modules/ee/billing/api/lib/create-setup-checkout-session", () => ({
  createSetupCheckoutSession: mocks.createSetupCheckoutSession,
}));

vi.mock("@/modules/ee/billing/api/lib/is-subscription-cancelled", () => ({
  isSubscriptionCancelled: mocks.isSubscriptionCancelled,
}));

vi.mock("@/modules/ee/billing/lib/organization-billing", () => ({
  createProTrialSubscription: mocks.createProTrialSubscription,
  ensureCloudStripeSetupForOrganization: mocks.ensureCloudStripeSetupForOrganization,
  ensureStripeCustomerForOrganization: mocks.ensureStripeCustomerForOrganization,
  reconcileCloudStripeSubscriptionsForOrganization: mocks.reconcileCloudStripeSubscriptionsForOrganization,
  syncOrganizationBillingFromStripe: mocks.syncOrganizationBillingFromStripe,
  addOptimisticBillingFeature: mocks.addOptimisticBillingFeature,
  getProTrialDays: mocks.getProTrialDays,
}));

vi.mock("@/modules/ee/billing/lib/stripe-client", () => ({
  stripeClient: {
    customerSessions: {
      create: mocks.stripeCustomerSessionsCreate,
    },
  },
}));

describe("billing actions", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mocks.assertCan.mockResolvedValue(undefined);
    mocks.getOrganization.mockResolvedValue({
      id: "org_1",
      billing: {
        stripeCustomerId: null,
      },
    });
    mocks.ensureStripeCustomerForOrganization.mockResolvedValue({ customerId: "cus_1" });
    mocks.createProTrialSubscription.mockResolvedValue(undefined);
    mocks.reconcileCloudStripeSubscriptionsForOrganization.mockResolvedValue(undefined);
    mocks.syncOrganizationBillingFromStripe.mockResolvedValue({ stripe: { features: ["ai-smart-tools"] } });
    mocks.addOptimisticBillingFeature.mockResolvedValue(undefined);
    mocks.getProTrialDays.mockResolvedValue(14);
    mocks.applyRateLimit.mockResolvedValue(undefined);
  });

  test("startHobbyAction ensures a customer, reconciles hobby, and syncs billing", async () => {
    const result = await startHobbyAction({
      ctx: { user: { id: "user_1" } },
      parsedInput: { organizationId: "org_1" },
    } as unknown as Parameters<typeof startHobbyAction>[0]);

    expect(mocks.assertCan).toHaveBeenCalledWith({ type: "user", id: "user_1" }, "organization.manage", {
      type: "organization",
      id: "org_1",
    });
    expect(mocks.getOrganization).toHaveBeenCalledWith("org_1");
    expect(mocks.ensureStripeCustomerForOrganization).toHaveBeenCalledWith("org_1");
    expect(mocks.reconcileCloudStripeSubscriptionsForOrganization).toHaveBeenCalledWith("org_1");
    expect(mocks.syncOrganizationBillingFromStripe).toHaveBeenCalledWith("org_1");
    expect(result).toEqual({ success: true });
  });

  test("startHobbyAction reuses an existing stripe customer id", async () => {
    mocks.getOrganization.mockResolvedValue({
      id: "org_1",
      billing: {
        stripeCustomerId: "cus_existing",
      },
    });

    const result = await startHobbyAction({
      ctx: { user: { id: "user_1" } },
      parsedInput: { organizationId: "org_1" },
    } as unknown as Parameters<typeof startHobbyAction>[0]);

    expect(mocks.ensureStripeCustomerForOrganization).not.toHaveBeenCalled();
    expect(mocks.reconcileCloudStripeSubscriptionsForOrganization).toHaveBeenCalledWith("org_1");
    expect(mocks.syncOrganizationBillingFromStripe).toHaveBeenCalledWith("org_1");
    expect(result).toEqual({ success: true });
  });

  test("startProTrialAction uses ensured customer when org snapshot has no stripe customer id", async () => {
    const auditLoggingCtx: { organizationId?: string; newObject?: Record<string, unknown> } = {};
    const result = await startProTrialAction({
      ctx: { user: { id: "user_1" }, auditLoggingCtx },
      parsedInput: { organizationId: "org_1" },
    } as unknown as Parameters<typeof startProTrialAction>[0]);

    expect(mocks.applyRateLimit).toHaveBeenCalledWith(rateLimitConfigs.actions.stateMutation, "org_1");
    expect(mocks.getOrganization).toHaveBeenCalledWith("org_1");
    expect(mocks.ensureStripeCustomerForOrganization).toHaveBeenCalledWith("org_1");
    expect(mocks.getProTrialDays).toHaveBeenCalledWith("org_1");
    expect(mocks.createProTrialSubscription).toHaveBeenCalledWith("org_1", "cus_1", 14);
    expect(mocks.reconcileCloudStripeSubscriptionsForOrganization).toHaveBeenCalledWith("org_1");
    expect(mocks.syncOrganizationBillingFromStripe).toHaveBeenCalledWith("org_1");
    expect(mocks.addOptimisticBillingFeature).toHaveBeenCalledWith("org_1", "ai-smart-tools");
    expect(auditLoggingCtx.organizationId).toBe("org_1");
    expect(auditLoggingCtx.newObject).toEqual({ plan: "pro", trialDurationDays: 14 });
    expect(result).toEqual({ success: true });
  });

  test("startProTrialAction passes the shortened trial length when the A/B test variant is active", async () => {
    mocks.getProTrialDays.mockResolvedValue(7);

    const result = await startProTrialAction({
      ctx: { user: { id: "user_1" }, auditLoggingCtx: {} },
      parsedInput: { organizationId: "org_1" },
    } as unknown as Parameters<typeof startProTrialAction>[0]);

    expect(mocks.createProTrialSubscription).toHaveBeenCalledWith("org_1", "cus_1", 7);
    expect(result).toEqual({ success: true });
  });

  test("startProTrialAction reports the trial end and length on reverse_trial_started", async () => {
    mocks.getProTrialDays.mockResolvedValue(7);
    mocks.syncOrganizationBillingFromStripe.mockResolvedValue({
      stripe: { trialEnd: "2026-10-07T12:00:00.000Z" },
    });

    await startProTrialAction({
      ctx: { user: { id: "user_1" }, auditLoggingCtx: {} },
      parsedInput: { organizationId: "org_1" },
    } as never);

    expect(mocks.capturePostHogEvent).toHaveBeenCalledWith(
      "user_1",
      "reverse_trial_started",
      { organization_id: "org_1", trial_end: "2026-10-07T12:00:00.000Z", trial_duration_days: 7 },
      { organizationId: "org_1" }
    );
  });

  test("startProTrialAction reuses an existing stripe customer id", async () => {
    mocks.getOrganization.mockResolvedValue({
      id: "org_1",
      billing: {
        stripeCustomerId: "cus_existing",
      },
    });

    const result = await startProTrialAction({
      ctx: { user: { id: "user_1" }, auditLoggingCtx: {} },
      parsedInput: { organizationId: "org_1" },
    } as unknown as Parameters<typeof startProTrialAction>[0]);

    expect(mocks.ensureStripeCustomerForOrganization).not.toHaveBeenCalled();
    expect(mocks.createProTrialSubscription).toHaveBeenCalledWith("org_1", "cus_existing", 14);
    expect(mocks.reconcileCloudStripeSubscriptionsForOrganization).toHaveBeenCalledWith("org_1");
    expect(mocks.syncOrganizationBillingFromStripe).toHaveBeenCalledWith("org_1");
    expect(mocks.addOptimisticBillingFeature).toHaveBeenCalledWith("org_1", "ai-smart-tools");
    expect(result).toEqual({ success: true });
  });

  test("createTrialPaymentCheckoutAction forwards upgrade intent to setup checkout", async () => {
    mocks.getOrganizationIdFromWorkspaceId.mockResolvedValue("org_1");
    mocks.getOrganization.mockResolvedValue({
      id: "org_1",
      billing: {
        stripeCustomerId: "cus_1",
        stripe: {
          subscriptionId: "sub_1",
        },
      },
    });
    mocks.getWorkspace.mockResolvedValue({ id: "ws_1" });
    mocks.createSetupCheckoutSession.mockResolvedValue("https://checkout.stripe.test/setup");

    const result = await createTrialPaymentCheckoutAction({
      ctx: { user: { id: "user_1" }, auditLoggingCtx: {} },
      parsedInput: {
        organizationId: "org_1",
        targetPlan: "pro",
        targetInterval: "yearly",
      },
    } as unknown as Parameters<typeof createTrialPaymentCheckoutAction>[0]);

    expect(mocks.createSetupCheckoutSession).toHaveBeenCalledWith(
      "cus_1",
      "sub_1",
      "https://app.formbricks.com/organizations/org_1/settings/billing",
      "org_1",
      {
        targetPlan: "pro",
        targetInterval: "yearly",
      }
    );
    expect(result).toBe("https://checkout.stripe.test/setup");
  });

  test("createTrialPaymentCheckoutAction creates payment-only setup checkout without upgrade intent", async () => {
    mocks.getOrganizationIdFromWorkspaceId.mockResolvedValue("org_1");
    mocks.getOrganization.mockResolvedValue({
      id: "org_1",
      billing: {
        stripeCustomerId: "cus_1",
        stripe: {
          subscriptionId: "sub_1",
        },
      },
    });
    mocks.getWorkspace.mockResolvedValue({ id: "ws_1" });
    mocks.createSetupCheckoutSession.mockResolvedValue("https://checkout.stripe.test/setup");

    await createTrialPaymentCheckoutAction({
      ctx: { user: { id: "user_1" }, auditLoggingCtx: {} },
      parsedInput: {
        organizationId: "org_1",
      },
    } as unknown as Parameters<typeof createTrialPaymentCheckoutAction>[0]);

    expect(mocks.createSetupCheckoutSession).toHaveBeenCalledWith(
      "cus_1",
      "sub_1",
      "https://app.formbricks.com/organizations/org_1/settings/billing",
      "org_1",
      undefined
    );
  });
});
