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

import cn.reghao.bnt.web.devops.build.model.provider.CompilerConfigGroupSequenceProvider;
import cn.reghao.bnt.web.devops.builder.model.CompilerDto;
import cn.reghao.bnt.web.devops.builder.model.constant.CompileType;
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.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.group.GroupSequenceProvider;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import java.util.List;

/**
 * 编译方式配置
 *
 * @author reghao
 * @date 2021-02-05 17:37:28
 */
@GroupSequenceProvider(CompilerConfigGroupSequenceProvider.class)
@NoArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "devops_compiler_config")
public class CompilerConfig extends BaseEntity {
    @ValidEnum(value = CompileType.class, message = "请选择正确的编译类型")
    private String type;
    @Pattern(regexp = "^\\S*$", message = "编译名字不能包含空白符")
    @Length(max = 255, message = "编译名字的最大长度不能超过 255 个字符")
    @Column(nullable = false, unique = true)
    private String name;
    private String homePath;
    @NotBlank(groups = { ShellCompiler.class}, message = "编译命令不能为空")
    @Length(max = 1000, message = "编译命令长度不能超过 1000 个字符")
    private String compileCmd;
    @NotBlank(groups = { ShellCompiler.class}, message = "编译器版本命令不能为空")
    @Length(max = 1000, message = "编译器版本命令长度不能超过 1000 个字符")
    private String versionCmd;
    @NotBlank(groups = { DockerCompiler.class}, message = "编译器版本命令不能为空")
    private String compilerImage;
    @ElementCollection(targetClass = CompilerBind.class)
    @LazyCollection(LazyCollectionOption.FALSE)
    @CollectionTable(name = "devops_compiler_config_binds")
    private List<CompilerBind> compilerBinds;

    public interface NoneCompiler {
    }

    public interface ShellCompiler {
    }

    public interface MavenCompiler {
    }

    public interface DockerCompiler {
    }

    public CompilerDto getCompilerDto() {
        return new CompilerDto(this.type, this.name, this.homePath, this.compileCmd, this.compilerImage, this.compilerBinds);
    }
}
