package com.arms.api.issue.almapi.model.vo;

import com.arms.api.issue.almapi.model.vo.cloudjiraspec.CloudJiraIssueRawDataVO;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.*;


@Getter
@Slf4j
public class CloudJiraIssueVOCollection {

    private final List<CloudJiraIssueVO> cloudJiraIssueVOS;

    private CloudJiraIssueVOCollection(List<CloudJiraIssueVO> cloudJiraIssueVOS){
        this.cloudJiraIssueVOS = cloudJiraIssueVOS;
    }

    public static CloudJiraIssueVOCollection of(List<CloudJiraIssueVO> cloudJiraIssueVOS){
        return new CloudJiraIssueVOCollection(cloudJiraIssueVOS);
    }

    public static CloudJiraIssueVOCollection fromCloudJiraIssueRawDataVO(List<CloudJiraIssueRawDataVO> cloudJiraIssueRawDataVOS){
        return new CloudJiraIssueVOCollection(
                cloudJiraIssueRawDataVOS
                    .stream()
                    .map(a->CloudJiraIssueVO.builder().cloudJiraIssueRawDataVO(a).build())
                    .toList()
        );
    }

    public List<String> keys(){
        return cloudJiraIssueVOS.stream().map(a->a.getCloudJiraIssueRawDataVO().getKey())
                .toList();
    }

    public List<CloudJiraIssueVO> appliedLinkedIssuePdServiceVO() {

        Map<CloudJiraIssueVO, List<AlmIssueWithRequirementVO>> cloudJiraIssueVOListMap
            = cloudJiraIssueVOS
                .stream()
                .collect(groupingBy(vo -> vo, mapping(CloudJiraIssueVO::getAlmIssueWithRequirementVO, toList())));

        return cloudJiraIssueVOListMap
                .entrySet()
                .stream()
                .map(a-> a.getKey().appliedLinkedIssuePdServiceElement(a.getValue()))
                .toList();

    }

}
