import { fireEvent, screen, waitFor } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { renderWithProviders } from "../utils/testHelpers";
import { SignUpV2Page } from "./signup-v2";

const mockSignUp = vi.fn();

vi.mock("../hooks/auth", () => ({
  useSignUp: () => ({
    mutate: mockSignUp,
    isPending: false,
  }),
  useLoginEmail: () => ({
    mutate: vi.fn(),
    isPending: false,
  }),
  useLoginGoogle: () => ({
    mutate: vi.fn(),
  }),
}));

describe("SignUpV2Page", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    localStorage.clear();
  });

  const fillAndSubmitForm = () => {
    fireEvent.change(screen.getByPlaceholderText(/your first name/i), {
      target: { value: "John" },
    });
    fireEvent.change(screen.getByPlaceholderText(/your last name/i), {
      target: { value: "Doe" },
    });
    fireEvent.change(screen.getByPlaceholderText(/your email/i), {
      target: { value: "john@example.com" },
    });
    fireEvent.change(screen.getAllByPlaceholderText(/your password/i)[0], {
      target: { value: "password123" },
    });
    fireEvent.change(screen.getByPlaceholderText(/confirm your password/i), {
      target: { value: "password123" },
    });
    fireEvent.click(screen.getByRole("checkbox", { name: /i accept/i }));
    fireEvent.click(screen.getByRole("button", { name: /create my account/i }));
  };

  it("submits solo billing intent by default", async () => {
    renderWithProviders(<SignUpV2Page />);

    fillAndSubmitForm();

    await waitFor(() => {
      expect(mockSignUp).toHaveBeenCalledWith(expect.objectContaining({ billing_intent: "solo" }));
    });
  });

  it("submits team billing intent when selected", async () => {
    renderWithProviders(<SignUpV2Page />);

    fireEvent.click(screen.getByRole("button", { name: /team/i }));
    fillAndSubmitForm();

    await waitFor(() => {
      expect(mockSignUp).toHaveBeenCalledWith(expect.objectContaining({ billing_intent: "team" }));
    });
  });

  it("submits founder billing intent when selected", async () => {
    renderWithProviders(<SignUpV2Page />);

    fireEvent.click(screen.getByRole("button", { name: /founder/i }));
    fillAndSubmitForm();

    await waitFor(() => {
      expect(mockSignUp).toHaveBeenCalledWith(
        expect.objectContaining({ billing_intent: "founder" })
      );
    });
  });
});
