import axios from 'axios'

const BASE_URL = import.meta.env.VITE_CLOUD_URL || 'http://localhost:8001'

const client = axios.create({ baseURL: BASE_URL })

client.interceptors.request.use(config => {
  const token = localStorage.getItem('sysadmin_token')
  if (token) config.headers.Authorization = `Bearer ${token}`
  return config
})

client.interceptors.response.use(
  res => res,
  err => {
    if (err.response?.status === 401) {
      localStorage.removeItem('sysadmin_token')
      window.location.href = '/login'
    }
    return Promise.reject(err)
  }
)

export default client
