import axios from 'axios'
import { ENV } from '../config/env'

/* ── GUARDiA ITSM 클라이언트 ── */
export const guardiaApi = axios.create({ baseURL: ENV.GUARDIA_API })

guardiaApi.interceptors.request.use(cfg => {
  const token = sessionStorage.getItem('gm_token')
  if (token) cfg.headers.Authorization = `Bearer ${token}`
  return cfg
})

guardiaApi.interceptors.response.use(
  r => r,
  err => {
    if (err.response?.status === 401) {
      sessionStorage.removeItem('gm_token')
      sessionStorage.removeItem('gm_user')
      window.location.href = '/login'
    }
    return Promise.reject(err)
  }
)

/* ── Manager Backend 클라이언트 ── */
export const managerApi = axios.create({ baseURL: ENV.MANAGER_API })

managerApi.interceptors.request.use(cfg => {
  const token = sessionStorage.getItem('gm_token')
  if (token) cfg.headers.Authorization = `Bearer ${token}`
  return cfg
})

/* ── Gitea API 클라이언트 ── */
export const giteaApi = axios.create({
  baseURL: ENV.GITEA_API,
  auth: { username: ENV.GITEA_USER, password: ENV.GITEA_PASS },
})
