import { request } from '@/api/request'

/** 业务系统基础信息 */
export interface BusinessSystemItem {
  id: number
  code: string
  name: string
  system_type: string
  owner_user_id?: number
  department_id?: number
  description?: string
  status: string
  is_builtin: boolean
  health_score?: number
  health_level?: string
  resource_count?: number
  active_incident_count?: number
  created_at?: string
  updated_at?: string
}

/** 分页响应 */
export interface PageResult<T> {
  total: number
  page: number
  page_size: number
  data: T[]
}

/** 业务系统列表查询参数 */
export interface BusinessSystemListParams {
  page?: number
  size?: number
  keyword?: string
  status?: string
}

/** 健康分扣分明细 */
export interface BusinessHealthFactor {
  key: string
  label: string
  score_delta: number
  status: string
  description: string
}

/** 业务健康摘要 */
export interface BusinessHealth {
  business_system_id: number
  health: {
    score: number
    level: string
    deductions: Record<string, number>
  }
  signal_counts: {
    resources: number
    url_probes: number
    active_incidents: number
    sla_status: string
  }
}

/** 业务系统资源成员 */
export interface BusinessResourceMember {
  id: number
  resource_uid: string
  resource_id?: number
  role: string
  criticality: string
  weight: number
  description?: string
}

/** 业务拓扑资源 */
export interface BusinessTopologyResource {
  id: number
  resource_uid: string
  resource_category: string
  service_identity: string
  display_name: string
  description?: string
  status: string
  business_system_id?: number
  owner_user_id?: number
  department_id?: number
  asset_id?: number
  tags?: string
  source_service?: string
  source_table?: string
  source_id?: number
  created_at?: string
  updated_at?: string
}

/** 业务拓扑节点 */
export interface BusinessTopologyNode {
  id?: number
  node_id: string
  resource_uid: string
  label: string
  type: string
  status: string
}

/** 业务拓扑边 */
export interface BusinessTopologyEdge {
  id?: number
  source_node_id: string
  target_node_id: string
  topology_id?: number
}

/** 业务系统依赖关系 */
export interface BusinessDependency {
  id: number
  business_system_id: number
  depends_on_business_system_id: number
  dependency_type: string
  criticality: string
  description?: string
}

/** 业务拓扑 */
export interface BusinessTopology {
  business_system_id: number
  resources: BusinessTopologyResource[] | null
  dependencies: BusinessDependency[] | null
  nodes: BusinessTopologyNode[] | null
  links: BusinessTopologyEdge[] | null
}

/** 业务时间线项 */
export interface BusinessTimelineItem {
  source: 'raw_event' | 'alert' | 'incident' | 'ticket' | 'note' | 'document'
  source_id: string
  occurred_at: string
  title: string
  message: string
  severity?: string
  status?: string
  metadata?: Record<string, any>
}

/** 业务笔记 */
export interface BusinessNote {
  id: number
  business_system_id: number
  title?: string
  content: string
  author_user_id?: number
  author_name?: string
  visibility: string
  occurred_at: string
  created_at: string
}

/** 关联知识或文档 */
export interface BusinessDocumentLink {
  id: number
  business_system_id: number
  title: string
  url: string
  link_type: string
  description?: string
  created_at: string
}

/** 列表响应 */
export interface ListResult<T> {
  list: T[] | null
  count: number
}

/** 获取业务系统列表 */
export const fetchBusinessSystems = (params?: BusinessSystemListParams) =>
  request.get<{ code?: number; details?: PageResult<BusinessSystemItem>; message?: string }>('/DC-Control/v1/business-systems', { params })

/** 获取业务系统详情 */
export const fetchBusinessSystemDetail = (id: number) =>
  request.get<{ code?: number; details?: BusinessSystemItem; message?: string }>(`/DC-Control/v1/business-systems/${id}`)

/** 获取业务健康摘要 */
export const fetchBusinessHealth = (id: number) =>
  request.get<{ code?: number; details?: BusinessHealth; message?: string }>(`/DC-Control/v1/business-systems/${id}/health`)

/** 获取业务资源成员 */
export const fetchBusinessMembers = (id: number) =>
  request.get<{ code?: number; details?: ListResult<BusinessResourceMember>; message?: string }>(`/DC-Control/v1/business-systems/${id}/resources`)

export interface BusinessResourceMemberPayload {
  resource_uid?: string
  resource_id?: number
  role?: string
  criticality?: string
  weight?: number
  description?: string
}

export const createBusinessMember = (id: number, data: BusinessResourceMemberPayload) =>
  request.post<{ code?: number; details?: BusinessResourceMember; message?: string }>(`/DC-Control/v1/business-systems/${id}/resources`, data)

export const deleteBusinessMember = (id: number, memberId: number) =>
  request.delete<{ code?: number; details?: { message: string }; message?: string }>(`/DC-Control/v1/business-systems/${id}/resources/${memberId}`)

/** 获取业务拓扑 */
export const fetchBusinessTopology = (id: number) =>
  request.get<{ code?: number; details?: BusinessTopology; message?: string }>(`/DC-Control/v1/business-systems/${id}/topology`)

/** 获取业务系统依赖 */
export const fetchBusinessDependencies = (id: number) =>
  request.get<{ code?: number; details?: ListResult<BusinessDependency>; message?: string }>(`/DC-Control/v1/business-systems/${id}/dependencies`)

/** 创建业务系统依赖 */
export const createBusinessDependency = (id: number, data: Partial<BusinessDependency>) =>
  request.post<{ code?: number; details?: BusinessDependency; message?: string }>(`/DC-Control/v1/business-systems/${id}/dependencies`, data)

/** 获取业务时间线 */
export const fetchBusinessTimeline = (id: number, params?: { limit?: number }) =>
  request.get<{ code?: number; details?: ListResult<BusinessTimelineItem>; message?: string }>(`/DC-Control/v1/business-systems/${id}/timeline`, {
    params,
  })

/** 获取业务笔记 */
export const fetchBusinessNotes = (id: number) =>
  request.get<{ code?: number; details?: ListResult<BusinessNote>; message?: string }>(`/DC-Control/v1/business-systems/${id}/notes`)

/** 创建业务笔记 */
export const createBusinessNote = (id: number, data: { title: string; content: string }) =>
  request.post<{ code?: number; details?: BusinessNote; message?: string }>(`/DC-Control/v1/business-systems/${id}/notes`, data)

/** 获取业务文档链接 */
export const fetchBusinessDocumentLinks = (id: number) =>
  request.get<{ code?: number; details?: ListResult<BusinessDocumentLink>; message?: string }>(`/DC-Control/v1/business-systems/${id}/documents`)

/** 新增业务文档链接 */
export const createBusinessDocumentLink = (id: number, data: Partial<BusinessDocumentLink>) =>
  request.post<{ code?: number; details?: BusinessDocumentLink; message?: string }>(`/DC-Control/v1/business-systems/${id}/documents`, data)
