"use client";

import React, { createContext, useContext, useState, ReactNode, Dispatch, SetStateAction } from "react";
import { PredictionResult } from "./types";

interface HealthRiskContextType {
  predictionResults: PredictionResult[];
  setPredictionResults: Dispatch<SetStateAction<PredictionResult[]>>;
  currentYear: number;
  setCurrentYear: Dispatch<SetStateAction<number>>;
  analysisQueryTime: string | null;
  setAnalysisQueryTime: Dispatch<SetStateAction<string | null>>;
}

const HealthRiskContext = createContext<HealthRiskContextType | undefined>(undefined);

export const HealthRiskProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
  const [predictionResults, setPredictionResults] = useState<PredictionResult[]>([]);
  const [currentYear, setCurrentYear] = useState<number>(4);
  const [analysisQueryTime, setAnalysisQueryTime] = useState<string | null>(null);

  return (
    <HealthRiskContext.Provider
      value={{
        predictionResults,
        setPredictionResults,
        currentYear,
        setCurrentYear,
        analysisQueryTime,
        setAnalysisQueryTime,
      }}
    >
      {children}
    </HealthRiskContext.Provider>
  );
};

export const useHealthRisk = () => {
  const context = useContext(HealthRiskContext);
  if (context === undefined) {
    throw new Error("useHealthRisk must be used within a HealthRiskProvider");
  }
  return context;
};
