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

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

describe("InboxPage", () => {
  test("初始状态显示空状态提示", () => {
    renderWithProviders(createElement(InboxPage));
    expect(screen.getByText("暂无素材")).not.toBeNull();
    expect(screen.getByText("请在左侧选择素材")).not.toBeNull();
  });

  test("新增素材后列表追加且自动选中", async () => {
    renderWithProviders(createElement(InboxPage));

    fireEvent.click(screen.getByRole("button", { name: /新增素材/ }));

    await waitFor(() => {
      expect(screen.getByText("新增素材", { selector: ".ant-modal-title" })).not.toBeNull();
    });

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

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

    await waitFor(() => {
      expect(screen.getByText("新增的素材")).not.toBeNull();
    });

    expect(screen.getByText("素材详情")).not.toBeNull();
    expect(screen.queryByText("暂无素材")).toBeNull();
    expect(screen.queryByText("请在左侧选择素材")).toBeNull();
  });

  test("删除素材后列表更新", async () => {
    renderWithProviders(createElement(InboxPage));

    fireEvent.click(screen.getByRole("button", { name: /新增素材/ }));

    await waitFor(() => {
      expect(screen.getByText("新增素材", { selector: ".ant-modal-title" })).not.toBeNull();
    });

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

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

    await waitFor(() => {
      expect(screen.getByText("待删除的素材")).not.toBeNull();
    });

    fireEvent.click(screen.getByLabelText("删除"));

    await waitFor(() => {
      expect(screen.getByText("暂无素材")).not.toBeNull();
      expect(screen.getByText("请在左侧选择素材")).not.toBeNull();
    });
  });
});
