import { z } from 'zod'
import { LinkSchema } from '../../organization-model/graph/link'
import type { OrganizationGraph } from '../../organization-model/graph/types'
import type { Link } from '../../organization-model/graph/link'

export const ResourceCategorySchema = z.enum(['production', 'diagnostic', 'internal', 'testing'])

export type ResourceCategory = z.infer<typeof ResourceCategorySchema>
export type ResourceLink = Link

export const ResourceLinkSchema = LinkSchema

export function kindPrefix(kind: string, sourceId: string): string {
  return `${kind}:${sourceId}`
}

export function validateNodeId(nodeId: string, graph: OrganizationGraph): void {
  if (graph.nodes.some((node) => node.id === nodeId)) {
    return
  }

  const known = graph.nodes
    .map((node) => node.id)
    .filter((id) => id.includes(nodeId.split(':')[0] ?? ''))
    .slice(0, 5)

  throw new Error(
    known.length > 0
      ? `Unknown organization graph node "${nodeId}". Nearby known nodes: ${known.join(', ')}`
      : `Unknown organization graph node "${nodeId}".`
  )
}
