import { screen, cleanup } from "@testing-library/react";
import { expect, test, vi, beforeEach, afterEach } from "vitest";
import LoginPage from "./page";
import { renderWithProviders } from "../test-utils";

const mockPush = vi.fn();
vi.mock("next/navigation", () => ({
  useRouter: () => ({ push: mockPush }),
}));

const mockLogin = vi.fn();
vi.mock("../auth-context", () => ({
  useAuth: () => ({ login: mockLogin }),
  AuthProvider: ({ children }: { children: React.ReactNode }) => <>{children}</>,
}));

beforeEach(() => vi.clearAllMocks());
afterEach(() => cleanup());

test("renders login form with title", () => {
  renderWithProviders(<LoginPage />);
  expect(screen.getByText("Welcome back")).toBeDefined();
});

test("renders email and password inputs", () => {
  renderWithProviders(<LoginPage />);
  expect(screen.getByLabelText("Email")).toBeDefined();
  expect(screen.getByLabelText("Password")).toBeDefined();
});

test("renders sign in button", () => {
  renderWithProviders(<LoginPage />);
  expect(screen.getByRole("button", { name: "Sign in" })).toBeDefined();
});

test("renders link to signup", () => {
  renderWithProviders(<LoginPage />);
  expect(screen.getByText("Sign up")).toBeDefined();
});
