import { buildPageAggregate, type PageAggregateProjection } from "@/lib/documents/page-aggregate";
import { normalizeDocumentContentResponse } from "@/lib/documents/page-subtree-response";
import type { PageSubtreeProjection } from "@/lib/documents/page-subtree";
import type {
  DocumentStats,
  PageFont,
  PageLayoutDensity,
  PageOptionsState,
} from "@/types/page-options";

export type DocumentMetaPayload = {
  id: string;
  workspace_id: string;
  title: string | null;
  updated_at: string | null;
  can_edit?: boolean | null;
  disable_download?: boolean | null;
  disable_copy?: boolean | null;
  wide_layout?: boolean | null;
  use_small_text?: boolean | null;
  show_heading_numbers?: boolean | null;
  show_toc?: boolean | null;
  show_structure?: boolean | null;
  protect_editing?: boolean | null;
  show_word_count?: boolean | null;
  collapse_backlinks?: boolean | null;
  page_font?: PageFont | null;
  layout_density?: PageLayoutDensity | null;
  hide_child_pages?: boolean | null;
  show_block_ref_count?: boolean | null;
  embed_default_block_id?: string | null;
  word_count?: number | null;
  character_count?: number | null;
  block_count?: number | null;
  todo_total?: number | null;
  todo_total_count?: number | null;
  todo_done?: number | null;
  todo_done_count?: number | null;
};

export type DocumentContentPayload = {
  content?: unknown;
  revision?: number | null;
  conflict_detection_key?: string | null;
  conflictDetectionKey?: string | null;
  page_subtree?: PageSubtreeProjection | null;
  pageSubtree?: PageSubtreeProjection | null;
  title?: string | null;
};

export function buildPageAggregateFromDocumentPayloads(input: {
  meta: DocumentMetaPayload;
  contentPayload?: DocumentContentPayload | null;
}): PageAggregateProjection {
  const normalizedContent = normalizeDocumentContentResponse({
    documentId: input.meta.id,
    title: input.meta.title ?? "无标题",
    payload: input.contentPayload,
  });

  const pageOptions: PageOptionsState = {
    wideLayout: input.meta.wide_layout ?? false,
    smallText: input.meta.use_small_text ?? false,
    showHeadingNumbers: input.meta.show_heading_numbers ?? true,
    showToc: input.meta.show_toc ?? false,
    showStructure: input.meta.show_structure ?? false,
    protectEditing: input.meta.protect_editing ?? false,
    showWordCount: input.meta.show_word_count ?? true,
    collapseBacklinks: input.meta.collapse_backlinks ?? false,
    pageFont: input.meta.page_font ?? "default",
    layoutDensity: input.meta.layout_density ?? "normal",
    hideChildPages: input.meta.hide_child_pages ?? false,
    showBlockRefCount: input.meta.show_block_ref_count ?? false,
    embedDefaultBlockId: input.meta.embed_default_block_id ?? null,
  };

  const stats: DocumentStats = {
    wordCount: input.meta.word_count ?? 0,
    characterCount: input.meta.character_count ?? 0,
    blockCount: input.meta.block_count ?? 0,
    todoTotal: input.meta.todo_total ?? input.meta.todo_total_count ?? 0,
    todoDone: input.meta.todo_done ?? input.meta.todo_done_count ?? 0,
  };

  return buildPageAggregate({
    documentId: input.meta.id,
    workspaceId: input.meta.workspace_id,
    title: input.meta.title ?? "无标题",
    updatedAt: input.meta.updated_at,
    readOnly: input.meta.can_edit === false,
    disableDownload: Boolean(input.meta.disable_download),
    disableCopy: Boolean(input.meta.disable_copy),
    pageOptions,
    content: normalizedContent.content,
    revision: normalizedContent.revision,
    conflictDetectionKey: normalizedContent.conflictDetectionKey,
    pageSubtree: normalizedContent.pageSubtree,
    stats,
  });
}
