package cn.axzo.msg.center.message.domain.vo;

import cn.axzo.msg.center.message.domain.param.RelationTemplateMapInitParam;
import cn.axzo.msg.center.service.enums.PushTerminalEnum;
import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;

import javax.validation.constraints.NotBlank;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author cold_blade
 * @date 2023/10/27
 * @version 1.0
 */
@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RelationTemplateMapInitRequest {

    private Collection<MapEntry> entries;
    @NotBlank(message = "groupNodeCode is required")
    private String groupNodeCode;

    public RelationTemplateMapInitParam toInitParam() {
        Map<Long, List<PushTerminalEnum>> map = Collections.emptyMap();
        if (CollectionUtils.isNotEmpty(entries)) {
            map = entries.stream()
                    .collect(Collectors.toMap(MapEntry::getKey, MapEntry::getValue));
        }
        return RelationTemplateMapInitParam.builder()
                .groupNodeCode(groupNodeCode)
                .relationPushTerminalMap(map)
                .build();
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    @Data
    static class MapEntry {

        /**
         * key
         */
        private Long key;
        /**
         * value
         */
        private List<PushTerminalEnum> value;
    }
}
