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

import com.arms.api.serverinfo.model.vo.ServerInfoVO;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.taskadapter.redmineapi.bean.Issue;
import com.taskadapter.redmineapi.bean.IssueRelation;
import com.taskadapter.redmineapi.bean.User;
import lombok.*;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
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 OnPremissRedmineIssueVO {

    private Issue issue;

    private User user;

    private ServerInfoVO serverInfoVO;

    private AlmIssueWithRequirementVO almIssueWithRequirementVO;

    private boolean excludeFromSave;

    private List<Long> linkedIssuePdServiceIds;

    private List<Long> linkedIssuePdServiceVersions;

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

    public OnPremissRedmineIssueVO appliedLinkedIssuePdServiceElement(List<AlmIssueWithRequirementVO> almIssueWithRequirementVOS){
        return OnPremissRedmineIssueVO.builder()
                .issue(this.issue)
                .user(this.user)
                .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.getProjectId()+"_"+issue.getId();
    }

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

    public Set<String> relationRecentIds(){

        List<IssueRelation> issueRelations = new ArrayList<>(this.issue.getRelations());

        Integer issueId = issue.getId();

        return issueRelations.stream().map(a->{
            if(issueId.equals(a.getIssueId())){
                return almIssueWithRequirementVO.extractIssueKeyFromMainKey(String.valueOf(a.getIssueToId()));
            }else{
                return almIssueWithRequirementVO.extractIssueKeyFromMainKey(String.valueOf(a.getIssueId()));
            }
        }).collect(Collectors.toSet());

    }


}
