import axios from "axios";
import { shouldStartLoginRedirect } from "../../../common/core/auth";
import {
  shouldSuppressDevelopmentSessionRedirect,
} from "../../../common/core/session";
import { userManager } from "./auth";

let isRedirectingToLogin = false;

const apiClient = axios.create({
  baseURL: (window as Window & typeof globalThis & { _IS_TEST_MODE?: boolean })
    ._IS_TEST_MODE
    ? "http://playwright-mock/api"
    : (import.meta.env.VITE_ADMIN_API_BASE ?? "/api"),
});

apiClient.interceptors.request.use(async (config) => {
  // IdP 중립 Auth 레이어 연동: oidc-client의 userManager에서 최신 토큰을 가져옵니다.
  const user = await userManager.getUser();
  const sessionToken =
    user?.access_token || window.localStorage.getItem("admin_session");

  if (sessionToken) {
    config.headers.Authorization = `Bearer ${sessionToken}`;
  }

  // 테넌트 선택 값을 보관하고 헤더로 전달한다.
  const tenantId = window.localStorage.getItem("admin_tenant");
  if (tenantId) {
    config.headers["X-Tenant-ID"] = tenantId;
  }

  // [Development Only] Inject Mock Role from RoleSwitcher
  const isMockRoleEnabled =
    window.localStorage.getItem("X-Mock-Role-Enabled") === "true";
  const mockRole = window.localStorage.getItem("X-Mock-Role");
  if (isMockRoleEnabled && mockRole) {
    config.headers["X-Test-Role"] = mockRole;
  }

  return config;
});

apiClient.interceptors.response.use(
  (response) => response,
  async (error) => {
    if (error.response?.status === 401) {
      if (
        shouldSuppressDevelopmentSessionRedirect({
          appMode: import.meta.env.MODE,
        })
      ) {
        console.warn(
          "[apiClient] 401 Unauthorized detected, but development session redirects are disabled.",
        );
        return Promise.reject(error);
      }

      console.warn(
        "[apiClient] 401 Unauthorized detected. Clearing session state.",
      );

      // 로컬 스토리지의 세션 키 제거
      window.localStorage.removeItem("admin_session");

      // oidc-client의 유저 상태도 제거하여 isAuthenticated를 false로 만듭니다.
      // 이를 통해 LoginPage에서의 무한 리다이렉션 루프를 방지합니다.
      await userManager.removeUser();

      if (
        shouldStartLoginRedirect({
          pathname: window.location.pathname,
          isRedirecting: isRedirectingToLogin,
        })
      ) {
        isRedirectingToLogin = true;
        console.info(
          "[apiClient] Redirecting to /login from",
          window.location.pathname,
        );
        window.location.href = "/login";
      }
    }
    return Promise.reject(error);
  },
);

export default apiClient;
