'use client';
import React, { createContext, use, useState, useContext } from 'react';
type User = {
    id: number;
    username: string;
};

interface AuthContextType {
    user: User | null;
    setUser: (user: User | null) => void;
}

const AuthContext = createContext<AuthContext | null>(null);

export const AuthContextProvider = ({ children }: React.PropsWithChildren) => {
    const [user, setUser] = useState<User | null>(null);

    return (
        <AuthContext.Provider value={{ user, setUser }}>
            {children}
        </AuthContext.Provider>
    );
};

export const useAuthContext = () => {
    const context = useContext(AuthContext);
    if (!context)
        throw new Error(
            'useAuthContext must be used within AuthContextProvider',
        );
    return context;
};

export const useUser = () => useAuthContext().user;
