import React from "react";
import "@testing-library/jest-dom";
import { render, screen } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";
import { api } from "./api";
import { I18nProvider } from "./i18n/I18nProvider";
import OfferComparisonPage from "./views/OfferComparisonPage";

jest.mock("./api", () => ({
  api: { get: jest.fn() },
  getApiErrorMessage: (_error: unknown, fallback: string) => fallback,
}));
const mockedApi = api as jest.Mocked<typeof api>;

test("compares recorded offer facts without filling missing values", async () => {
  mockedApi.get.mockResolvedValue({ data: { items: [
    { id: 1, jobTitle: "Backend Engineer", company: { id: 1, name: "Acme" }, status: "Offer", salaryMin: 800000, salaryMax: 900000, salaryCurrency: "NOK", salaryPeriod: "year", location: "Oslo", deadline: "2026-09-10", nextAction: "Review contract", notes: "Hybrid three days", dateApplied: "2026-08-01", savedAt: "2026-07-20", responseReceived: true, daysSince: 10 },
    { id: 2, jobTitle: "Platform Engineer", company: { id: 2, name: "Beta" }, status: "Offer", dateApplied: "2026-08-02", savedAt: "2026-07-21", responseReceived: true, daysSince: 9 },
  ], total: 2, page: 1, pageSize: 25 } } as any);

  render(<I18nProvider><MemoryRouter><OfferComparisonPage /></MemoryRouter></I18nProvider>);

  expect(await screen.findByText("Backend Engineer")).toBeInTheDocument();
  expect(screen.getByText("800,000 – 900,000 NOK / year")).toBeInTheDocument();
  expect(screen.getByText("Hybrid three days")).toBeInTheDocument();
  expect(screen.getAllByText("Not recorded").length).toBeGreaterThan(0);
  expect(mockedApi.get).toHaveBeenCalledWith("/jobapplications", { params: { status: "Offer", page: 1, pageSize: 25, sortBy: "deadline", sortDir: "asc" } });
});

test("shows an honest empty state when there are no offers", async () => {
  mockedApi.get.mockResolvedValue({ data: { items: [], total: 0, page: 1, pageSize: 25 } } as any);
  render(<I18nProvider><MemoryRouter><OfferComparisonPage /></MemoryRouter></I18nProvider>);
  expect(await screen.findByText("No active offers to compare")).toBeInTheDocument();
});
