import { fireEvent, screen, waitFor } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { renderWithProviders } from "../test/testHelpers";
import { ResetPasswordPage } from "./ResetPasswordPage";

const { mockResetPasswordForEmail } = vi.hoisted(() => ({
  mockResetPasswordForEmail: vi.fn(),
}));

vi.mock("../lib/supabase", () => ({
  supabase: {
    auth: {
      resetPasswordForEmail: mockResetPasswordForEmail,
    },
  },
}));

describe("ResetPasswordPage", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mockResetPasswordForEmail.mockResolvedValue({ error: null });
  });

  it("renders the shared auth shell", () => {
    renderWithProviders(<ResetPasswordPage />, { testUser: undefined });

    expect(screen.getByTestId("auth-card-shell")).toBeInTheDocument();
    expect(screen.getByLabelText("Email")).toBeInTheDocument();
  });

  it("submits a password reset email", async () => {
    renderWithProviders(<ResetPasswordPage />, { testUser: undefined });

    fireEvent.change(screen.getByLabelText("Email"), { target: { value: "client@example.com" } });
    fireEvent.click(screen.getByRole("button", { name: "Envoyer le lien de réinitialisation" }));

    await waitFor(() => {
      expect(mockResetPasswordForEmail).toHaveBeenCalled();
      expect(screen.getByText(/vérifiez votre boîte mail/i)).toBeInTheDocument();
    });
  });
});
