import { requestHttp } from "./http";
import { getAccessToken, setAccessToken, clearAccessToken } from "./auth";
import { redirectToLoginPage } from "./navigation";

let isRefreshing = false;
let refreshSubscribers = [];

function subscribeTokenRefresh(callback) {
  refreshSubscribers.push(callback);
}

function onTokenRefreshed(token) {
  refreshSubscribers.forEach((cb) => cb(token));
  refreshSubscribers = [];
}

function makeError(type, message, extra = {}) {
  return {
    type,
    message,
    ...extra,
  };
}

export async function apiRequest(method, path, body, options) {
  try {
    const { response, data, meta } = await requestHttp(method, path, body, options);

    if (!response.ok) {
      if (response.status === 401 && !options?._retryOnce) {
        if (!isRefreshing) {
          isRefreshing = true;
          try {
            const refreshResult = await requestHttp("POST", "/auth/refresh", null);
            if (refreshResult.response.ok) {
              const newToken = refreshResult.data?.data?.access_token;
              if (newToken) {
                setAccessToken(newToken);
                onTokenRefreshed(newToken);
              } else {
                throw new Error("No token in refresh response");
              }
            } else {
              throw new Error("Refresh failed");
            }
          } catch (refreshErr) {
            onTokenRefreshed(null);
            clearAccessToken();
            const isLoginPage = window.location.hash.includes("/login");
            if (!isLoginPage) {
              redirectToLoginPage();
            }
            return {
              ok: false,
              error: makeError("http_error", `HTTP 401`, {
                statusCode: 401,
                raw: data,
              }),
              meta,
            };
          } finally {
            isRefreshing = false;
          }
        } else {
          // Wait for refresh to complete
          await new Promise((resolve) => {
            subscribeTokenRefresh((token) => resolve(token));
          });
        }

        const currentToken = getAccessToken();
        if (currentToken) {
          return apiRequest(method, path, body, { ...options, _retryOnce: true });
        }

        return {
          ok: false,
          error: makeError("http_error", `HTTP 401`, {
            statusCode: 401,
            raw: data,
          }),
          meta,
        };
      }

      return {
        ok: false,
        error: makeError("http_error", `HTTP ${response.status}`, {
          statusCode: response.status,
          raw: data,
        }),
        meta,
      };
    }

    if (data && typeof data === "object" && (data.status === false || data.status === "error")) {
      return {
        ok: false,
        error: makeError("api_error", data.msg || data.message || "API error", {
          errorAlias: data.error_alias,
          failedFields: data.failed_fields || [],
          raw: data,
        }),
        meta,
      };
    }

    return {
      ok: true,
      data,
      meta,
    };
  } catch (error) {
    const isAbort = error?.name === "AbortError";
    return {
      ok: false,
      error: makeError(isAbort ? "timeout" : "network_error", error?.message || "Network error", {
        details: error,
      }),
      meta: {
        url: path,
        method,
        statusCode: 0,
        headers: null,
      },
    };
  }
}

export function apiGet(path, options) {
  return apiRequest("GET", path, null, options);
}

export function apiPost(path, body, options) {
  return apiRequest("POST", path, body, options);
}
