import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, waitFor } from "@testing-library/react";
import DashboardPage from "./page";
import { fetchDashboardSummary } from "@/lib/api/dashboard";

// Mock Phase 3 dashboard widgets
vi.mock("@/components/dashboard/DashboardMetrics", () => ({
  DashboardMetrics: (): React.JSX.Element => (
    <div data-testid="dashboard-metrics">Dashboard Metrics</div>
  ),
}));

vi.mock("@/components/dashboard/OrchestratorSessions", () => ({
  OrchestratorSessions: (): React.JSX.Element => (
    <div data-testid="orchestrator-sessions">Orchestrator Sessions</div>
  ),
}));

vi.mock("@/components/dashboard/QuickActions", () => ({
  QuickActions: (): React.JSX.Element => <div data-testid="quick-actions">Quick Actions</div>,
}));

vi.mock("@/components/dashboard/ActivityFeed", () => ({
  ActivityFeed: (): React.JSX.Element => <div data-testid="activity-feed">Activity Feed</div>,
}));

vi.mock("@/components/dashboard/TokenBudget", () => ({
  TokenBudget: (): React.JSX.Element => <div data-testid="token-budget">Token Budget</div>,
}));

// Mock hooks and API calls
vi.mock("@/lib/hooks", () => ({
  useWorkspaceId: (): string | null => "ws-test-123",
}));

vi.mock("@/lib/api/dashboard", () => ({
  fetchDashboardSummary: vi.fn().mockResolvedValue({
    metrics: {
      activeAgents: 5,
      tasksCompleted: 42,
      totalTasks: 100,
      tasksInProgress: 10,
      activeProjects: 3,
      errorRate: 0.5,
    },
    recentActivity: [],
    activeJobs: [],
    tokenBudget: [],
  }),
}));

describe("DashboardPage", (): void => {
  beforeEach((): void => {
    vi.clearAllMocks();
    vi.mocked(fetchDashboardSummary).mockResolvedValue({
      metrics: {
        activeAgents: 5,
        tasksCompleted: 42,
        totalTasks: 100,
        tasksInProgress: 10,
        activeProjects: 3,
        errorRate: 0.5,
      },
      recentActivity: [],
      activeJobs: [],
      tokenBudget: [],
    });
  });

  it("should render the DashboardMetrics widget", async (): Promise<void> => {
    render(<DashboardPage />);
    await waitFor((): void => {
      expect(screen.getByTestId("dashboard-metrics")).toBeInTheDocument();
    });
  });

  it("should render the OrchestratorSessions widget", async (): Promise<void> => {
    render(<DashboardPage />);
    await waitFor((): void => {
      expect(screen.getByTestId("orchestrator-sessions")).toBeInTheDocument();
    });
  });

  it("should render the QuickActions widget", async (): Promise<void> => {
    render(<DashboardPage />);
    await waitFor((): void => {
      expect(screen.getByTestId("quick-actions")).toBeInTheDocument();
    });
  });

  it("should render the ActivityFeed widget", async (): Promise<void> => {
    render(<DashboardPage />);
    await waitFor((): void => {
      expect(screen.getByTestId("activity-feed")).toBeInTheDocument();
    });
  });

  it("should render the TokenBudget widget", async (): Promise<void> => {
    render(<DashboardPage />);
    await waitFor((): void => {
      expect(screen.getByTestId("token-budget")).toBeInTheDocument();
    });
  });

  it("should render error state when API fails", async (): Promise<void> => {
    vi.mocked(fetchDashboardSummary).mockRejectedValueOnce(new Error("Network error"));
    render(<DashboardPage />);
    await waitFor((): void => {
      expect(screen.getByText("Failed to load dashboard data")).toBeInTheDocument();
    });
  });
});
