import axios from 'axios';

// Create a base axios instance
export const apiClient = axios.create({
    baseURL: import.meta.env.VITE_API_BASE_URL || '/v1',
    timeout: 10000,
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
});

// Request interceptor
apiClient.interceptors.request.use(
    (config) => {
        // You can add auth tokens here if needed
        const token = localStorage.getItem('auth_token');
        if (token) {
            config.headers.Authorization = `Bearer ${token}`;
        }
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);

// Response interceptor
apiClient.interceptors.response.use(
    (response) => {
        return response;
    },
    (error) => {
        // Handle common errors (e.g., unauthorized, server errors)
        if (error.response) {
            // Handle specific status codes
            if (error.response.status === 401) {
                // Handle unauthorized
                console.error('Unauthorized request');
                // You might want to redirect to login page
            }
        }
        return Promise.reject(error);
    }
);
