import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import HomeView from "../components/HomeView";

// État minimal du store pour que HomeView ne plante pas
const baseStoreState = {
  guides: [],
  profiles: [],
  activeProfileId: null,
  syncing: false,
  openGuide: vi.fn(),
};

vi.mock("../store", () => ({
  useStore: vi.fn((selector?: (s: unknown) => unknown) => {
    return typeof selector === "function"
      ? selector(baseStoreState)
      : baseStoreState;
  }),
}));

describe("HomeView", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('ne montre pas la bannière "Première utilisation" sans needsSync', () => {
    render(<HomeView />);
    expect(screen.queryByText("Première utilisation")).not.toBeInTheDocument();
  });

  it('affiche la bannière "Première utilisation" quand needsSync est true', () => {
    render(<HomeView needsSync={true} />);
    expect(screen.getByText("Première utilisation")).toBeInTheDocument();
  });

  it('affiche le bouton "Synchroniser les guides" quand needsSync est true', () => {
    render(<HomeView needsSync={true} onSync={vi.fn()} />);
    expect(
      screen.getByRole("button", { name: /Synchroniser les guides/i })
    ).toBeInTheDocument();
  });

  it("appelle onSync au clic sur le bouton de synchronisation", async () => {
    const user = userEvent.setup();
    const onSync = vi.fn();
    render(<HomeView needsSync={true} onSync={onSync} />);

    await user.click(screen.getByRole("button", { name: /Synchroniser les guides/i }));

    expect(onSync).toHaveBeenCalledOnce();
  });
});
