import axios from "axios";
import { getEnvUrl } from "@/shared/config/env";

const api = axios.create({
  baseURL: getEnvUrl("VITE_API_BASE_URL", false),
  headers: {
    "Content-Type": "application/json",
  },
});

// Single response interceptor: pass success through, redirect on 401, reject everything else.
api.interceptors.response.use(
  (response) => response,
  (error) => {
    if (error.response?.status === 401) {
      localStorage.removeItem("token");
      window.location.href = "/";
    }
    return Promise.reject(error);
  },
);

export { api };
