import { render, screen } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { KEYS } from "../data/mock/keys";
import { I18nProvider } from "../i18n/I18nContext";
import { ThemeProvider } from "../theme/ThemeContext";
import { SettingsPage } from "./SettingsPage";

vi.mock("../data/DataContext", () => ({
  useRepos: () => ({
    settings: {
      async getAi() {
        return {
          provider: "xai",
          model: "grok-4",
          research_provider: "xai",
          research_model: "grok-4",
          selected_model: "grok-4",
          models: ["grok-4"],
          api_key_configured: false,
          research_api_key_configured: false,
        };
      },
      async getPlacement() {
        return {
          web_search_provider: "exa",
          expand_strategy: "hybrid",
          brave_api_key_configured: false,
          exa_api_key_configured: false,
          dev_mode_enabled: false,
        };
      },
    },
  }),
  useData: () => ({ refresh: vi.fn() }),
}));

function renderPage() {
  return render(
    <I18nProvider>
      <ThemeProvider>
        <SettingsPage />
      </ThemeProvider>
    </I18nProvider>,
  );
}

describe("SettingsPage lab section", () => {
  beforeEach(() => {
    localStorage.setItem(
      KEYS.uiPrefs,
      JSON.stringify({ locale: "zh-TW", currency: "TWD", theme: "system" }),
    );
  });

  it("shows local-session controls without crawler wording", async () => {
    renderPage();
    expect(await screen.findByTestId("settings-lab")).toBeTruthy();
    expect(screen.getByText("測試海巡（本機工作階段）")).toBeTruthy();
    expect(screen.queryByText(/爬蟲/)).toBeNull();
    expect(screen.queryByText(/crawler/i)).toBeNull();
  });
});

describe("SettingsPage lab section (en)", () => {
  beforeEach(() => {
    localStorage.setItem(
      KEYS.uiPrefs,
      JSON.stringify({ locale: "en", currency: "USD", theme: "system" }),
    );
  });

  it("shows English labels without crawler wording", async () => {
    renderPage();
    expect(await screen.findByTestId("settings-lab")).toBeTruthy();
    expect(screen.getByText("Test patrol (local session)")).toBeTruthy();
    expect(screen.queryByText(/crawler/i)).toBeNull();
    expect(screen.queryByText(/爬蟲/)).toBeNull();
  });
});
