package com.codechecker.api.model;

import java.util.ArrayList;
import java.util.List;

/**
 * 单条 API 变更报告（路径 / 方法 / 参数各自独立，不与其他类型混合）。
 */
public class EndpointChangeReport {
    private final ApiChangeKind changeKind;
    private final String httpMethod;
    private final String oldHttpMethod;
    private final String uri;
    private final String oldUri;
    private final String sourceFile;
    private final String controllerClass;
    private final String endpointDescription;
    private final boolean dtoFollowUp;
    private final String relatedDtoClassName;
    private final List<ParameterChange> parameterChanges = new ArrayList<>();

    public EndpointChangeReport(ApiChangeKind changeKind, String httpMethod, String oldHttpMethod,
                                String uri, String oldUri, String sourceFile, String controllerClass,
                                String endpointDescription) {
        this(changeKind, httpMethod, oldHttpMethod, uri, oldUri, sourceFile, controllerClass,
                endpointDescription, false, null);
    }

    public EndpointChangeReport(ApiChangeKind changeKind, String httpMethod, String oldHttpMethod,
                                String uri, String oldUri, String sourceFile, String controllerClass,
                                String endpointDescription, boolean dtoFollowUp, String relatedDtoClassName) {
        this.changeKind = changeKind;
        this.httpMethod = httpMethod;
        this.oldHttpMethod = oldHttpMethod;
        this.uri = uri;
        this.oldUri = oldUri;
        this.sourceFile = sourceFile;
        this.controllerClass = controllerClass;
        this.endpointDescription = endpointDescription == null ? "" : endpointDescription;
        this.dtoFollowUp = dtoFollowUp;
        this.relatedDtoClassName = relatedDtoClassName;
    }

    /** 基于已有报告创建 Dto 跟进产生的副本 */
    public static EndpointChangeReport dtoFollowUp(EndpointChangeReport source, String relatedDtoClassName) {
        EndpointChangeReport copy = new EndpointChangeReport(
                source.getChangeKind(),
                source.getHttpMethod(),
                source.getOldHttpMethod(),
                source.getUri(),
                source.getOldUri(),
                source.getSourceFile(),
                source.getControllerClass(),
                source.getEndpointDescription(),
                true,
                relatedDtoClassName);
        source.getParameterChanges().forEach(copy::addParameterChange);
        return copy;
    }

    public ApiChangeKind getChangeKind() {
        return changeKind;
    }

    public String getHttpMethod() {
        return httpMethod;
    }

    public String getOldHttpMethod() {
        return oldHttpMethod;
    }

    public String getUri() {
        return uri;
    }

    public String getOldUri() {
        return oldUri;
    }

    public String getSourceFile() {
        return sourceFile;
    }

    public String getControllerClass() {
        return controllerClass;
    }

    public String getEndpointDescription() {
        return endpointDescription;
    }

    public List<ParameterChange> getParameterChanges() {
        return parameterChanges;
    }

    public void addParameterChange(ParameterChange change) {
        parameterChanges.add(change);
    }

    public boolean hasParameterChanges() {
        return !parameterChanges.isEmpty();
    }

    public boolean isDtoFollowUp() {
        return dtoFollowUp;
    }

    public String getRelatedDtoClassName() {
        return relatedDtoClassName;
    }
}
