import axios from 'axios'


const api = axios.create({
  baseURL: import.meta.env.VITE_API_URL || '/api',
  timeout: 60000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// Request interceptor handling can be added here if needed
api.interceptors.request.use(
  config => {
    const user = JSON.parse(localStorage.getItem('user'));
    if (user && user.access_token) {
       config.headers.Authorization = `Bearer ${user.access_token}`;
    } else if (user && user.token) {
       config.headers.Authorization = `${user.tokenType} ${user.token}`;
    }
    
    const projectId = localStorage.getItem('active_project_id');
    if (projectId) {
      config.headers['X-Project-ID'] = projectId;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

api.interceptors.response.use(
    response => response,
    error => {
        return Promise.reject(error)
    }
)

export default api
