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

describe("SessionExpiryWarning", (): void => {
  it("should render with minutes remaining", (): void => {
    render(<SessionExpiryWarning minutesRemaining={5} />);
    expect(screen.getByText(/your session will end in 5 minutes/i)).toBeInTheDocument();
  });

  it("should use singular 'minute' when 1 minute remaining", (): void => {
    render(<SessionExpiryWarning minutesRemaining={1} />);
    expect(screen.getByText(/your session will end in 1 minute\./i)).toBeInTheDocument();
  });

  it("should show extend button when onExtend is provided", (): void => {
    const onExtend = vi.fn();
    render(<SessionExpiryWarning minutesRemaining={3} onExtend={onExtend} />);
    expect(screen.getByText("Extend Session")).toBeInTheDocument();
  });

  it("should not show extend button when onExtend is not provided", (): void => {
    render(<SessionExpiryWarning minutesRemaining={3} />);
    expect(screen.queryByText("Extend Session")).not.toBeInTheDocument();
  });

  it("should call onExtend when extend button is clicked", async (): Promise<void> => {
    const user = userEvent.setup();
    const onExtend = vi.fn();
    render(<SessionExpiryWarning minutesRemaining={3} onExtend={onExtend} />);

    await user.click(screen.getByText("Extend Session"));
    expect(onExtend).toHaveBeenCalledOnce();
  });

  it("should show dismiss button when onDismiss is provided", (): void => {
    const onDismiss = vi.fn();
    render(<SessionExpiryWarning minutesRemaining={3} onDismiss={onDismiss} />);
    expect(screen.getByLabelText("Dismiss")).toBeInTheDocument();
  });

  it("should not show dismiss button when onDismiss is not provided", (): void => {
    render(<SessionExpiryWarning minutesRemaining={3} />);
    expect(screen.queryByLabelText("Dismiss")).not.toBeInTheDocument();
  });

  it("should call onDismiss when dismiss button is clicked", async (): Promise<void> => {
    const user = userEvent.setup();
    const onDismiss = vi.fn();
    render(<SessionExpiryWarning minutesRemaining={3} onDismiss={onDismiss} />);

    await user.click(screen.getByLabelText("Dismiss"));
    expect(onDismiss).toHaveBeenCalledOnce();
  });

  it("should have role='status' for accessibility", (): void => {
    render(<SessionExpiryWarning minutesRemaining={5} />);
    expect(screen.getByRole("status")).toBeInTheDocument();
  });

  it("should have aria-live='polite' for screen readers", (): void => {
    render(<SessionExpiryWarning minutesRemaining={5} />);
    const statusElement = screen.getByRole("status");
    expect(statusElement).toHaveAttribute("aria-live", "polite");
  });

  it("should use blue theme (not red) for PDA-friendly design", (): void => {
    render(<SessionExpiryWarning minutesRemaining={5} />);
    const statusElement = screen.getByRole("status");
    expect(statusElement.className).toContain("bg-blue-50");
    expect(statusElement.className).toContain("border-blue-200");
  });

  it("should include saving work reminder in message", (): void => {
    render(<SessionExpiryWarning minutesRemaining={5} />);
    expect(screen.getByText(/consider saving your work/i)).toBeInTheDocument();
  });
});
