import { describe, expect, it } from "vitest";
import type { DocumentRecord } from "@/lib/documents";
import { buildKernelFileTreeProjection } from "@/lib/kernel-file-tree";
import type { MediaAsset } from "@/types/media";
import {
  buildSidebarLocalFileTreeProjection,
  moveSidebarDocumentRecord,
  removeSidebarDocumentRecords,
  sidebarTreeNodeToDocumentRecord,
  upsertSidebarDocumentRecord,
} from "./sidebar-local-projection";

function documentRecord(input: Partial<DocumentRecord> & { id: string }): DocumentRecord {
  return {
    access_scope: "private",
    id: input.id,
    workspace_id: input.workspace_id ?? "ws_1",
    title: input.title ?? input.id,
    parent_id: input.parent_id ?? null,
    sort_order: input.sort_order ?? 0,
    is_starred: input.is_starred ?? false,
    is_template: input.is_template ?? false,
    created_at: input.created_at ?? "2026-05-14T00:00:00.000Z",
    updated_at: input.updated_at ?? null,
  };
}

function mindmapAsset(input: Partial<MediaAsset> & { id: string; document_id: string }): MediaAsset {
  return {
    id: input.id,
    workspace_id: input.workspace_id ?? "ws_1",
    document_id: input.document_id,
    asset_type: "mindmap",
    file_url: input.file_url ?? `/documents/${input.document_id}/mindmap-${input.id}.json`,
    thumbnail_url: null,
    bucket: null,
    storage_path: null,
    file_name: input.file_name ?? `mindmap-${input.id}.json`,
    file_size: null,
    mime_type: "application/json",
    ocr_payload: undefined,
    ocr_strategy: null,
    ocr_text: null,
    ocr_status: null,
    signed_url: null,
    created_at: input.created_at ?? "2026-05-14T00:00:00.000Z",
    updated_at: input.updated_at ?? "2026-05-14T00:00:00.000Z",
  };
}

describe("sidebar-local-projection", () => {
  it("新建页面本地 upsert 后应能生成 File Tree document/index 行", () => {
    const baseProjection = buildKernelFileTreeProjection({
      documents: [documentRecord({ id: "root", sort_order: 0 })],
      mediaAssets: [],
      mindmapAssets: [],
      tableAssets: [],
      mindmapAssetChildren: {},
    });
    const created = sidebarTreeNodeToDocumentRecord(
      {
        id: "created",
        workspace_id: "ws_1",
        title: "新页面",
        parent_id: "root",
        sort_order: 1,
        access_scope: "private",
        is_starred: false,
        is_template: false,
        created_at: "2026-05-14T00:01:00.000Z",
        updated_at: "2026-05-14T00:01:00.000Z",
        children: [],
      },
      "ws_1",
    );
    const documents = upsertSidebarDocumentRecord([documentRecord({ id: "root", sort_order: 0 })], created);
    const projection = buildSidebarLocalFileTreeProjection({
      baseProjection,
      documents,
      mediaAssets: [],
      mindmapAssets: [],
      tableAssets: [],
      mindmapAssetChildren: {},
    });

    expect(projection.items.map((item) => item.rowId)).toContain("doc:created");
    expect(projection.items.map((item) => item.rowId)).toContain("index:created");
  });

  it("删除页面本地移除后应同时移除其子树 File Tree 行", () => {
    const root = documentRecord({ id: "root" });
    const child = documentRecord({ id: "child", parent_id: "root" });
    const grandchild = documentRecord({ id: "grandchild", parent_id: "child" });
    const documents = removeSidebarDocumentRecords([root, child, grandchild], ["child"]);
    const projection = buildSidebarLocalFileTreeProjection({
      baseProjection: buildKernelFileTreeProjection({
        documents: [root, child, grandchild],
        mediaAssets: [],
        mindmapAssets: [],
        tableAssets: [],
        mindmapAssetChildren: {},
      }),
      documents,
      mediaAssets: [],
      mindmapAssets: [],
      tableAssets: [],
      mindmapAssetChildren: {},
    });

    const rowIds = projection.items.map((item) => item.rowId);
    expect(rowIds).toContain("doc:root");
    expect(rowIds).not.toContain("doc:child");
    expect(rowIds).not.toContain("index:grandchild");
  });

  it("新建 mindmap asset 后应能生成 File Tree asset 行", () => {
    const root = documentRecord({ id: "root" });
    const projection = buildSidebarLocalFileTreeProjection({
      baseProjection: buildKernelFileTreeProjection({
        documents: [root],
        mediaAssets: [],
        mindmapAssets: [],
        tableAssets: [],
        mindmapAssetChildren: {},
      }),
      documents: [root],
      mediaAssets: [],
      mindmapAssets: [mindmapAsset({ id: "mind_1", document_id: "root" })],
      tableAssets: [],
      mindmapAssetChildren: {},
    });

    const assetRow = projection.items.find((item) => item.resourceMeta.assetId === "mind_1");
    expect(assetRow?.rowId).toBe("asset:mind_1");
    expect(assetRow?.resourceMeta.assetKind).toBe("mindmap");
  });

  it("移动页面本地更新后 File Tree row 的父级应同步变化", () => {
    const root = documentRecord({ id: "root", sort_order: 0 });
    const target = documentRecord({ id: "target", parent_id: null, sort_order: 1 });
    const moved = moveSidebarDocumentRecord([root, target], "target", "root", 0);
    const projection = buildSidebarLocalFileTreeProjection({
      baseProjection: buildKernelFileTreeProjection({
        documents: [root, target],
        mediaAssets: [],
        mindmapAssets: [],
        tableAssets: [],
        mindmapAssetChildren: {},
      }),
      documents: moved,
      mediaAssets: [],
      mindmapAssets: [],
      tableAssets: [],
      mindmapAssetChildren: {},
    });

    const targetRow = projection.items.find((item) => item.rowId === "doc:target");
    expect(targetRow?.parentNodeId).toBe("root");
  });
});
