'use client'

import { useState, useEffect, useMemo } from 'react'

interface Component { id: string; name: string; component_type: string }
interface Hazard { id: string; name: string; category: string; operational_states?: string[]; component_id?: string }
interface Mitigation { id: string; name?: string; title?: string; reduction_type: string; hazard_id?: string; linked_hazard_ids?: string[] }

export interface GraphNode {
  id: string
  type: 'component' | 'hazard' | 'mitigation'
  label: string
  subLabel?: string
  color: string
}

export interface GraphEdge {
  id: string
  source: string
  target: string
  label?: string
}

const NODE_COLORS: Record<string, string> = {
  component: '#6366F1',  // indigo
  hazard: '#EF4444',     // red
  mitigation: '#10B981', // green
}

export function useKnowledgeGraph(projectId: string) {
  const [components, setComponents] = useState<Component[]>([])
  const [hazards, setHazards] = useState<Hazard[]>([])
  const [mitigations, setMitigations] = useState<Mitigation[]>([])
  const [loading, setLoading] = useState(true)

  useEffect(() => {
    loadData()
  }, [projectId]) // eslint-disable-line react-hooks/exhaustive-deps

  async function loadData() {
    try {
      const [compRes, hazRes, mitRes] = await Promise.all([
        fetch(`/api/sdk/v1/iace/projects/${projectId}/components`),
        fetch(`/api/sdk/v1/iace/projects/${projectId}/hazards`),
        fetch(`/api/sdk/v1/iace/projects/${projectId}/mitigations`),
      ])

      if (compRes.ok) {
        const j = await compRes.json()
        setComponents((j.components || j || []).map((c: Record<string, unknown>) => ({
          id: c.id as string, name: c.name as string, component_type: c.component_type as string || '',
        })))
      }
      if (hazRes.ok) {
        const j = await hazRes.json()
        setHazards((j.hazards || j || []).map((h: Record<string, unknown>) => ({
          id: h.id as string, name: h.name as string, category: h.category as string || '',
          operational_states: (h.operational_states || []) as string[],
          component_id: (h.component_id || '') as string,
        })))
      }
      if (mitRes.ok) {
        const j = await mitRes.json()
        setMitigations((j.mitigations || j || []).map((m: Record<string, unknown>) => ({
          id: m.id as string, name: (m.name || m.title || '') as string,
          title: (m.title || m.name || '') as string,
          reduction_type: (m.reduction_type || '') as string,
          hazard_id: (m.hazard_id || '') as string,
          linked_hazard_ids: (m.linked_hazard_ids || []) as string[],
        })))
      }
    } catch (err) {
      console.error('Failed to load graph data:', err)
    } finally {
      setLoading(false)
    }
  }

  const { nodes, edges } = useMemo(() => {
    const graphNodes: GraphNode[] = []
    const graphEdges: GraphEdge[] = []

    // Component nodes
    components.forEach((c) => {
      graphNodes.push({
        id: `comp-${c.id}`, type: 'component',
        label: c.name, subLabel: c.component_type,
        color: NODE_COLORS.component,
      })
    })

    // Hazard nodes
    const compIdSet = new Set(components.map((c) => c.id))
    hazards.forEach((h) => {
      graphNodes.push({
        id: `haz-${h.id}`, type: 'hazard',
        label: h.name, subLabel: h.category,
        color: NODE_COLORS.hazard,
      })
      // Edge: the component that actually causes this hazard → hazard.
      // Only drawn when the hazard carries a component_id that maps to a known
      // component node (no synthetic "all from the first component" edges).
      if (h.component_id && compIdSet.has(h.component_id)) {
        graphEdges.push({
          id: `e-comp-haz-${h.id}`,
          source: `comp-${h.component_id}`,
          target: `haz-${h.id}`,
          label: 'erzeugt',
        })
      }
    })

    // Mitigation nodes
    mitigations.forEach((m) => {
      graphNodes.push({
        id: `mit-${m.id}`, type: 'mitigation',
        label: m.title || m.name || m.id,
        subLabel: m.reduction_type,
        color: NODE_COLORS.mitigation,
      })
      // Edge: mitigation → hazard
      const hazardIds = m.linked_hazard_ids?.length ? m.linked_hazard_ids : m.hazard_id ? [m.hazard_id] : []
      hazardIds.forEach((hid) => {
        graphEdges.push({
          id: `e-mit-haz-${m.id}-${hid}`,
          source: `mit-${m.id}`,
          target: `haz-${hid}`,
          label: 'schuetzt',
        })
      })
    })

    return { nodes: graphNodes, edges: graphEdges }
  }, [components, hazards, mitigations])

  return { nodes, edges, loading, stats: { components: components.length, hazards: hazards.length, mitigations: mitigations.length } }
}
