// Helper function for API calls
export const authFetch = async (url, options = {}) => {
    const response = await fetch(`${url}`, {
        ...options,
        headers: {
            'Content-Type': 'application/json',
            ...options.headers,
        },
        credentials: 'include', // For http-only cookies
    });

    if (response.status === 401) {
        // Handle unauthorized (redirect to login)
        window.location.href = '/login';
        throw new Error('Please login again');
    }

    if (!response.ok) {
        const error = await response.json()
        throw new Error(error.message || 'Authentication failed')
    }

    const data = await response.json();

    return data;
}