import { Session } from '@supabase/supabase-js';
import React, { createContext, useContext, useState } from 'react';
import { supabase } from '../supabaseClient';

export const SessionContext = createContext<{
  session: Session | null;
} | null>(null);

export const SessionProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
  const [session] = useState(() => supabase.auth.session());

  // const value = useMemo(() => ({ session, setSession }), [session, setSession]);
  const value = { session };

  return <SessionContext.Provider value={value}>{children}</SessionContext.Provider>;
};

export const useSession = () => {
  const context = useContext(SessionContext);

  if (!context) {
    throw new Error('useSession must be used within a SessionProvider');
  }

  return context;
};
