// src/services/api/performanceInstance.ts
import axios from "axios";
import Cookies from "js-cookie";
import { toast } from "sonner";
import { getEnvUrl } from "@/shared/config/env";
import { getRefreshToken } from "../utils/refreshTokenHandler";

const performanceInstance = axios.create({
  baseURL: getEnvUrl("VITE_PERFORMANCE_API_URL"),
});

// Add auth token from localStorage (or context later)
performanceInstance.interceptors.request.use((config) => {
  const token = Cookies.get("auth-token");
  if (token) {
    config.headers["Authorization"] = `Bearer ${token}`;
  }
  return config;
});

let isRefreshing = false;
let failedQueue: any[] = [];

const processQueue = (error: any, token: string | null = null) => {
  failedQueue.forEach((prom) => {
    if (error) prom.reject(error);
    else prom.resolve(token);
  });
  failedQueue = [];
};

const skipRefreshUrls = ["/auth/login", "/auth/refresh", "/auth/me"]; // Add all URLs to skip

performanceInstance.interceptors.response.use(
  (response) => response,
  async (error) => {
    const originalRequest = error.config;

    // 🔒 Skip refresh logic for specific URLs
    const shouldSkip = skipRefreshUrls.some((url) =>
      originalRequest.url?.includes(url),
    );

    if (shouldSkip) {
      return Promise.reject(error);
    }
    const rememberMe = JSON.parse(
      localStorage.getItem("rememberMe") || "false",
    );

    if (error.response?.status === 401 && !originalRequest._retry) {
      if (isRefreshing) {
        return new Promise(function (resolve, reject) {
          failedQueue.push({ resolve, reject });
        })
          .then((token) => {
            originalRequest.headers["Authorization"] = "Bearer " + token;
            return performanceInstance(originalRequest);
          })
          .catch((err) => Promise.reject(err));
      }

      originalRequest._retry = true;
      isRefreshing = true;
      try {
        const refreshToken = Cookies.get("refresh-token") as string;

        const newToken = await getRefreshToken(refreshToken);
        const updatedToken = newToken.data.token;
        const updatedRefreshToken = newToken.data.refreshToken;
        processQueue(null, newToken.data.token);
        Cookies.set("auth-token", updatedToken);
        Cookies.set("refresh-token", updatedRefreshToken);
        originalRequest.headers["Authorization"] = `Bearer ${newToken}`;
        return performanceInstance(originalRequest);
      } catch (err) {
        processQueue(err, null);
        Cookies.remove("auth-token");
        Cookies.remove("refresh-token");
        Cookies.remove("auth-user");

        toast.error("Session expired", {
          description: "Please log in again.",
          duration: 4000,
        });

        setTimeout(() => {
          window.location.href = "/";
        }, 3000);

        return Promise.reject(err);
      } finally {
        isRefreshing = false;
      }
    }

    return Promise.reject(error);
  },
);

export default performanceInstance;
