package com.codechecker.api.model;

/**
 * API 参数或 RequestBody 嵌套字段变更。
 */
public class ParameterChange {
    public enum ChangeType {
        ADDED, REMOVED, MODIFIED, RENAMED
    }

    private final ChangeType changeType;
    private final String paramName;
    private final String oldName;
    private final String paramType;
    private final String description;
    private final String oldDescription;
    private final String source;
    private final String bodyParamName;
    private final String parentDto;
    private final String fieldPath;
    private final String detail;

    private ParameterChange(ChangeType changeType, String paramName, String oldName,
                            String paramType, String description, String oldDescription,
                            String source, String bodyParamName, String parentDto,
                            String fieldPath, String detail) {
        this.changeType = changeType;
        this.paramName = paramName;
        this.oldName = oldName;
        this.paramType = paramType;
        this.description = description;
        this.oldDescription = oldDescription;
        this.source = source;
        this.bodyParamName = bodyParamName;
        this.parentDto = parentDto;
        this.fieldPath = fieldPath;
        this.detail = detail;
    }

    public static ParameterChange added(String name, String type, String desc, String source,
                                        String bodyParam, String dto, String fieldPath) {
        return new ParameterChange(ChangeType.ADDED, name, null, type, desc, null,
                source, bodyParam, dto, fieldPath, null);
    }

    public static ParameterChange removed(String name, String type, String desc, String source,
                                          String bodyParam, String dto, String fieldPath) {
        return new ParameterChange(ChangeType.REMOVED, name, null, type, desc, null,
                source, bodyParam, dto, fieldPath, null);
    }

    public static ParameterChange modified(String name, String type, String desc,
                                           String detail, String source, String bodyParam,
                                           String dto, String fieldPath) {
        return new ParameterChange(ChangeType.MODIFIED, name, null, type, desc, null,
                source, bodyParam, dto, fieldPath, detail);
    }

    public static ParameterChange renamed(String oldName, String newName, String type, String desc,
                                          String source, String bodyParam, String dto, String fieldPath) {
        return new ParameterChange(ChangeType.RENAMED, newName, oldName, type, desc, null,
                source, bodyParam, dto, fieldPath, null);
    }

    public ChangeType getChangeType() {
        return changeType;
    }

    public String getParamName() {
        return paramName;
    }

    public String getOldName() {
        return oldName;
    }

    public String getParamType() {
        return paramType;
    }

    public String getDescription() {
        return description;
    }

    public String getSource() {
        return source;
    }

    public String getBodyParamName() {
        return bodyParamName;
    }

    public String getParentDto() {
        return parentDto;
    }

    public String getFieldPath() {
        return fieldPath;
    }

    public String getDetail() {
        return detail;
    }

    public boolean isBodyField() {
        return "body".equals(source);
    }

    public String displayName() {
        return fieldPath == null || fieldPath.isBlank() ? paramName : fieldPath;
    }
}
