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

import com.arms.api.util.alm.JiraApi;
import com.atlassian.jira.rest.client.api.domain.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 OnPremissJiraIssueVOCollection {

    private final List<OnPremissJiraIssueVO> onPremissJiraIssueVOS;

    private OnPremissJiraIssueVOCollection(List<OnPremissJiraIssueVO> onPremissJiraIssueVOS){
        this.onPremissJiraIssueVOS = onPremissJiraIssueVOS;
    }

    public static OnPremissJiraIssueVOCollection of(List<OnPremissJiraIssueVO> onPremissJiraIssueVOS){
        return new OnPremissJiraIssueVOCollection(onPremissJiraIssueVOS);
    }

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

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

    public List<OnPremissJiraIssueVO> appliedLinkedIssuePdServiceVO() {

        Map<OnPremissJiraIssueVO, List<AlmIssueWithRequirementVO>> onPremissJiraIssueVOListMap
            = onPremissJiraIssueVOS
                .stream()
                .collect(groupingBy(vo -> vo, mapping(OnPremissJiraIssueVO::getAlmIssueWithRequirementVO, toList())));

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

    }

}
