import axios from 'axios';

const axiosInstance = axios.create({
  // baseURL: process.env.NEXT_PUBLIC_API_BASE_URL || '/api', // Default to /api if not set for Next.js proxy
  // Since your Spring Boot backend is at /api, and Next.js dev server can proxy, 
  // for client-side requests, you might not need a full baseURL if using relative paths correctly,
  // or ensure your Next.js proxy is set up for /api paths.
  // If Spring Boot is running on a different port during dev (e.g. 7529) and not proxied,
  // you'd need something like: baseURL: 'http://localhost:7529/api'
  // However, the backend controller is already mapped to /api/admin/mqtt-log, so calling /api/admin/... should work if proxied.
  headers: {
    'Content-Type': 'application/json',
  },
});

// Optional: Add interceptors for request/response, e.g., for auth tokens
axiosInstance.interceptors.request.use(
  (config) => {
    // Example: Add auth token if available
    // if (typeof window !== 'undefined') {
    //   const token = localStorage.getItem('token'); 
    //   if (token) {
    //     config.headers.Authorization = `Bearer ${token}`;
    //   }
    // }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

axiosInstance.interceptors.response.use(
  (response) => {
    // Any status code that lie within the range of 2xx cause this function to trigger
    return response;
  },
  (error) => {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // You can add global error handling here, e.g., for 401 Unauthorized redirect to login
    // if (error.response && error.response.status === 401) {
    //   // redirect to login page
    // }
    return Promise.reject(error);
  }
);

export default axiosInstance; 