'use client'

import { useState, useCallback } from 'react'

export interface GTRisk { f: number; w: number; p: number; s: number; r: number }
export interface GTPLr { s: string; f: string; p: string; ew?: string; plr: string }

export interface GroundTruthEntry {
  nr: string
  hazard_group: string
  hazard_group_applicable: boolean
  hazard_subgroup: string
  hazard_type: string
  hazard_cause: string
  lifecycle_phases: string[]
  component_zone: string
  risk_in: GTRisk
  plr?: GTPLr | null
  measures: string[]
  measure_type: string
  risk_out: GTRisk
  norm_references: string[]
  sufficient: boolean
  comment?: string
  reduction_steps?: {
    risk_in: GTRisk; measures: string[]; measure_type: string
    risk_out: GTRisk; norm_references: string[]; sufficient: boolean
  }[]
}

export interface HazardSummary {
  id: string; name: string; category: string
  component?: string; zone?: string; risk_level?: string
  description?: string; scenario?: string
  possible_harm?: string; trigger_event?: string
  affected_person?: string; lifecycle_phase?: string
  mitigations?: string[]
}

export interface HazardMatchPair {
  gt_entry: GroundTruthEntry
  engine_hazard: HazardSummary
  match_score: number
  match_reason: string
}

export interface CategoryScore {
  category: string; gt_count: number; match_count: number; coverage: number
}

export interface RiskComparisonPair {
  hazard_name: string
  gt_severity: number; gt_frequency: number; gt_probability: number; gt_avoidance: number; gt_risk: number
  eng_severity: number; eng_frequency: number; eng_probability: number; eng_avoidance: number
  fk_score: number; fk_band: string
  eng_risk_point: number; eng_risk_low: number; eng_risk_high: number
  eng_risk_level: string; eng_risk_level_range: string
  confidence: string // hoch | mittel | niedrig
}

export interface RiskAgreement {
  n: number
  severity_within1: number; frequency_within1: number
  probability_within1: number; avoidance_within1: number
  rank_concordance: number
  high_confidence_pct: number
}

export interface DistanceToken {
  value: number
  unit: string // "mm" | "mm/s"
  raw: string
}

export interface DistanceComparison {
  gt_count: number
  matched_count: number
  agreement_pct: number
  matched: DistanceToken[]
  gt_only: DistanceToken[]
  engine_only: DistanceToken[]
}

export interface BenchmarkResult {
  coverage_score: number
  measure_coverage: number
  total_gt: number
  total_engine: number
  matched_pairs: HazardMatchPair[]
  missing_from_engine: GroundTruthEntry[]
  extra_in_engine: HazardSummary[]
  category_breakdown: CategoryScore[]
  risk_rank_pairs: { gt_rank: number; engine_rank: number; hazard_name: string; gt_risk_score: number }[]
  risk_comparison?: RiskComparisonPair[]
  risk_agreement?: RiskAgreement
  distances?: DistanceComparison
}

interface UseBenchmarkReturn {
  result: BenchmarkResult | null
  gtLoaded: boolean
  gtEntryCount: number
  loading: boolean
  error: string | null
  importGT: (gt: { entries: GroundTruthEntry[]; source_file?: string; description?: string }) => Promise<void>
  runBenchmark: (gtProjectId?: string) => Promise<void>
}

export function useBenchmark(projectId: string): UseBenchmarkReturn {
  const [result, setResult] = useState<BenchmarkResult | null>(null)
  const [gtLoaded, setGtLoaded] = useState(false)
  const [gtEntryCount, setGtEntryCount] = useState(0)
  const [loading, setLoading] = useState(false)
  const [error, setError] = useState<string | null>(null)

  const importGT = useCallback(async (gt: { entries: GroundTruthEntry[]; source_file?: string; description?: string }) => {
    setLoading(true)
    setError(null)
    try {
      const res = await fetch(`/api/sdk/v1/iace/projects/${projectId}/benchmark/import-gt`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(gt),
      })
      if (!res.ok) throw new Error(await res.text())
      const data = await res.json()
      setGtLoaded(true)
      setGtEntryCount(data.entry_count || gt.entries.length)
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Import failed')
    } finally {
      setLoading(false)
    }
  }, [projectId])

  const runBenchmark = useCallback(async (gtProjectId?: string) => {
    setLoading(true)
    setError(null)
    try {
      const params = gtProjectId ? `?gt_project_id=${gtProjectId}` : ''
      const res = await fetch(`/api/sdk/v1/iace/projects/${projectId}/benchmark${params}`)
      if (!res.ok) throw new Error(await res.text())
      const data: BenchmarkResult = await res.json()
      setResult(data)
      setGtLoaded(true)
      setGtEntryCount(data.total_gt)
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Benchmark failed')
    } finally {
      setLoading(false)
    }
  }, [projectId])

  return { result, gtLoaded, gtEntryCount, loading, error, importGT, runBenchmark }
}
