import { createContext, useContext, useState, type ReactNode } from "react";

interface SymbolContextValue {
  symbol: string;
  setSymbol: (s: string) => void;
}

const SymbolContext = createContext<SymbolContextValue | undefined>(undefined);

const DEFAULT_SYMBOL = "SPY";

export function SymbolProvider({ children }: { children: ReactNode }) {
  // React state only — per sandbox constraints we never persist to localStorage.
  const [symbol, setSymbol] = useState<string>(DEFAULT_SYMBOL);
  return (
    <SymbolContext.Provider value={{ symbol, setSymbol }}>{children}</SymbolContext.Provider>
  );
}

export function useSymbol(): SymbolContextValue {
  const ctx = useContext(SymbolContext);
  if (!ctx) throw new Error("useSymbol must be used inside SymbolProvider");
  return ctx;
}
