import axios from 'axios'

const api = axios.create({
  baseURL: '/api',
  timeout: 30000,
})

// Long-running operations (sync, import, discovery) get a much longer timeout.
// Sync can take hours for million-row SAP tables (e.g. MARA: 1M rows × 236 columns).
export const longRunningApi = axios.create({
  baseURL: '/api',
  timeout: 0, // no timeout — sync can legitimately run for hours
})

// Let axios auto-detect Content-Type for FormData (multipart/form-data with boundary).
// Only set application/json for non-FormData requests.
const setJsonContentType = (config: any) => {
  if (!(config.data instanceof FormData)) {
    config.headers['Content-Type'] = 'application/json'
  }
  return config
}
api.interceptors.request.use(setJsonContentType)
longRunningApi.interceptors.request.use(setJsonContentType)

export default api
