import axios from 'axios';
import { log } from '../../utils/logger';

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

// Request interceptor
api.interceptors.request.use(
  (config) => {
    log.info('guacamole-api', `Request: ${config.method?.toUpperCase()} ${config.url}`, {
      headers: config.headers,
      params: config.params,
      data: config.data,
    });
    return config;
  },
  (error) => {
    log.error('guacamole-api', 'Request error:', { error: error.message });
    return Promise.reject(error);
  }
);

// Response interceptor
api.interceptors.response.use(
  (response) => {
    log.info('guacamole-api', `Response: ${response.status} ${response.config.url}`, {
      data: response.data,
    });
    return response;
  },
  (error) => {
    log.error('guacamole-api', 'Response error:', {
      status: error.response?.status,
      data: error.response?.data,
      message: error.message,
    });
    return Promise.reject(error);
  }
);

export default api; 