import type { AxiosResponse } from 'axios';
import { BACKEND_ERROR_CODE, createFlatRequest, createRequest } from '@sa/axios';
import { useAuthStore } from '@/store/modules/auth';
import { localStg } from '@/utils/storage';
import { getServiceBaseURL } from '@/utils/service';
import { $t } from '@/locales';
import { applyApiEncrypt } from './api-encrypt';
import { parseServiceCodes, shouldDeferBackendFailToCaller, shouldSuppressErrorMessage } from './error-message';
import { getAuthorization, handleExpiredRequest, notifySessionExpired, showErrorMsg } from './shared';
import { withDedupe } from './dedupe';
import type { RequestInstanceState } from './type';

const isHttpProxy = import.meta.env.DEV && import.meta.env.VITE_HTTP_PROXY === 'Y';
const { baseURL, otherBaseURL } = getServiceBaseURL(import.meta.env, isHttpProxy);
const REQUEST_TIMEOUT = 15 * 1000;

export const request = withDedupe(
  createFlatRequest(
    {
      baseURL,
      timeout: REQUEST_TIMEOUT,
      headers: {
        apifoxToken: 'XL299LiMEDZ0H5h3A29PxwQXdMJqWyY2'
      }
    },
    {
      defaultState: {
        errMsgStack: [],
        refreshTokenPromise: null
      } as RequestInstanceState,
      transform(response: AxiosResponse<App.Service.Response<any>>) {
        return response.data.data;
      },
      async onRequest(config) {
        // skipAuth 为 true 的请求不注入 Authorization——避免给公开接口（如 refresh-token）
        // 带上过期 access 头被网关拦截（网关只看 Authorization，不区分路由是否 PermitAll）
        if (!config.skipAuth) {
          const Authorization = getAuthorization();
          Object.assign(config.headers, { Authorization });
        }
        applyApiEncrypt(config);

        return config;
      },
      isBackendSuccess(response) {
        // 当后端返回码为 "0"（默认）时，表示请求成功
        // 如需调整该逻辑，可修改 `.env` 中的 `VITE_SERVICE_SUCCESS_CODE`
        return String(response.data.code) === import.meta.env.VITE_SERVICE_SUCCESS_CODE;
      },
      async onBackendFail(response, instance) {
        const authStore = useAuthStore();
        const responseCode = String(response.data.code);

        if (
          shouldDeferBackendFailToCaller({
            suppressErrorMessage: response.config.suppressErrorMessage,
            skipTokenRefresh: response.config.skipTokenRefresh
          })
        ) {
          return null;
        }

        function handleLogout() {
          authStore.resetStore();
        }

        function logoutAndCleanup() {
          handleLogout();
          window.removeEventListener('beforeunload', handleLogout);

          request.state.errMsgStack = request.state.errMsgStack.filter(msg => msg !== response.data.msg);
        }

        // 当后端返回码命中 `logoutCodes` 时，表示登录态已失效，需要提示后退出登录
        // 走 notifySessionExpired 而不是裸 resetStore：保证并发请求只弹一次 toast、只清一次状态
        const logoutCodes = parseServiceCodes(import.meta.env.VITE_SERVICE_LOGOUT_CODES);
        if (logoutCodes.includes(responseCode)) {
          notifySessionExpired();
          return null;
        }

        // 当后端返回码命中 `modalLogoutCodes` 时，表示通过弹窗提示后再退出登录
        const modalLogoutCodes = parseServiceCodes(import.meta.env.VITE_SERVICE_MODAL_LOGOUT_CODES);
        if (modalLogoutCodes.includes(responseCode) && !request.state.errMsgStack?.includes(response.data.msg)) {
          request.state.errMsgStack = [...(request.state.errMsgStack || []), response.data.msg];

          // 防止用户刷新页面绕过退出逻辑
          window.addEventListener('beforeunload', handleLogout);

          window.$messageBox
            ?.confirm(response.data.msg, $t('common.error'), {
              confirmButtonText: $t('common.confirm'),
              cancelButtonText: $t('common.cancel'),
              type: 'error',
              closeOnClickModal: false,
              closeOnPressEscape: false
            })
            .then(() => {
              logoutAndCleanup();
            });

          return null;
        }

        // 当后端返回码命中 `expiredTokenCodes` 时，表示 token 已过期，需要刷新 token
        // `refreshToken` 接口不能再返回 `expiredTokenCodes` 中的错误码，否则会形成死循环，应返回 `logoutCodes` 或 `modalLogoutCodes`
        const expiredTokenCodes = parseServiceCodes(import.meta.env.VITE_SERVICE_EXPIRED_TOKEN_CODES);
        if (expiredTokenCodes.includes(responseCode)) {
          if (response.config.skipTokenRefresh) {
            notifySessionExpired();
            return null;
          }

          const success = await handleExpiredRequest(request.state);
          if (success) {
            const Authorization = getAuthorization();
            Object.assign(response.config.headers, { Authorization });

            return instance.request(response.config) as Promise<AxiosResponse>;
          }
        }

        return null;
      },
      onError(error) {
        // 请求失败时，在这里统一处理错误提示

        let message = error.message;
        let backendErrorCode = '';

        if (error.code === 'ECONNABORTED' && error.message.includes('timeout')) {
          message = '请求超时，请稍后重试';
        }

        // 获取后端错误信息和错误码
        if (error.code === BACKEND_ERROR_CODE) {
          message = error.response?.data?.msg || message;
          backendErrorCode = String(error.response?.data?.code || '');
        }

        const suppressErrorMessage = Boolean(error.config?.suppressErrorMessage);
        const logoutCodes = parseServiceCodes(import.meta.env.VITE_SERVICE_LOGOUT_CODES);
        const modalLogoutCodes = parseServiceCodes(import.meta.env.VITE_SERVICE_MODAL_LOGOUT_CODES);
        const expiredTokenCodes = parseServiceCodes(import.meta.env.VITE_SERVICE_EXPIRED_TOKEN_CODES);
        if (
          shouldSuppressErrorMessage({
            backendErrorCode,
            suppressErrorMessage,
            logoutCodes,
            modalLogoutCodes,
            expiredTokenCodes
          })
        ) {
          return;
        }

        showErrorMsg(request.state, message);
      }
    }
  )
);

export const demoRequest = createRequest(
  {
    baseURL: otherBaseURL.demo
  },
  {
    transform(response: AxiosResponse<App.Service.DemoResponse>) {
      return response.data.result;
    },
    async onRequest(config) {
      const { headers } = config;

      // 设置 token
      const token = localStg.get('token');
      const Authorization = token ? `Bearer ${token}` : null;
      Object.assign(headers, { Authorization });

      return config;
    },
    isBackendSuccess(response) {
      // 当后端返回码为 "200" 时，表示请求成功
      // 如有需要可以自行调整这段逻辑
      return response.data.status === '200';
    },
    async onBackendFail(_response) {
      // 当后端返回码不是 "200" 时，表示请求失败
      // 例如：token 过期后可以在这里刷新 token 并重试请求
    },
    onError(error) {
      // 请求失败时，在这里统一处理错误提示

      let message = error.message;

      // 展示后端返回的错误信息
      if (error.code === BACKEND_ERROR_CODE) {
        message = error.response?.data?.message || message;
      }

      window.$message?.error(message);
    }
  }
);
