import type { Session, SupabaseClient, User } from "@supabase/supabase-js";
import { createContext, useContext, useEffect, useState } from "react";

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

export const useSession = () => {
  const context = useContext(SessionContext);
  if (!context) {
    throw new Error("useSession must be used within a SessionProvider");
  }
  return context;
};

type Props = { supabase: SupabaseClient; children: React.ReactNode };

export const SessionProvider = ({ supabase, children }: Props) => {
  const [session, setSession] = useState<Session | null>(null);

  useEffect(() => {
    const authStateListener = supabase.auth.onAuthStateChange(async (_, session) => {
      setSession(session);
    });

    return () => {
      authStateListener.data.subscription.unsubscribe();
    };
  }, [supabase]);

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

type TestProps = { children: React.ReactNode; testUser?: User };

export const SessionTestProvider = ({ children, testUser }: TestProps) => {
  const session = testUser
    ? {
        user: testUser,
        access_token: "test_access_token",
        refresh_token: "test_refresh_token",
        expires_in: 3600,
        token_type: "bearer" as const,
      }
    : null;
  return <SessionContext.Provider value={{ session }}>{children}</SessionContext.Provider>;
};
