"use client";

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

interface UserInfo {
  firstName: string;
  lastName: string;
  teamName: string;
  teamId?: string;
  uuid?: string;
}

interface UserContextType {
  userInfo: UserInfo | null;
  setUserInfo: (userInfo: UserInfo | null) => void;
  loading: boolean;
}

const UserContext = createContext<UserContextType | undefined>(undefined);

interface UserProviderProps {
  children: ReactNode;
  initialUserInfo: UserInfo | null;
}

export function UserProvider({ children, initialUserInfo }: UserProviderProps) {
  const [userInfo, setUserInfo] = useState<UserInfo | null>(initialUserInfo);
  const [loading, setLoading] = useState(false); // Plus de loading initial

  return (
    <UserContext.Provider value={{ userInfo, setUserInfo, loading }}>
      {children}
    </UserContext.Provider>
  );
}

export function useUser() {
  const context = useContext(UserContext);
  if (context === undefined) {
    throw new Error("useUser must be used within a UserProvider");
  }
  return context;
}
