import request from './request'
import type {
  GraphStatsResponse,
  GraphHealthResponse,
  EntityListResponse,
  EntityDetailResponse,
  EntityCreateRequest,
  EntityCreateResponse,
  EntityUpdateRequest,
  DuplicateListResponse,
  EntityMergeRequest,
  EntityMergeResponse,
  RelationshipListResponse,
  RelationshipCreateRequest,
  RebuildResponse,
  RebuildAllResponse,
  RebuildStatusResponse,
  PlaceholderListResponse,
  EntityTypeListResponse,
  EntityTypeItem,
  EntityTypeCreateRequest,
  EntityTypeUpdateRequest,
  RelTypeListResponse,
  RelTypeItem,
  RelTypeCreateRequest,
  RelTypeUpdateRequest,
} from '@/types/admin-graph'

const BASE = '/api/ai/v1/admin/graph'

// ── Statistics ──────────────────────────────────────────────────────────────

export function getGraphStats(): Promise<GraphStatsResponse> {
  return request.get<GraphStatsResponse>(`${BASE}/stats`).then((res) => res.data)
}

export function getGraphHealth(): Promise<GraphHealthResponse> {
  return request.get<GraphHealthResponse>(`${BASE}/health`).then((res) => res.data)
}

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

export function listEntities(params: {
  label?: string
  name?: string
  page?: number
  page_size?: number
  sort_by?: string
}): Promise<EntityListResponse> {
  return request
    .get<EntityListResponse>(`${BASE}/entities`, { params })
    .then((res) => res.data)
}

export function getEntityDetail(entityId: string): Promise<EntityDetailResponse> {
  return request
    .get<EntityDetailResponse>(`${BASE}/entities/${entityId}`)
    .then((res) => res.data)
}

export function createEntity(body: EntityCreateRequest): Promise<EntityCreateResponse> {
  return request
    .post<EntityCreateResponse>(`${BASE}/entities`, body)
    .then((res) => res.data)
}

export function updateEntity(
  entityId: string,
  body: EntityUpdateRequest,
): Promise<Record<string, any>> {
  return request.put(`${BASE}/entities/${entityId}`, body).then((res) => res.data)
}

export function deleteEntity(entityId: string): Promise<{ id: string; deleted_relationships: number }> {
  return request.delete(`${BASE}/entities/${entityId}`).then((res) => res.data)
}

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

export function detectDuplicates(params: {
  label?: string
  threshold?: number
  limit?: number
}): Promise<DuplicateListResponse> {
  return request
    .get<DuplicateListResponse>(`${BASE}/duplicates`, { params })
    .then((res) => res.data)
}

export function mergeEntities(body: EntityMergeRequest): Promise<EntityMergeResponse> {
  return request.post<EntityMergeResponse>(`${BASE}/entities/merge`, body).then((res) => res.data)
}

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

export function rebuildDocGraphs(doc_ids: string[]): Promise<RebuildResponse> {
  return request
    .post<RebuildResponse>(`${BASE}/rebuild`, { doc_ids })
    .then((res) => res.data)
}

export function rebuildAllGraphs(): Promise<RebuildAllResponse> {
  return request.post<RebuildAllResponse>(`${BASE}/rebuild-all`).then((res) => res.data)
}

export function getRebuildStatus(taskId: string): Promise<RebuildStatusResponse> {
  return request
    .get<RebuildStatusResponse>(`${BASE}/rebuild-status/${taskId}`)
    .then((res) => res.data)
}

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

export function listRelationships(params: {
  type?: string
  source_id?: string
  target_id?: string
  page?: number
  page_size?: number
}): Promise<RelationshipListResponse> {
  return request
    .get<RelationshipListResponse>(`${BASE}/relationships`, { params })
    .then((res) => res.data)
}

export function createRelationship(body: RelationshipCreateRequest): Promise<Record<string, any>> {
  return request.post(`${BASE}/relationships`, body).then((res) => res.data)
}

export function deleteRelationship(relId: string): Promise<{ id: string; deleted: boolean }> {
  return request.delete(`${BASE}/relationships/${relId}`).then((res) => res.data)
}

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

export function listPlaceholders(): Promise<PlaceholderListResponse> {
  return request.get<PlaceholderListResponse>(`${BASE}/placeholders`).then((res) => res.data)
}

export function linkPlaceholder(
  placeholderId: string,
  realDocId: string,
): Promise<{ placeholder_id: string; real_doc_id: string; migrated_relationships: number }> {
  return request
    .post(`${BASE}/placeholders/${placeholderId}/link`, { real_doc_id: realDocId })
    .then((res) => res.data)
}

export function deletePlaceholder(placeholderId: string): Promise<{ id: string; deleted: boolean }> {
  return request.delete(`${BASE}/placeholders/${placeholderId}`).then((res) => res.data)
}

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

export function listEntityTypes(): Promise<EntityTypeListResponse> {
  return request.get<EntityTypeListResponse>(`${BASE}/entity-types`).then((res) => res.data)
}

export function createEntityType(body: EntityTypeCreateRequest): Promise<EntityTypeItem> {
  return request.post<EntityTypeItem>(`${BASE}/entity-types`, body).then((res) => res.data)
}

export function updateEntityType(
  name: string,
  body: EntityTypeUpdateRequest,
): Promise<Record<string, any>> {
  return request.put(`${BASE}/entity-types/${name}`, body).then((res) => res.data)
}

export function deleteEntityType(name: string): Promise<{ name: string; deleted: boolean }> {
  return request.delete(`${BASE}/entity-types/${name}`).then((res) => res.data)
}

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

export function listRelTypes(): Promise<RelTypeListResponse> {
  return request.get<RelTypeListResponse>(`${BASE}/relationship-types`).then((res) => res.data)
}

export function createRelType(body: RelTypeCreateRequest): Promise<RelTypeItem> {
  return request.post<RelTypeItem>(`${BASE}/relationship-types`, body).then((res) => res.data)
}

export function updateRelType(
  name: string,
  body: RelTypeUpdateRequest,
): Promise<Record<string, any>> {
  return request.put(`${BASE}/relationship-types/${name}`, body).then((res) => res.data)
}

export function deleteRelType(name: string): Promise<{ name: string; deleted: boolean }> {
  return request.delete(`${BASE}/relationship-types/${name}`).then((res) => res.data)
}
