import { fireEvent, screen, waitFor } from "@testing-library/react";
import { describe, expect, mock, test } from "bun:test";
import { createElement } from "react";

import type { Model, Provider } from "../../../src/shared/api";

import { ModelTable } from "../../../src/web/pages/models/components/ModelTable";
import { renderWithProviders } from "../test-utils";

const ENABLED_PROVIDER: Provider = {
  apiKey: "sk-test",
  baseUrl: "https://api.openai.com/v1",
  createdAt: "2024-01-01T00:00:00.000Z",
  enabled: true,
  id: "pv1",
  name: "OpenAI",
  type: "openai",
  updatedAt: "2024-01-01T00:00:00.000Z",
};

const DISABLED_PROVIDER: Provider = {
  apiKey: "sk-off",
  baseUrl: "https://api.deepseek.com/v1",
  createdAt: "2024-01-01T00:00:00.000Z",
  enabled: false,
  id: "pv2",
  name: "DeepSeek",
  type: "openai-compatible",
  updatedAt: "2024-01-01T00:00:00.000Z",
};

const ENABLED_MODEL: Model = {
  capabilities: ["text", "reasoning"],
  contextLength: 128000,
  createdAt: "2024-01-01T00:00:00.000Z",
  enabled: true,
  id: "m1",
  maxOutputTokens: 4096,
  modelId: "gpt-4o",
  name: "GPT-4o",
  providerId: "pv1",
  updatedAt: "2024-01-01T00:00:00.000Z",
};

const DISABLED_MODEL: Model = {
  capabilities: ["text"],
  contextLength: null,
  createdAt: "2024-01-01T00:00:00.000Z",
  enabled: false,
  id: "m2",
  maxOutputTokens: null,
  modelId: "deepseek-chat",
  name: "DeepSeek Chat",
  providerId: "pv2",
  updatedAt: "2024-01-01T00:00:00.000Z",
};

function clickLatestConfirmButton() {
  const buttons = screen.getAllByRole("button", { name: /OK|确定/ });
  fireEvent.click(buttons[buttons.length - 1]!);
}

describe("ModelTable", () => {
  test("渲染模型表格数据", () => {
    renderWithProviders(
      createElement(ModelTable, {
        data: { items: [ENABLED_MODEL, DISABLED_MODEL], page: 1, pageSize: 20, total: 2 },
        loading: false,
        onDelete: () => Promise.resolve(),
        onDisable: () => Promise.resolve(),
        onEdit: () => undefined,
        onEnable: () => Promise.resolve(),
        onPageChange: () => undefined,
        page: 1,
        pageSize: 20,
        providers: [ENABLED_PROVIDER, DISABLED_PROVIDER],
      }),
    );

    expect(screen.getByText("GPT-4o")).not.toBeNull();
    expect(screen.getByText("gpt-4o")).not.toBeNull();
    expect(screen.getByText("DeepSeek Chat")).not.toBeNull();
    expect(screen.getByText("OpenAI")).not.toBeNull();
    expect(screen.getByText("DeepSeek")).not.toBeNull();
  });

  test("模型表格操作触发 enable/disable/delete", async () => {
    const onDisable = mock(() => Promise.resolve());
    const onEnable = mock(() => Promise.resolve());
    const onDelete = mock(() => Promise.resolve());

    renderWithProviders(
      createElement(ModelTable, {
        data: { items: [ENABLED_MODEL, DISABLED_MODEL], page: 1, pageSize: 20, total: 2 },
        loading: false,
        onDelete,
        onDisable,
        onEdit: () => undefined,
        onEnable,
        onPageChange: () => undefined,
        page: 1,
        pageSize: 20,
        providers: [ENABLED_PROVIDER, DISABLED_PROVIDER],
      }),
    );

    const disableButtons = screen.getAllByRole("button", { name: /禁用/ });
    fireEvent.click(disableButtons[0]!);
    await waitFor(() => expect(screen.getByText("确认禁用此模型？")).not.toBeNull());
    clickLatestConfirmButton();
    await waitFor(() => expect(onDisable).toHaveBeenCalledWith("m1"));

    const enableButtons = screen.getAllByRole("button", { name: /启用/ });
    fireEvent.click(enableButtons[0]!);
    await waitFor(() => expect(onEnable).toHaveBeenCalledWith("m2"));
  });
});
