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

import com.eactive.apim.portal.portalproperty.entity.PortalProperty;
import com.eactive.apim.portal.portalproperty.entity.PortalPropertyGroup;
import com.eactive.apim.portal.portalproperty.entity.PortalPropertyId;
import com.eactive.apim.portal.portalproperty.repository.PortalPropertyGroupRepository;
import com.eactive.apim.portal.portalproperty.repository.PortalPropertyRepository;
import com.eactive.eai.rms.common.base.OnlBaseService;
import com.eactive.eai.rms.onl.manage.comm.property.PropertyGroupUI;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.Map;

@Service
@RequiredArgsConstructor
@Transactional(transactionManager = "transactionManagerForEMS")
public class PortalPropertyManService extends OnlBaseService {

    private final PortalPropertyGroupRepository portalPropertyGroupRepository;
    private final PortalPropertyRepository portalPropertyRepository;
    private final PortalPropertyGroupMapper propertyGroupMapper;

    public void saveProperties(String propertyGroupName, Map<String, String> properties) {
        // Get or create PortalPropertyGroup
        PortalPropertyGroup group = portalPropertyGroupRepository.findById(propertyGroupName)
            .orElseThrow(() -> new IllegalArgumentException("Property group not found: " + propertyGroupName));

        // Update or create properties
        properties.forEach((propertyName, propertyValue) -> {
            PortalPropertyId propertyId = new PortalPropertyId(propertyGroupName, propertyName);
            PortalProperty property = portalPropertyRepository.findById(propertyId)
                .orElseGet(() -> {
                    PortalProperty newProperty = new PortalProperty();
                    newProperty.setId(propertyId);
                    newProperty.setPortalPropertyGroup(group);
                    return newProperty;
                });

            property.setPropertyValue(propertyValue);
            portalPropertyRepository.save(property);
        });
    }

    public PortalPropertyGroup getByIdWithSortedProps(String propertyGroupName) {
        PortalPropertyGroup propGroup = portalPropertyGroupRepository.findById(propertyGroupName).orElse(null);
        if (propGroup != null && propGroup.getPortalProperties() != null) {
            propGroup.getPortalProperties().sort(Comparator.comparing(p -> p.getId().getPropertyName()));
        }
        return propGroup;
    }

    public PropertyGroupUI selectDetail(String prptyGroupName) {
        PortalPropertyGroup propGroup = getByIdWithSortedProps(prptyGroupName);
        return propGroup == null ? null : propertyGroupMapper.toVo(propGroup);
    }

    public void update(PropertyGroupUI propertyGroupUI) {
        PortalPropertyGroup propGroup = portalPropertyGroupRepository.findByIdWithProperties(propertyGroupUI.getPrptyGroupName()).orElse(new PortalPropertyGroup());

        propGroup.getPortalProperties().clear();
        propertyGroupMapper.updateToEntity(propertyGroupUI, propGroup);

        propGroup.getPortalProperties().forEach(prop -> prop.setPortalPropertyGroup(propGroup));

        portalPropertyGroupRepository.save(propGroup);
    }

}
