// ── Graph Statistics ─────────────────────────────────────────────────────────

export interface NodeTypeCount {
  label: string
  count: number
}

export interface RelTypeCount {
  type: string
  count: number
}

export interface GraphStatsResponse {
  total_nodes: number
  total_relationships: number
  orphan_nodes: number
  placeholder_count: number
  node_counts: NodeTypeCount[]
  rel_counts: RelTypeCount[]
}

export interface GraphHealthResponse {
  no_relation_ratio: number
  duplicate_candidates: number
  missing_in_graph: number
  total_es_docs: number
}

// ── Entity CRUD ─────────────────────────────────────────────────────────────

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

export interface EntityListResponse {
  items: EntityListItem[]
  total: number
  page: number
  page_size: number
}

export interface EntityNeighbor {
  id: string
  labels: string[]
  name: string
  rel_type: string
  direction: string
}

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

export interface EntityDetailResponse {
  id: string
  labels: string[]
  properties: Record<string, any>
  neighbors: EntityNeighbor[]
  related_docs: EntityRelatedDoc[]
}

export interface EntityCreateRequest {
  label: string
  properties: Record<string, any>
}

export interface EntityUpdateRequest {
  properties: Record<string, any>
}

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

// ── Duplicate Detection & Merge ─────────────────────────────────────────────

export interface DuplicateCandidate {
  entity_a_id: string
  entity_a_name: string
  entity_b_id: string
  entity_b_name: string
  similarity: number
  match_type: string
  label: string
}

export interface DuplicateListResponse {
  candidates: DuplicateCandidate[]
  total: number
}

export interface EntityMergeRequest {
  primary_id: string
  secondary_id: string
  add_alias?: boolean
}

export interface EntityMergeResponse {
  primary_id: string
  migrated_relationships: number
  aliases: string[]
}

// ── Relationships ───────────────────────────────────────────────────────────

export interface RelationshipItem {
  id: string
  type: string
  source_id: string
  source_name: string
  source_labels: string[]
  target_id: string
  target_name: string
  target_labels: string[]
  properties: Record<string, any>
}

export interface RelationshipListResponse {
  items: RelationshipItem[]
  total: number
  page: number
  page_size: number
}

export interface RelationshipCreateRequest {
  source_id: string
  target_id: string
  type: string
  properties?: Record<string, any>
}

// ── Rebuild ─────────────────────────────────────────────────────────────────

export interface RebuildResponse {
  task_id: string
  doc_count: number
}

export interface RebuildAllResponse {
  task_id: string
  total_docs: number
}

export interface RebuildStatusResponse {
  task_id: string
  status: string
  progress: number
  current: number
  total: number
  current_doc_id: string
  error?: string
}

// ── Placeholders ────────────────────────────────────────────────────────────

export interface PlaceholderReferencer {
  doc_id: string
  title: string
}

export interface PlaceholderItem {
  id: string
  doc_id: string
  doc_number: string
  referencers: PlaceholderReferencer[]
}

export interface PlaceholderListResponse {
  items: PlaceholderItem[]
  total: number
}

// ── Type Definitions ────────────────────────────────────────────────────────

export interface EntityTypeItem {
  name: string
  description: string
  zh_name?: string
  icon: string
  color: string
  key_property?: string
  properties_schema: Record<string, any>
  instance_count: number
}

export interface EntityTypeListResponse {
  items: EntityTypeItem[]
}

export interface EntityTypeCreateRequest {
  name: string
  description?: string
  icon?: string
  color?: string
  properties_schema?: Record<string, any>
}

export interface EntityTypeUpdateRequest {
  zh_name?: string
  icon?: string
  color?: string
}

export interface RelTypeItem {
  name: string
  description: string
  zh_name?: string
  source_labels: string[]
  target_labels: string[]
  instance_count: number
}

export interface RelTypeListResponse {
  items: RelTypeItem[]
}

export interface RelTypeCreateRequest {
  name: string
  description?: string
  source_labels?: string[]
  target_labels?: string[]
}

export interface RelTypeUpdateRequest {
  zh_name?: string
}
