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

import type { Material } from "../../../../src/web/features/inbox/types";

import { MaterialCard } from "../../../../src/web/features/inbox/components/MaterialCard";
import { renderWithProviders } from "../../test-utils";

const MOCK_MATERIAL: Material = {
  associatedDate: "2026-06-03",
  createdAt: new Date().toISOString(),
  description: "测试素材描述",
  id: "test-id",
};

describe("MaterialCard", () => {
  test("渲染素材描述和日期信息", () => {
    renderWithProviders(
      createElement(MaterialCard, {
        material: MOCK_MATERIAL,
        onDelete: vi.fn(),
        onSelect: vi.fn(),
        selected: false,
      }),
    );
    expect(screen.getByText("测试素材描述")).not.toBeNull();
    expect(screen.getByText(/2026-06-03/)).not.toBeNull();
  });

  test("点击卡片触发 onSelect", () => {
    const onSelect = vi.fn();
    renderWithProviders(
      createElement(MaterialCard, {
        material: MOCK_MATERIAL,
        onDelete: vi.fn(),
        onSelect,
        selected: false,
      }),
    );
    const card = screen.getByText("测试素材描述").closest(".ant-card")!;
    fireEvent.click(card);
    expect(onSelect).toHaveBeenCalledTimes(1);
  });

  test("点击删除按钮触发 onDelete 且不触发 onSelect", () => {
    const onDelete = vi.fn();
    const onSelect = vi.fn();
    renderWithProviders(
      createElement(MaterialCard, {
        material: MOCK_MATERIAL,
        onDelete,
        onSelect,
        selected: false,
      }),
    );
    fireEvent.click(screen.getByLabelText("删除"));
    expect(onDelete).toHaveBeenCalledTimes(1);
    expect(onSelect).not.toHaveBeenCalled();
  });

  test("选中状态添加 app-inbox-card-selected 类", () => {
    renderWithProviders(
      createElement(MaterialCard, {
        material: MOCK_MATERIAL,
        onDelete: vi.fn(),
        onSelect: vi.fn(),
        selected: true,
      }),
    );
    const card = screen.getByText("测试素材描述").closest(".app-inbox-card-selected");
    expect(card).not.toBeNull();
  });
});
