import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";

const originalEnv = { ...process.env };
const mockFetch = vi.fn();

describe("API Client (mock auth mode)", (): void => {
  beforeEach((): void => {
    process.env = {
      ...originalEnv,
      NODE_ENV: "development",
      NEXT_PUBLIC_AUTH_MODE: "mock",
    };
    vi.resetModules();
    mockFetch.mockReset();
    global.fetch = mockFetch;
  });

  afterEach((): void => {
    process.env = originalEnv;
    vi.restoreAllMocks();
  });

  it("should return local mock data for active projects widget without network calls", async (): Promise<void> => {
    const { apiPost } = await import("./client");
    interface ProjectResponse {
      id: string;
      status: string;
    }

    const response = await apiPost<ProjectResponse[]>("/api/widgets/data/active-projects");

    expect(response.length).toBeGreaterThan(0);
    const firstProject = response[0];
    expect(firstProject).toBeDefined();
    if (firstProject) {
      expect(typeof firstProject.id).toBe("string");
      expect(typeof firstProject.status).toBe("string");
    }
    expect(mockFetch).not.toHaveBeenCalled();
  });

  it("should return local mock data for agent chains widget without network calls", async (): Promise<void> => {
    const { apiPost } = await import("./client");
    interface AgentChainResponse {
      id: string;
      status: string;
    }

    const response = await apiPost<AgentChainResponse[]>("/api/widgets/data/agent-chains");

    expect(response.length).toBeGreaterThan(0);
    expect(response.some((session) => session.status === "active")).toBe(true);
    expect(mockFetch).not.toHaveBeenCalled();
  });
});
