import { createContext, useContext, useEffect, useState, useCallback } from 'react' type Theme = 'dark' | 'light' | 'system' interface ThemeContextValue { theme: Theme resolvedTheme: 'dark' | 'light' setTheme: (theme: Theme) => void cycleTheme: () => void } const ThemeContext = createContext(undefined) const STORAGE_KEY = 'portfolio-theme' function getSystemTheme(): 'dark' | 'light' { return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light' } function getStoredTheme(): Theme { try { const stored = localStorage.getItem(STORAGE_KEY) if (stored === 'dark' || stored === 'light' || stored === 'system') { return stored } } catch { // localStorage unavailable } return 'system' } function resolveTheme(theme: Theme): 'dark' | 'light' { return theme === 'system' ? getSystemTheme() : theme } export function ThemeProvider({ children }: { children: React.ReactNode }) { const [theme, setThemeState] = useState(getStoredTheme) const resolvedTheme = resolveTheme(theme) useEffect(() => { document.documentElement.setAttribute('data-theme', resolvedTheme) try { localStorage.setItem(STORAGE_KEY, theme) } catch { // localStorage unavailable } }, [resolvedTheme, theme]) const setTheme = useCallback((newTheme: Theme) => { setThemeState(newTheme) }, []) const cycleTheme = useCallback(() => { setThemeState((prev) => { if (prev === 'system') { return getSystemTheme() === 'dark' ? 'light' : 'dark' } if (prev === 'dark') return 'light' if (prev === 'light') return 'system' return 'dark' }) }, []) return ( {children} ) } export function useTheme() { const context = useContext(ThemeContext) if (!context) { throw new Error('useTheme must be used within ThemeProvider') } return context }