import { render, screen, fireEvent } from "@testing-library/react";
import { expect, test, vi } from "vitest";
import { GeneralTab } from "./GeneralTab";
import { CleanConfig } from "../types";

const mockConfig: CleanConfig = {
  strip_utms: true,
  fix_quotes: true,
  fix_newlines: true,
  play_sound: true,
  autostart: false,
  hotkey: "Alt+Shift+C",
  custom_params: [],
};

test("renders hotkey display and toggles", () => {
  const onToggleRecording = vi.fn();
  const onUpdateConfig = vi.fn();

  render(
    <GeneralTab
      config={mockConfig}
      isConflict={false}
      isRecording={false}
      onToggleRecording={onToggleRecording}
      onUpdateConfig={onUpdateConfig}
    />,
  );

  expect(screen.getByText("Alt")).toBeInTheDocument();
  expect(screen.getByText("Shift")).toBeInTheDocument();
  expect(screen.getByText("C")).toBeInTheDocument();

  const hotkeyZone = screen.getByTestId("hotkey-display");

  expect(hotkeyZone).toBeInTheDocument();

  fireEvent.click(hotkeyZone);

  expect(onToggleRecording).toHaveBeenCalledTimes(1);

  const soundCheckbox = screen.getAllByRole("checkbox")[0];

  fireEvent.click(soundCheckbox);

  expect(onUpdateConfig).toHaveBeenCalledWith({ play_sound: false });
});

test("shows conflict text when isConflict is true", () => {
  render(
    <GeneralTab
      config={mockConfig}
      isConflict={true}
      isRecording={false}
      onToggleRecording={() => {}}
      onUpdateConfig={() => {}}
    />,
  );

  expect(screen.getByText(/conflict/i)).toBeInTheDocument();
});
