package com.rj.util;

import com.rj.common.LbThirdIntegrationConstants;
import com.rj.entity.LbThirdIntegrationConfig;
import org.springframework.util.StringUtils;

/**
 * 从 {@link LbThirdIntegrationConfig} 解析第三方 API 地址与请求头。
 */
public final class LbThirdIntegrationConfigUtil {

    private LbThirdIntegrationConfigUtil() {
    }

    public static String resolveGoodsApiBaseUrl(LbThirdIntegrationConfig config) {
        String base = trimTrailingSlash(requireText(config.getAdminBaseUrl(), "adminBaseUrl"));
        String path = config.getGoodsApiPath();
        if (!StringUtils.hasText(path)) {
            path = LbThirdIntegrationConstants.DEFAULT_GOODS_API_PATH;
        }
        path = path.trim();
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        return base + path;
    }

    public static String resolveGoodsApiOrigin(LbThirdIntegrationConfig config) {
        if (StringUtils.hasText(config.getGoodsApiOrigin())) {
            return config.getGoodsApiOrigin().trim();
        }
        return trimTrailingSlash(requireText(config.getWebBaseUrl(), "webBaseUrl"));
    }

    public static String resolveGoodsApiReferer(LbThirdIntegrationConfig config) {
        if (StringUtils.hasText(config.getGoodsApiReferer())) {
            return config.getGoodsApiReferer().trim();
        }
        return trimTrailingSlash(requireText(config.getWebBaseUrl(), "webBaseUrl")) + "/";
    }

    public static int resolveGoodsPageLimit(LbThirdIntegrationConfig config) {
        Integer limit = config.getGoodsPageLimit();
        if (limit == null || limit < 1) {
            return LbThirdIntegrationConstants.DEFAULT_GOODS_PAGE_LIMIT;
        }
        return limit;
    }

    public static String resolveUserSelectBaseUrl(LbThirdIntegrationConfig config) {
        String base = trimTrailingSlash(requireText(config.getAdminBaseUrl(), "adminBaseUrl"));
        String path = config.getUserSelectPath();
        if (!StringUtils.hasText(path)) {
            path = LbThirdIntegrationConstants.DEFAULT_USER_SELECT_PATH;
        }
        path = path.trim();
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        return base + path;
    }

    public static String resolveUserReferer(LbThirdIntegrationConfig config) {
        if (StringUtils.hasText(config.getUserReferer())) {
            return config.getUserReferer().trim();
        }
        return trimTrailingSlash(requireText(config.getAdminBaseUrl(), "adminBaseUrl"))
                + "/app/admin/user/index";
    }

    public static int resolveUserPageLimit(LbThirdIntegrationConfig config) {
        Integer limit = config.getUserPageLimit();
        if (limit == null || limit < 1) {
            return LbThirdIntegrationConstants.DEFAULT_USER_PAGE_LIMIT;
        }
        return limit;
    }

    public static String resolveOrderSelectBaseUrl(LbThirdIntegrationConfig config) {
        String base = trimTrailingSlash(requireText(config.getAdminBaseUrl(), "adminBaseUrl"));
        String path = config.getOrderSelectPath();
        if (!StringUtils.hasText(path)) {
            path = LbThirdIntegrationConstants.DEFAULT_ORDER_SELECT_PATH;
        }
        path = path.trim();
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        return base + path;
    }

    public static String resolveOrderReferer(LbThirdIntegrationConfig config) {
        if (StringUtils.hasText(config.getOrderReferer())) {
            return config.getOrderReferer().trim();
        }
        return trimTrailingSlash(requireText(config.getAdminBaseUrl(), "adminBaseUrl"))
                + "/app/admin/order/index";
    }

    public static int resolveOrderPageLimit(LbThirdIntegrationConfig config) {
        Integer limit = config.getOrderPageLimit();
        if (limit == null || limit < 1) {
            return LbThirdIntegrationConstants.DEFAULT_ORDER_PAGE_LIMIT;
        }
        return limit;
    }

    /**
     * 组装 Cookie 请求头：优先完整 cookie，否则 {@code PHPSID=} + phpsid。
     */
    public static String resolveCookieHeader(LbThirdIntegrationConfig config) {
        String c = firstNonBlank(config.getCookie());
        if (c != null) {
            return c;
        }
        String id = firstNonBlank(config.getPhpsid());
        if (id != null) {
            return "PHPSID=" + id;
        }
        return "";
    }

    private static String firstNonBlank(String s) {
        if (s == null) {
            return null;
        }
        String t = s.trim();
        return t.isEmpty() ? null : t;
    }

    private static String requireText(String value, String fieldName) {
        if (!StringUtils.hasText(value)) {
            throw new IllegalStateException(fieldName + " 未配置");
        }
        return value.trim();
    }

    private static String trimTrailingSlash(String url) {
        String trimmed = url.trim();
        while (trimmed.endsWith("/")) {
            trimmed = trimmed.substring(0, trimmed.length() - 1);
        }
        return trimmed;
    }
}
