/**
 * ComparisonProvider
 * Provides shared comparison state to all components
 */

'use client';

import { useState, useEffect, useCallback } from 'react';
import { getFromLocalStorage, setToLocalStorage } from '@/lib/utils';
import type { School } from '@/lib/types';
import { ComparisonContext } from './ComparisonContext';

const STORAGE_KEY = 'selectedSchools';
const MAX_SCHOOLS = 5;

export function ComparisonProvider({ children }: { children: React.ReactNode }) {
  const [selectedSchools, setSelectedSchools] = useState<School[]>([]);
  const [isInitialized, setIsInitialized] = useState(false);

  // Load from localStorage on mount
  useEffect(() => {
    const stored = getFromLocalStorage<School[]>(STORAGE_KEY, []);
    setSelectedSchools(stored);
    setIsInitialized(true);
  }, []);

  // Save to localStorage when schools change
  useEffect(() => {
    if (isInitialized) {
      setToLocalStorage(STORAGE_KEY, selectedSchools);
    }
  }, [selectedSchools, isInitialized]);

  // Listen for storage changes from other tabs
  useEffect(() => {
    const handleStorageChange = (e: StorageEvent) => {
      if (e.key === STORAGE_KEY && e.newValue) {
        try {
          const parsed = JSON.parse(e.newValue);
          setSelectedSchools(parsed);
        } catch {
          // Ignore parse errors
        }
      }
    };

    window.addEventListener('storage', handleStorageChange);
    return () => window.removeEventListener('storage', handleStorageChange);
  }, []);

  const addSchool = useCallback((school: School) => {
    setSelectedSchools((prev) => {
      if (prev.some((s) => s.urn === school.urn)) {
        return prev;
      }
      if (prev.length >= MAX_SCHOOLS) {
        alert(`Maximum ${MAX_SCHOOLS} schools can be compared`);
        return prev;
      }
      return [...prev, school];
    });
  }, []);

  const removeSchool = useCallback((urn: number) => {
    setSelectedSchools((prev) => prev.filter((s) => s.urn !== urn));
  }, []);

  const clearAll = useCallback(() => {
    setSelectedSchools([]);
  }, []);

  const isSelected = useCallback(
    (urn: number) => selectedSchools.some((s) => s.urn === urn),
    [selectedSchools]
  );

  // Placeholder mutate - actual SWR mutate is in useComparison hook
  const mutate = useCallback(() => {}, []);

  return (
    <ComparisonContext.Provider
      value={{
        selectedSchools,
        comparisonData: null,
        isLoading: false,
        error: null,
        addSchool,
        removeSchool,
        clearAll,
        isSelected,
        canAddMore: selectedSchools.length < MAX_SCHOOLS,
        isInitialized,
        mutate,
      }}
    >
      {children}
    </ComparisonContext.Provider>
  );
}
