import { describe, expect, it } from "vitest";
import type { FileTreeRow } from "@/lib/file-tree/types";
import { buildParentById } from "./dnd";
import { computeFileTreeDeleteTargets } from "./delete";

function makeDoc(id: string, parent_id: string | null): any {
  return {
    id,
    parent_id,
    title: id,
    access_scope: "private",
    icon: null,
    cover: null,
    is_template: false,
    user_id: "u1",
    workspace_id: "w1",
    created_at: "",
    updated_at: "",
    children: [],
  };
}

describe("computeFileTreeDeleteTargets", () => {
  it("去掉被父级页面覆盖的子页面", () => {
    const docA = makeDoc("A", null);
    const docB = makeDoc("B", "A");
    const rows: FileTreeRow[] = [
      { rowId: "doc:A", kind: "doc", docId: "A", parentDocId: null, node: docA, depth: 0, hasChildren: true, isExpanded: true },
      { rowId: "index:A", kind: "index", docId: "A", parentDocId: "A", node: docA, depth: 1 },
      { rowId: "doc:B", kind: "doc", docId: "B", parentDocId: "A", node: docB, depth: 1, hasChildren: false, isExpanded: false },
      { rowId: "index:B", kind: "index", docId: "B", parentDocId: "B", node: docB, depth: 2 },
    ];
    const parentById = buildParentById([
      { id: "A", parentId: null },
      { id: "B", parentId: "A" },
    ]);

    const selectedRowIds = new Set(["doc:A", "doc:B"]);
    const result = computeFileTreeDeleteTargets({ visibleRows: rows, selectedRowIds, parentById });
    expect(result.docIds).toEqual(["A"]);
  });

  it("选中 index 行等价于选中页面本身（去重）", () => {
    const docA = makeDoc("A", null);
    const rows: FileTreeRow[] = [
      { rowId: "doc:A", kind: "doc", docId: "A", parentDocId: null, node: docA, depth: 0, hasChildren: false, isExpanded: false },
      { rowId: "index:A", kind: "index", docId: "A", parentDocId: "A", node: docA, depth: 1 },
    ];
    const parentById = buildParentById([{ id: "A", parentId: null }]);
    const selectedRowIds = new Set(["doc:A", "index:A"]);
    const result = computeFileTreeDeleteTargets({ visibleRows: rows, selectedRowIds, parentById });
    expect(result.docIds).toEqual(["A"]);
  });

  it("如果页面被删除，则跳过同页面下的附件删除（避免重复/无效操作）", () => {
    const docA = makeDoc("A", null);
    const rows: FileTreeRow[] = [
      { rowId: "doc:A", kind: "doc", docId: "A", parentDocId: null, node: docA, depth: 0, hasChildren: true, isExpanded: true },
      { rowId: "index:A", kind: "index", docId: "A", parentDocId: "A", node: docA, depth: 1 },
      {
        rowId: "asset:1",
        kind: "asset",
        docId: "A",
        parentDocId: "A",
        asset: {
          id: "1",
          workspace_id: "w1",
          document_id: "A",
          asset_type: "file",
          file_url: "https://example.com/1",
          thumbnail_url: null,
          bucket: "b",
          storage_path: "p",
          file_name: "a.txt",
          file_size: 1,
          mime_type: "text/plain",
          ocr_payload: undefined,
          ocr_strategy: null,
          ocr_text: null,
          ocr_status: null,
          signed_url: null,
          created_at: "",
          updated_at: "",
        },
        depth: 2,
      },
    ];
    const parentById = buildParentById([{ id: "A", parentId: null }]);
    const selectedRowIds = new Set(["doc:A", "asset:1"]);
    const result = computeFileTreeDeleteTargets({ visibleRows: rows, selectedRowIds, parentById });
    expect(result.docIds).toEqual(["A"]);
    expect(result.assetIds).toEqual([]);
  });
});
