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

import com.arms.api.serverinfo.model.vo.ServerInfoVO;
import com.arms.api.util.StreamUtil;
import com.atlassian.jira.rest.client.api.domain.Issue;
import com.atlassian.jira.rest.client.api.domain.IssueLink;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.*;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
@Slf4j
@EqualsAndHashCode(of = {"issue"})
public class OnPremissJiraIssueVO {

    private Issue issue;

    private ServerInfoVO serverInfoVO;

    private AlmIssueWithRequirementVO almIssueWithRequirementVO;

    private boolean excludeFromSave;

    private List<Long> linkedIssuePdServiceIds;

    private List<Long> linkedIssuePdServiceVersions;

    public OnPremissJiraIssueVO markAsExcludedFromSave(){
        return OnPremissJiraIssueVO.builder()
                .issue(this.issue)
                .serverInfoVO(this.serverInfoVO)
                .almIssueWithRequirementVO(this.almIssueWithRequirementVO)
                .excludeFromSave(true)
                .build();
    }

    public OnPremissJiraIssueVO appliedLinkedIssuePdServiceElement(List<AlmIssueWithRequirementVO> almIssueWithRequirementVOS){
        return OnPremissJiraIssueVO.builder()
                .issue(this.issue)
                .serverInfoVO(this.serverInfoVO)
                .almIssueWithRequirementVO(
                        almIssueWithRequirementVOS
                                .stream()
                                .filter(a->a.getIssueKey().equals(almIssueWithRequirementVO.getIssueKey()))
                                .findFirst()
                                .orElseGet(()->this.almIssueWithRequirementVO)
                )
                .linkedIssuePdServiceIds(almIssueWithRequirementVOS.stream().map(AlmIssueWithRequirementVO::getServiceId).toList())
                .linkedIssuePdServiceVersions(almIssueWithRequirementVOS.stream().flatMap(a->a.getVersions().stream()).toList())
                .excludeFromSave(this.excludeFromSave)
                .build();
    }

    public String recentId(){
        return this.serverInfoVO.getConnectId() +"_"+issue.getProject().getKey()+"_"+issue.getKey();
    }

    public String recentId(String key){
        return this.serverInfoVO.getConnectId() +"_"+issue.getProject().getKey()+"_"+key;
    }

    public Set<String> relationRecentIds(){

        List<IssueLink> issueRelations = StreamUtil.toStream(issue.getIssueLinks()).toList();

        return issueRelations
                .stream().map(a-> {
                    String projectKey = Arrays.stream(a.getTargetIssueKey().split("-")).findFirst().orElseGet(() -> almIssueWithRequirementVO.getProjectKeyOrId());
                    return almIssueWithRequirementVO.extractIssueKeyFromMainKey(projectKey,a.getTargetIssueKey());
                })
                .collect(Collectors.toSet());

    }



}
