'use client' import { useState, useEffect, useCallback } from 'react' import { GCIResult, GCIBreakdown, GCIHistoryResponse, GCIMatrixResponse, NIS2Score, ISOGapAnalysis, WeightProfile, } from '@/lib/sdk/gci/types' import { getGCIScore, getGCIBreakdown, getGCIHistory, getGCIMatrix, getNIS2Score, getISOGapAnalysis, getWeightProfiles, } from '@/lib/sdk/gci/api' import { TabId } from '../_components/GCIHelpers' export function useGCI() { const [activeTab, setActiveTab] = useState('overview') const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const [gci, setGCI] = useState(null) const [breakdown, setBreakdown] = useState(null) const [history, setHistory] = useState(null) const [matrix, setMatrix] = useState(null) const [nis2, setNIS2] = useState(null) const [iso, setISO] = useState(null) const [profiles, setProfiles] = useState([]) const [selectedProfile, setSelectedProfile] = useState('default') const loadData = useCallback(async (profile?: string) => { setLoading(true) setError(null) try { const [gciRes, historyRes, profilesRes] = await Promise.all([ getGCIScore(profile), getGCIHistory(), getWeightProfiles(), ]) setGCI(gciRes) setHistory(historyRes) setProfiles(profilesRes.profiles || []) } catch (err: any) { setError(err.message || 'Fehler beim Laden der GCI-Daten') } finally { setLoading(false) } }, []) useEffect(() => { loadData(selectedProfile) }, [selectedProfile, loadData]) useEffect(() => { if (activeTab === 'breakdown' && !breakdown && gci) { getGCIBreakdown(selectedProfile).then(setBreakdown).catch(() => {}) } if (activeTab === 'nis2' && !nis2) { getNIS2Score().then(setNIS2).catch(() => {}) } if (activeTab === 'iso' && !iso) { getISOGapAnalysis().then(setISO).catch(() => {}) } if (activeTab === 'matrix' && !matrix) { getGCIMatrix().then(setMatrix).catch(() => {}) } }, [activeTab, breakdown, nis2, iso, matrix, gci, selectedProfile]) const handleProfileChange = (profile: string) => { setSelectedProfile(profile) setBreakdown(null) } return { activeTab, setActiveTab, loading, error, gci, breakdown, history, matrix, nis2, iso, profiles, selectedProfile, loadData, handleProfileChange, } }