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

import com.taskadapter.redmineapi.bean.Issue;
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 OnPremissRedmineVOCollection {

    private final List<OnPremissRedmineIssueVO> onPremissRedmineIssueVO;

    private OnPremissRedmineVOCollection(List<OnPremissRedmineIssueVO> onPremissRedmineIssueVO){
        this.onPremissRedmineIssueVO = onPremissRedmineIssueVO;
    }

    public static OnPremissRedmineVOCollection of(List<OnPremissRedmineIssueVO> onPremissRedmineIssueVO){
        return new OnPremissRedmineVOCollection(onPremissRedmineIssueVO);
    }

    public static OnPremissRedmineVOCollection fromIssues(List<Issue> issues){
        return new OnPremissRedmineVOCollection(
            issues
                .stream()
                .map(a->OnPremissRedmineIssueVO.builder().issue(a).build())
                .toList()
        );
    }

    public List<String> keys(){
        return onPremissRedmineIssueVO.stream().map(a->String.valueOf(a.getIssue().getId()))
                .toList();
    }

    public List<OnPremissRedmineIssueVO> appliedLinkedIssuePdServiceVO() {

        Map<OnPremissRedmineIssueVO, List<AlmIssueWithRequirementVO>> onPremissRedmineIssueVOListMap
            = onPremissRedmineIssueVO
                .stream()
                .collect(groupingBy(vo -> vo, mapping(OnPremissRedmineIssueVO::getAlmIssueWithRequirementVO, toList())));

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

    }

}
