package com.eactive.eai.rms.onl.apim.portalmain;

import com.eactive.apim.portal.portalproperty.service.PortalPropertyService;
import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.onl.apim.apigroup.ApiGroupManService;
import com.eactive.eai.rms.onl.apim.apigroup.ui.ApiGroupUI;
import com.eactive.eai.rms.onl.apim.portalproperty.PortalPropertyManService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
@RequiredArgsConstructor
@Transactional(transactionManager = "transactionManagerForEMS")
public class PortalMainManService extends BaseService {

    private static final String PORTAL_PROPERTY_GROUP = "Portal";
    private static final String SERVICE_LIST_KEY = "main.service.list";
    private static final String KEYWORD_LIST_KEY = "main.keyword.list";

    private final PortalPropertyService portalPropertyService;
    private final PortalPropertyManService portalPropertyManService;

    private final ApiGroupManService apiGroupManService;

    public PortalMainUI loadPortalMain() throws Exception {
        PortalMainUI portalMainUI = new PortalMainUI();

        // Get properties from Portal property group
        Map<String, String> properties = portalPropertyService.getPortalPropertiesAsMap(PORTAL_PROPERTY_GROUP);

        // Load service list
        String serviceIds = properties.getOrDefault(SERVICE_LIST_KEY, "");
        if (StringUtils.hasText(serviceIds)) {
            List<ApiGroupUI> services = new ArrayList<>();
            for (String serviceId : serviceIds.split(",")) {
                if (StringUtils.hasText(serviceId)) {
                    ApiGroupUI apiGroupUI = apiGroupManService.selectDetail(serviceId.trim());
                    services.add(apiGroupUI);
                }
            }
            portalMainUI.setServices(services);
        }

        // Load search tags
        String searchTags = properties.getOrDefault(KEYWORD_LIST_KEY, "");
        portalMainUI.setSearchTags(searchTags);

        return portalMainUI;
    }

    public void savePortalMain(PortalMainUI portalMainUI) {
        Map<String, String> properties = portalPropertyService.getPortalPropertiesAsMap(PORTAL_PROPERTY_GROUP);

        // Convert service list to comma-separated string
        if (portalMainUI.getServices() != null && !portalMainUI.getServices().isEmpty()) {
            String serviceIds = portalMainUI.getServices().stream()
                .map(ApiGroupUI::getId)
                .collect(Collectors.joining(","));
            properties.put(SERVICE_LIST_KEY, serviceIds);
        }

        // Save search tags
        if (StringUtils.hasText(portalMainUI.getSearchTags())) {
            properties.put(KEYWORD_LIST_KEY, StringEscapeUtils.unescapeHtml(portalMainUI.getSearchTags()));
        }

        portalPropertyManService.saveProperties(PORTAL_PROPERTY_GROUP, properties);

    }

}
