// @vitest-environment jsdom

import React from "react";
import { describe, it, expect, vi, beforeEach } from "vitest";
import { createRoot } from "react-dom/client";
import Home from "@/app/page";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";

// Mock Sentry to avoid needing full initialization in tests
vi.mock("@sentry/nextjs", () => ({
  startSpan: (
    _opts: { op: string; name: string },
    cb: (span: { setAttribute: (k: string, v: unknown) => void }) => void,
  ) => cb({ setAttribute: () => {} }),
}));

// Basic fetch mock to satisfy initial FilesPanel query
beforeEach(() => {
  global.fetch = vi.fn(async (input: RequestInfo | URL) => {
    const url = typeof input === "string" ? input : input.toString();
    if (url.includes("/api/files/list")) {
      return {
        ok: true,
        json: async () => ({
          total: 0,
          page: 1,
          perPage: 50,
          items: [],
        }),
      } as unknown as Response;
    }
    // default ok empty
    return {
      ok: true,
      json: async () => ({}),
    } as unknown as Response;
  }) as unknown as typeof fetch;
});

function render(ui: React.ReactElement) {
  const container = document.createElement("div");
  document.body.appendChild(container);
  const root = createRoot(container);
  const qc = new QueryClient();
  root.render(<QueryClientProvider client={qc}>{ui}</QueryClientProvider>);
  return container;
}

describe("Dashboard Tabs (Home)", () => {
  it("renders all top-level dashboard tabs", () => {
    const container = render(<Home />);
    const text = container.textContent || "";
    expect(text).toContain("Files");
    expect(text).toContain("Search");
    expect(text).toContain("Document");
    expect(text).toContain("Qdrant");
  });
});
