import React from "react";
import "@testing-library/jest-dom";
import { fireEvent, render, screen } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";

import { api } from "./api";
import LandingPage from "./views/LandingPage";

const mockNavigate = jest.fn();
jest.mock("react-router-dom", () => ({
  ...jest.requireActual("react-router-dom"),
  useNavigate: () => mockNavigate,
}));

jest.mock("./api", () => ({ api: { get: jest.fn() } }));

beforeEach(() => {
  jest.clearAllMocks();
  (api.get as jest.Mock).mockRejectedValue({ response: { status: 401 } });
});

test("shows exactly Free and Pro without invented commercial terms", async () => {
  render(
    <MemoryRouter>
      <LandingPage />
    </MemoryRouter>,
  );

  expect(await screen.findByRole("heading", { name: "Two clear plans" })).toBeInTheDocument();
  expect(screen.getAllByRole("heading", { level: 3 }).map((heading) => heading.textContent)).toEqual(["Free", "Pro"]);
  expect(screen.queryByText(/bring your own key|unlimited AI|billed monthly|trial|£9|£3/i)).not.toBeInTheDocument();
  expect(screen.getByText(/Commercial terms appear only in configured checkout/i)).toBeInTheDocument();

  fireEvent.click(screen.getByRole("button", { name: "Create a Free account" }));
  expect(mockNavigate).toHaveBeenCalledWith("/register");

  fireEvent.click(screen.getByRole("button", { name: "Sign in to view Pro" }));
  expect(mockNavigate).toHaveBeenCalledWith("/login", { state: { from: "/settings" } });
});
