// ── Core graph structures ─────────────────────────────────────────────────────

export interface GraphNode {
  id: string
  labels: string[]            // Neo4j labels, e.g. ["Organization"]
  properties: Record<string, any>
  // Optional UI helpers (added client-side before rendering)
  _label?: string             // display label (name or doc title)
  _type?: string              // primary label for colour/icon mapping
  degree?: number
  // Backend pre-computed layout positions (used with preset layout)
  x?: number
  y?: number
}

export interface GraphEdge {
  source: string
  target: string
  type: string                // relationship type, e.g. "ISSUED"
  properties?: Record<string, any>
}

export interface GraphData {
  nodes: GraphNode[]
  edges: GraphEdge[]
}

/** 分页版 GraphData，用于支持渐进式加载 */
export interface PaginatedGraphData extends GraphData {
  next_cursor?: string
  total_count?: number
}

// ── G6 渲染用数据结构 ─────────────────────────────────────────────────────────

/** G6 节点数据（在 GraphNode 基础上补充渲染必需字段） */
export interface G6NodeData extends GraphNode {
  _type: string   // 主标签，用于颜色/图标映射
  _label: string  // 画布上显示的文本
}

/** G6 边数据（重命名 type → relType，因 G6 v5 保留 type 字段） */
export interface G6EdgeData {
  id: string
  source: string
  target: string
  relType: string
}

// ── Entity search ─────────────────────────────────────────────────────────────

export interface EntitySearchResult {
  id: string
  labels: string[]
  name: string
  properties: Record<string, any>
}

// ── Entity detail ─────────────────────────────────────────────────────────────

export interface EntityDetail {
  id: string
  labels: string[]
  properties: Record<string, any>
  connections: {
    type: string
    neighbor_id: string
    neighbor_labels: string[]
    neighbor_name: string
  }[]
}

export interface EntityDetailResponse {
  entity: EntityDetail
  graph: GraphData
}

// ── Related documents ─────────────────────────────────────────────────────────

export interface RelatedDocItem {
  doc_id: string
  title: string
  doc_number: string
  rel_type: string
}

// ── Governance / document enhancement ───────────────────────────────────────

export interface GovernanceDocItem {
  doc_id: string
  title: string
  doc_code: string
  publish_date: string
  shared_entity?: string
  theme_name?: string
  status?: string
  is_placeholder?: boolean
}

export interface ChainEdge {
  from_doc_id: string
  to_doc_id: string
  rel_type: string
}

export interface DocumentRecommendations {
  same_org: GovernanceDocItem[]
  same_theme: GovernanceDocItem[]
  same_region: GovernanceDocItem[]
}

export interface PolicyChainResponse {
  chain: GovernanceDocItem[]
  edges: ChainEdge[]
}

export interface RevisionHistoryResponse {
  documents: GovernanceDocItem[]
  edges: ChainEdge[]
}

export interface SameThemeDocItem {
  doc_id: string
  title: string
  doc_code: string
  publish_date: string
  theme_name: string
  status: string
}
