import React from "react";
import "@testing-library/jest-dom";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import CareerEvidenceBank from "./views/career/CareerEvidenceBank";
import { I18nProvider } from "./i18n/I18nProvider";
import { api } from "./api";

jest.mock("./api", () => ({ api: { get: jest.fn(), post: jest.fn(), delete: jest.fn() }, getApiErrorMessage: (_e: unknown, fallback: string) => fallback }));
jest.mock("./confirm", () => ({ useConfirm: () => ({ confirm: jest.fn().mockResolvedValue(true) }) }));
const mocked = api as jest.Mocked<typeof api>;

test("saves factual evidence with provenance and tags", async () => {
  mocked.get.mockResolvedValue({ data: [] } as any);
  mocked.post.mockResolvedValue({ data: { id: 1, category: "achievement", title: "Reduced incidents", statement: "Added rollback checks", tags: ["reliability"], sourceReference: "retrospective" } } as any);
  render(<I18nProvider><CareerEvidenceBank /></I18nProvider>);
  fireEvent.change(await screen.findByLabelText("Short title"), { target: { value: "Reduced incidents" } });
  fireEvent.change(screen.getByLabelText("Evidence statement"), { target: { value: "Added rollback checks" } });
  fireEvent.change(screen.getByLabelText("Skills and keywords"), { target: { value: "reliability" } });
  fireEvent.change(screen.getByLabelText("Source or provenance"), { target: { value: "retrospective" } });
  fireEvent.click(screen.getByRole("button", { name: "Save evidence" }));
  await waitFor(() => expect(mocked.post).toHaveBeenCalledWith("/career/evidence", expect.objectContaining({ title: "Reduced incidents", isVerified: true, sourceType: "manual" })));
  expect(await screen.findByText("Added rollback checks")).toBeInTheDocument();
});
