package cn.reghao.bnt.web.devops.build.model.po;

import cn.reghao.bnt.common.docker.model.DockerAuth;
import cn.reghao.bnt.common.msg.constant.PackType;
import cn.reghao.bnt.web.devops.build.model.provider.PackerConfigGroupSequenceProvider;
import cn.reghao.bnt.web.devops.builder.model.PackerDto;
import cn.reghao.bnt.web.util.BaseEntity;
import cn.reghao.jutil.jdk.web.validator.ValidEnum;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.group.GroupSequenceProvider;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;

/**
 * 打包方式配置
 *
 * @author reghao
 * @date 2020-05-13 16:40:22
 */
@GroupSequenceProvider(PackerConfigGroupSequenceProvider.class)
@NoArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "devops_packer_config")
public class PackerConfig extends BaseEntity {
    @ValidEnum(value = PackType.class, message = "请选择正确的打包类型")
    private String type;
    @NotBlank(message = "打包名字不能包含空白符")
    @Length(max = 255, message = "打包名字的最大长度不能超过 255 个字符")
    @Column(nullable = false, unique = true)
    private String name;
    // 打包后的应用存放的位置，可以是一个本地目录，也可以是一个网络位置
    // 根据打包类型来确定
    @NotBlank(groups = { DockerPacker.class }, message = "可执行包的存放位置不能为空白字符串")
    private String targetPath;
    @NotNull(groups = { DockerPacker.class }, message = "dockerRegistry 不能为 null")
    @OneToOne
    private DockerRegistry dockerRegistry;
    // 构建生成的可执行文件及配置文件, 脚本文件等所在的目录名, 目录位于 appRootPath 下
    @NotBlank(groups = { ZipPacker.class }, message = "bin 目录不能为空")
    private String binDirname;

    public interface DockerPacker {
    }

    public interface ZipPacker {
    }

    public PackerDto getPackerDto() {
        return new PackerDto(this.type, this.name, this.targetPath, this.binDirname);
    }

    public DockerAuth getDockerAuth() {
        DockerRegistry registry = this.dockerRegistry;
        if (registry != null) {
            return new DockerAuth(registry.getRegistryUrl(), registry.getUsername(), registry.getPassword());
        }

        return null;
    }
}
