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

import OnboardingChecklist from "./components/OnboardingChecklist";
import { I18nProvider } from "./i18n/I18nProvider";
import { api } from "./api";

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

const mockedApi = api as jest.Mocked<typeof api>;

function renderChecklist() {
  return render(<MemoryRouter><I18nProvider><OnboardingChecklist hasJobs={false} /></I18nProvider></MemoryRouter>);
}

test("uses structured career data and connected inbox status for onboarding progress", async () => {
  mockedApi.get.mockImplementation((url: string) => {
    if (url === "/auth/me") return Promise.resolve({ data: { firstName: "Ada" } } as any);
    if (url === "/career/profile") return Promise.resolve({ data: { cvText: "", profile: { jobs: [{ title: "Engineer" }] } } } as any);
    return Promise.resolve({ data: { connected: url === "/gmail/status" } } as any);
  });

  renderChecklist();

  expect(await screen.findByText("5 / 6")).toBeInTheDocument();
  expect(screen.queryByRole("button", { name: "Add CV" })).not.toBeInTheDocument();
  expect(screen.queryByRole("button", { name: "Open profile" })).not.toBeInTheDocument();
  expect(screen.queryByRole("button", { name: "Connect email" })).not.toBeInTheDocument();
  expect(screen.getByRole("button", { name: "Add job" })).toBeInTheDocument();
  await waitFor(() => expect(mockedApi.get).toHaveBeenCalledWith("/career/profile"));
});
