package com.microservices.pms.productLibrary.domain.vo;

import com.alibaba.fastjson2.JSONValidator;
import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.common.core.utils.html.EscapeUtil;
import com.microservices.pms.productLibrary.domain.PmsProductLibraryComponent;
import com.microservices.pms.productLibrary.domain.PmsProductLibraryRepositories;
import com.microservices.pms.productLibrary.domain.ProductLibraryRepositoriesFormatEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * 制品Raw输入对象
 *
 * @author otto
 * @date 2024-07-04
 */
@Data
@ApiModel("制品Raw输入对象")
public class ComponentRawInputVo {
    @ApiModelProperty(value = "制品库标识", hidden = true)
    private String repoName;
    @ApiModelProperty(value = "快照库标识", hidden = true)
    private String snapshotName;
    @ApiModelProperty(value = "组织标识", hidden = true)
    private String enterpriseIdentifier;
    @ApiModelProperty(value = "制品格式", hidden = true)
    private String format = ProductLibraryRepositoriesFormatEnum.RAW.getKey();
    @ApiModelProperty(value = "制品版本")
    @NotBlank(message = "制品版本不能为空")
    private String version;
    @ApiModelProperty(value = "制品额外属性")
    private String attributes;
    @ApiModelProperty(value = "文件列表")
    @Size(min = 1, message = "最少上传一个文件")
    @Valid
    private List<AssetRawInputVo> assetList;
    @ApiModelProperty(value = "目录：上传文件的目录（例如/path/to/files/）")
    @NotNull(message = "目录不能为空")
    private String directory;

    public String getNexusRepoName() {
        if (ProductLibraryRepositoriesFormatEnum.isProduct(format)) {
            return PmsProductLibraryRepositories.getNexusProductRepositoriesName(
                    enterpriseIdentifier,
                    repoName
            );
        } else if (StringUtils.isNotEmpty(snapshotName)) {
            return PmsProductLibraryRepositories.getNexusSnapshotRepositoriesName(
                    enterpriseIdentifier,
                    repoName,
                    snapshotName
            );
        } else {
            return PmsProductLibraryRepositories.getNexusRepositoriesName(
                    enterpriseIdentifier,
                    repoName
            );
        }

    }

    public PmsProductLibraryComponent toPmsProductLibraryComponent(Long repoId, String name) {
        PmsProductLibraryComponent pmsProductLibraryComponent = new PmsProductLibraryComponent();
        pmsProductLibraryComponent.setRepoId(repoId);
        pmsProductLibraryComponent.setName(name);
        // 去除目录中重复的斜杠
        directory = directory.replaceAll("/+", "/");
        // 去除目录开头所有的斜杠
        pmsProductLibraryComponent.setPath(EscapeUtil.removeExtraSlashOfUrl(String.format("%s/%s", directory.replaceAll("^/+", ""), name)));
        pmsProductLibraryComponent.setGroup(directory);
        pmsProductLibraryComponent.setVersion(version);
        // 判断额外属性是否为JSON字符串
        if (StringUtils.isNotEmpty(attributes)) {
            if (JSONValidator.from(attributes).validate()) {
                pmsProductLibraryComponent.setAttributes(attributes);
            } else {
                throw new ServiceException("用户提供的制品额外属性格式错误，请确保为JSON格式");
            }
        }
        pmsProductLibraryComponent.setPmsEnterpriseIdentifier(enterpriseIdentifier);
        return pmsProductLibraryComponent;
    }
}
