import { fireEvent, screen, waitFor } 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 { AddMaterialModal } from "../../../../src/web/features/inbox/components/AddMaterialModal";
import { renderWithProviders } from "../../test-utils";

describe("AddMaterialModal", () => {
  test("打开时渲染表单字段", () => {
    renderWithProviders(
      createElement(AddMaterialModal, {
        onAdd: vi.fn(),
        onOpenChange: vi.fn(),
        open: true,
      }),
    );
    expect(screen.getByText("新增素材")).not.toBeNull();
    expect(screen.getByText("描述")).not.toBeNull();
    expect(screen.getByText("关联时间")).not.toBeNull();
  });

  test("关闭时不渲染", () => {
    renderWithProviders(
      createElement(AddMaterialModal, {
        onAdd: vi.fn(),
        onOpenChange: vi.fn(),
        open: false,
      }),
    );
    expect(screen.queryByText("新增素材")).toBeNull();
  });

  test("提交空表单显示必填校验", async () => {
    renderWithProviders(
      createElement(AddMaterialModal, {
        onAdd: vi.fn(),
        onOpenChange: vi.fn(),
        open: true,
      }),
    );

    fireEvent.click(screen.getByText("确 定"));

    await waitFor(() => {
      expect(screen.getByText("请输入描述")).not.toBeNull();
    });
  });

  test("点击确定触发表单提交", async () => {
    const onAdd = vi.fn<(material: Material) => void>();
    renderWithProviders(
      createElement(AddMaterialModal, {
        onAdd,
        onOpenChange: vi.fn(),
        open: true,
      }),
    );

    const textarea = screen.getByPlaceholderText("请输入素材描述");
    fireEvent.change(textarea, { target: { value: "测试描述" } });

    fireEvent.click(screen.getByText("确 定"));

    await waitFor(() => {
      expect(onAdd).toHaveBeenCalledTimes(1);
    });

    const callArgs = onAdd.mock.calls[0];
    expect(callArgs).toBeDefined();
    const calledMaterial = callArgs![0];
    expect(calledMaterial.description).toBe("测试描述");
    expect(calledMaterial.associatedDate).toMatch(/^\d{4}-\d{2}-\d{2}$/);
    expect(calledMaterial.id).toBeTruthy();
  });
});
