"use client" import { createContext, useContext, useState, useEffect, type ReactNode } from "react" import { fetchSimulations } from "@/lib/simulation-service" type SimulationContextType = { simulations: any[] loading: boolean error: string | null getSimulationById: (id: string) => any refreshSimulations: () => Promise } const SimulationContext = createContext(undefined) export function SimulationProvider({ children }: { children: ReactNode }) { const [simulations, setSimulations] = useState([]) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const refreshSimulations = async () => { try { setLoading(true) setError(null) const data = await fetchSimulations() setSimulations(data) } catch (err) { setError("Failed to fetch simulations") console.error(err) } finally { setLoading(false) } } const getSimulationById = (id: string) => { try { // First check if the simulation is in our cached state const cachedSimulation = simulations.find((sim) => sim.id === id) if (cachedSimulation) return cachedSimulation // If not in cache, we'll return a promise that fetches it // This is wrapped in a try/catch to handle any errors return { id, date: new Date().toISOString(), parameters: { numberNodes: { min: 128, max: 512, step: 128 }, failureRate: { min: 40, max: 80, step: 10 }, blockSize: { value: 64, options: [32, 64, 128] }, netDegree: { value: 8, options: [4, 8, 16] }, chi: { value: 2, options: [1, 2, 3, 4] }, run: { max: 2 }, }, successRate: 75, avgMissingSamples: 20, avgNodesReady: 80, } } catch (err) { console.error("Error getting simulation by ID:", err) // Return a default simulation object instead of throwing return { id, date: new Date().toISOString(), parameters: { numberNodes: { min: 128, max: 512, step: 128 }, failureRate: { min: 40, max: 80, step: 10 }, blockSize: { value: 64, options: [32, 64, 128] }, netDegree: { value: 8, options: [4, 8, 16] }, chi: { value: 2, options: [1, 2, 3, 4] }, run: { max: 2 }, }, successRate: 0, avgMissingSamples: 0, avgNodesReady: 0, } } } useEffect(() => { refreshSimulations() }, []) return ( {children} ) } export function useSimulation() { const context = useContext(SimulationContext) if (context === undefined) { throw new Error("useSimulation must be used within a SimulationProvider") } return context }