import axios from 'axios';
import { getApiBaseUrl, getAppUrl, getRouterBasename } from '../utils/url';

const baseURL = getApiBaseUrl();

const axiosInstance = axios.create({
  baseURL,
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  }
});

let isRedirecting = false;

// Interceptor for attaching auth token
axiosInstance.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem('admin_token');
    const isAuthOrAdminRoute = config.url?.includes('/admin') || config.url?.includes('/auth');
    if (token && isAuthOrAdminRoute) {
      config.headers['Authorization'] = `Bearer ${token}`;
    }
    return config;
  },
  (error) => Promise.reject(error)
);

// Interceptor to handle 401 Unauthorized safely
axiosInstance.interceptors.response.use(
  (response) => response,
  (error) => {
    if (error.response && error.response.status === 401) {
      localStorage.removeItem('admin_token');
      localStorage.removeItem('admin_nama');

      const basename = getRouterBasename();
      const relativePathname = basename !== '/' && window.location.pathname.startsWith(basename)
        ? window.location.pathname.substring(basename.length)
        : window.location.pathname;

      if (!isRedirecting && relativePathname.startsWith('/admin') && relativePathname !== '/login') {
        isRedirecting = true;
        window.location.href = getAppUrl('/login');
      }
    }
    return Promise.reject(error);
  }
);

export default axiosInstance;
