/* eslint-disable @typescript-eslint/no-empty-object-type */
/* eslint-disable @typescript-eslint/no-explicit-any */
"use client";

import { useState, useCallback } from "react";
import axios, {
  AxiosRequestConfig,
  AxiosError,
  InternalAxiosRequestConfig,
} from "axios";
import toast from "react-hot-toast";
import { getApiBaseUrl } from "@/components/main/BaseUrl";
import Cookies from "js-cookie";
import { clearAuthSession } from "@/lib/auth/session";

const axiosInstance = axios.create({
  timeout: 900000,
  maxContentLength: Infinity,
  maxBodyLength: Infinity,
});

const getToken = (): string => {
  if (typeof window !== "undefined") {
    const token = Cookies.get("token");
    return token ? `Bearer ${token}` : "";
  }
  return "";
};

const isPublicAuthRequest = (url: string): boolean => {
  const path = url.split("?")[0];
  const publicPatterns = [
    /^\/login\/?$/,
    /^\/login\/verify$/,
    /^\/login\/login_username$/,
    /^\/login\/forgot_password$/,
    /^\/register\/?$/,
    /^\/register\/verify$/,
    /^\/auth\/google$/,
  ];
  return publicPatterns.some((pattern) => pattern.test(path));
};

// اضافه کردن توکن به درخواست‌های محافظت‌شده (نه لاگین/ثبت‌نام)
axiosInstance.interceptors.request.use(
  (config: InternalAxiosRequestConfig) => {
    const requestUrl = String(config.url ?? "");

    if (config.headers) {
      if (isPublicAuthRequest(requestUrl)) {
        delete config.headers.Authorization;
      } else {
        const token = getToken();
        if (token) {
          config.headers.Authorization = token;
        } else {
          delete config.headers.Authorization;
        }
      }
    }
    return config;
  },
  (error) => Promise.reject(error)
);

// بررسی noToast در headers و جلوگیری از نمایش toast در پاسخ‌ها
axiosInstance.interceptors.response.use(
  (response) => {
    if (!response.config.headers?.["X-No-Toast"] && response.data?.message) {
      // toast.success(response.data.message);
    }
    return response;
  },
  (error: AxiosError) => {
    const { response, config } = error;

    if (response) {
      const { status, data } = response;
      const noToast = config?.headers?.["X-No-Toast"];
      const requestUrl = String(config?.url ?? "");
      const isAuthRequest =
        requestUrl.includes("/login") || requestUrl.includes("/register");

      if (status === 401 && !noToast && !isAuthRequest) {
        handleUnauthorized();
      } else if (status === 403 && (data as { type?: string })?.type === "block") {
        if (!noToast && (data as { message?: string })?.message) {
          toast.error((data as { message: string }).message);
        }
      } else if (status === 422) {
        const errors = (data as { [key: string]: string[] }).errors || {};
        if (!noToast) {
          if (
            typeof data === "object" &&
            (data as { message?: string }).message
          ) {
            toast.error((data as { message: string }).message);
          } else {
            Object.values(errors).forEach((messages) => {
              (messages as unknown as string[]).forEach((message) =>
                toast.error(message)
              );
            });
          }
        }
      } else if (status === 404 && !noToast) {
        if (
          typeof data === "object" &&
          (data as { message?: string }).message
        ) {
          toast.error((data as { message: string }).message);
        }
      } else if ([429, 500, 503].includes(status)) {
        if (!noToast) {
          // toast.error("An unexpected error occurred. Please try again later.");
        }
      } else {
        if (!noToast) {
          // toast.error((data as any)?.message || "An error occurred.");
        }
      }
    }

    return Promise.reject(error);
  }
);

const handleUnauthorized = (): void => {
  if (typeof window === "undefined") return;

  // مهمان (بدون توکن) نباید به صفحه ورود هدایت شود؛ فقط آزادانه صفحات را می‌بیند
  // و درخواست‌های محافظت‌شده بی‌سروصدا رد می‌شوند. عملیات‌ها خودشان کاربر را به ورود می‌برند.
  const hasToken = Boolean(Cookies.get("token"));
  if (!hasToken) return;

  const path = window.location.pathname;
  if (
    path.startsWith("/login") ||
    path.startsWith("/register") ||
    path.startsWith("/verify-otp") ||
    path.startsWith("/forget-password") ||
    path.startsWith("/verify") ||
    path.startsWith("/auth")
  ) {
    return;
  }

  clearAuthSession().finally(() => {
    window.location.href = "/login";
  });
};

// تایپ اصلاح‌شده برای درخواست‌ها
interface RequestConfig extends AxiosRequestConfig {
  noToast?: boolean;
}

const useAxios = () => {
  const [loading, setLoading] = useState<boolean>(false);
  const [error, setError] = useState<AxiosError | null>(null);

  const request = useCallback(
    async <T,>(
      method: AxiosRequestConfig["method"],
      url: string,
      data: unknown = null,
      config: RequestConfig = {}
    ): Promise<T> => {
      setLoading(true);
      setError(null);

      try {
        const isFormData = data instanceof FormData;

        const response = await axiosInstance({
          method,
          url,
          data,
          baseURL: getApiBaseUrl(),
          ...config,
          headers: {
          
         
            ...config.headers,
            ...(isFormData ? {} : { "Content-Type": "application/json" }), // حذف Content-Type برای FormData
            ...(config.noToast ? { "X-No-Toast": "true" } : {}), // ارسال noToast به عنوان یک هدر
          },
        });

        return response.data;
      } catch (err) {
        setError(err as AxiosError);
        throw err;
      } finally {
        setLoading(false);
      }
    },
    []
  );

  return {
    request,
    loading,
    error,
  };
};

export { axiosInstance as apiClient };
export default useAxios;
