/* eslint-disable @typescript-eslint/no-require-imports */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import "../../../tests/web/test-utils";
import { render } from "@testing-library/react";
import { describe, expect, test, vi } from "bun:test";

import { App } from "../../../src/web/app";

// Mock hooks
void vi.mock("../../../src/web/hooks/use-queries", () => ({
  useDashboard: vi.fn(() => ({
    data: {
      summary: {
        down: 0,
        incidents: 0,
        lastCheckTime: "2025-01-15T10:00:00.000Z",
        total: 0,
        up: 0,
        window: {
          from: "2025-01-14T10:00:00.000Z",
          label: "24h",
          to: "2025-01-15T10:00:00.000Z",
        },
      },
      targets: [],
    },
    dataUpdatedAt: Date.now(),
    error: null,
    isFetching: false,
    isLoading: false,
    refetch: vi.fn(),
  })),
  useMeta: vi.fn(() => ({
    data: { checkerTypes: ["http", "cmd"] },
  })),
}));

void vi.mock("../../../src/web/hooks/use-target-detail", () => ({
  useTargetDetail: vi.fn(() => ({
    activeTab: "overview",
    closeDrawer: vi.fn(),
    handlePageChange: vi.fn(),
    handleTabChange: vi.fn(),
    handleTimeChange: vi.fn(),
    historyData: {
      items: [],
      page: 1,
      pageSize: 20,
      total: 0,
    },
    historyLoading: false,
    metricsData: null,
    metricsLoading: false,
    openDrawer: vi.fn(),
    selectedTarget: null,
    timeFrom: "",
    timeTo: "",
  })),
}));

describe("App", () => {
  test("渲染不崩溃", () => {
    const { container } = render(<App />);
    expect(container.firstChild).not.toBeNull();
  });

  test("loading 状态不崩溃", () => {
    const { useDashboard } = require("../../../src/web/hooks/use-queries");
    useDashboard.mockReturnValue({
      data: null,
      dataUpdatedAt: 0,
      error: null,
      isFetching: true,
      isLoading: true,
      refetch: vi.fn(),
    });

    const { container } = render(<App />);
    expect(container.firstChild).not.toBeNull();
  });

  test("错误状态不崩溃", () => {
    const { useDashboard } = require("../../../src/web/hooks/use-queries");
    useDashboard.mockReturnValue({
      data: null,
      dataUpdatedAt: 0,
      error: { message: "Network error" },
      isFetching: false,
      isLoading: false,
      refetch: vi.fn(),
    });

    const { container } = render(<App />);
    expect(container.firstChild).not.toBeNull();
  });

  test("有数据状态不崩溃", () => {
    const { useDashboard } = require("../../../src/web/hooks/use-queries");
    useDashboard.mockReturnValue({
      data: {
        summary: {
          down: 1,
          incidents: 0,
          lastCheckTime: "2025-01-15T10:00:00.000Z",
          total: 2,
          up: 1,
          window: {
            from: "2025-01-14T10:00:00.000Z",
            label: "24h",
            to: "2025-01-15T10:00:00.000Z",
          },
        },
        targets: [],
      },
      dataUpdatedAt: Date.now(),
      error: null,
      isFetching: false,
      isLoading: false,
      refetch: vi.fn(),
    });

    const { container } = render(<App />);
    expect(container.firstChild).not.toBeNull();
  });
});
