// @vitest-environment jsdom
// [nexus] Unit tests for OnboardingSummaryStep component
import { describe, it, expect, vi, afterEach } from "vitest";
import { render, screen, fireEvent, cleanup } from "@testing-library/react";
import { renderToStaticMarkup } from "react-dom/server";
import { OnboardingSummaryStep } from "./OnboardingSummaryStep";
import type { HardwareInfo } from "@/api/hardware";

afterEach(() => {
  cleanup();
});

const mockHardwareInfo: HardwareInfo = {
  totalGb: 16,
  freeGb: 8,
  usableGb: 12,
  platform: "darwin",
  gpuName: null,
  gpuVramGb: null,
  unifiedMemory: true,
  hardwareTier: "apple_silicon",
  cpuModel: "Apple M2",
};

const defaultProps = {
  hardwareInfo: mockHardwareInfo,
  selectedMode: "both" as const,
  providerLabel: "Puter (free, zero-config)",
  rootDir: "~/projects/test",
  loading: false,
  error: null,
  onStartChat: vi.fn(),
  onBack: vi.fn(),
};

describe("OnboardingSummaryStep", () => {
  it("renders all summary rows with provided data", () => {
    const html = renderToStaticMarkup(
      <OnboardingSummaryStep {...defaultProps} />
    );

    expect(html).toContain("Hardware");
    expect(html).toContain("Apple Silicon");
    expect(html).toContain("Mode");
    expect(html).toContain("Both (recommended)");
    expect(html).toContain("Provider");
    expect(html).toContain("Puter (free, zero-config)");
    expect(html).toContain("Root directory");
    expect(html).toContain("~/projects/test");
  });

  it("hides root directory row when rootDir is empty string", () => {
    const html = renderToStaticMarkup(
      <OnboardingSummaryStep {...defaultProps} rootDir="" />
    );

    expect(html).not.toContain("Root directory");
  });

  it("shows error message when error prop is non-null", () => {
    const html = renderToStaticMarkup(
      <OnboardingSummaryStep
        {...defaultProps}
        error="Setup failed. Please try again."
      />
    );

    expect(html).toContain("Setup failed. Please try again.");
  });

  it("calls onStartChat when 'Start chatting' button is clicked", () => {
    const onStartChat = vi.fn();
    render(<OnboardingSummaryStep {...defaultProps} onStartChat={onStartChat} />);

    const buttons = screen.getAllByRole("button");
    const startBtn = buttons.find((b) => b.textContent?.includes("Start chatting"));
    expect(startBtn).toBeTruthy();
    fireEvent.click(startBtn!);

    expect(onStartChat).toHaveBeenCalledOnce();
  });

  it("disables 'Start chatting' button when loading is true", () => {
    render(<OnboardingSummaryStep {...defaultProps} loading={true} />);

    const buttons = screen.getAllByRole("button");
    const startBtn = buttons.find((b) => b.textContent?.includes("Setting up"));
    expect(startBtn).toBeTruthy();
    expect(startBtn).toHaveProperty("disabled", true);
  });

  it("shows 'Setting up...' text when loading is true", () => {
    const html = renderToStaticMarkup(
      <OnboardingSummaryStep {...defaultProps} loading={true} />
    );

    expect(html).toContain("Setting up...");
  });
});
