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

export interface Session {
  id: string;
  display_name: string;
  tool_type_name: string;
  tool_icon: string;
  tool_type_interfaces: string[];
  repository_name: string;
  repository_id: string;
  project_name: string;
  project_id: string;
  status: string;
  url: string | null;
}

interface SessionsContextType {
  sessions: Session[];
  setAllSessions: (sessions: Session[]) => void;
}

const SessionsContext = createContext<SessionsContextType | undefined>(undefined);

export const SessionsProvider = ({ children }: { children: ReactNode }) => {
  const [sessions, setSessions] = useState<Session[]>([]);

  const setAllSessions = useCallback((newSessions: Session[]) => {
    setSessions(newSessions);
  }, []);

  return (
    <SessionsContext.Provider value={{ sessions, setAllSessions }}>
      {children}
    </SessionsContext.Provider>
  );
};

export const useSessions = () => {
  const context = useContext(SessionsContext);
  if (context === undefined) {
    throw new Error("useSessions must be used within a SessionsProvider");
  }
  return context;
};
