package cn.reghao.autodop.dmaster.app.model.po.config;

import cn.reghao.autodop.dmaster.app.model.constant.AppType;
import cn.reghao.autodop.dmaster.app.model.constant.EnvList;
import cn.reghao.autodop.dmaster.app.model.po.config.build.CompilerConfig;
import cn.reghao.autodop.dmaster.app.model.po.config.build.PackerConfig;
import cn.reghao.autodop.dmaster.app.model.po.config.build.RepoAuthConfig;
import cn.reghao.autodop.dmaster.util.db.BaseEntity;
import cn.reghao.autodop.dmaster.notification.model.po.NotifyGroup;
import cn.reghao.autodop.dmaster.util.validator.ValidEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.validator.constraints.Length;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author reghao
 * @date 2020-02-28 14:11:02
 */
@Getter
@Setter
@Entity
public class AppConfig extends BaseEntity<Integer> implements Cloneable {
    @Column(nullable = false, unique = true)
    @NotNull(message = "应用 ID 不能为 NULL")
    @Length(min = 4, max = 16, message = "应用 ID 长度为 4 ～ 16 个字符")
    private String appId;
    @NotBlank(message = "应用名字不能为空白字符串")
    private String appName;
    private String description;
    @ValidEnum(value = AppType.class, message = "请选择正确的应用类型")
    private String appType;
    @ValidEnum(value = EnvList.class, message = "请选择正确的环境类型")
    private String env;
    @NotBlank(message = "应用仓库不能为空白字符串")
    private String appRepo;
    @NotBlank(message = "仓库分支不能为空白字符串")
    private String repoBranch;
    @ManyToMany(cascade = CascadeType.REFRESH)
    @JoinColumn(name = "notify_group_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<NotifyGroup> notifyGroups;
    // TODO 添加定时构建部署选项

    // TODO 操作系统类型和版本，CPU 架构
    /* 构建配置 */
    @NotBlank(message = "代码目录不能为空白字符串")
    private String codeDirname;
    // 编译应用时所处的目录，以仓库目录为起点
    @NotBlank(message = "代码编译主目录不能为空白字符串")
    private String compileHome;
    private String execBinName;
    // TODO packType 是 docker 时使用 Dockerfile
    @Lob
    @Column(columnDefinition="text")
    private String packScript;

    // buildConfig
    @ManyToOne(cascade = CascadeType.REFRESH)
    @JoinColumn(name = "repo_auth_config_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    //@Valid
    private RepoAuthConfig repoAuthConfig;
    @ManyToOne(cascade = CascadeType.REFRESH)
    @JoinColumn(name = "compiler_config_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    //@Valid
    private CompilerConfig compilerConfig;
    @ManyToOne(cascade = CascadeType.REFRESH)
    @JoinColumn(name = "packer_config_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    //@Valid
    private PackerConfig packerConfig;

    /* 部署配置 */
    @ElementCollection(targetClass = DeployConfig.class)
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<DeployConfig> deployConfigs;
    /* 运行配置 */
    @Embedded
    private RunningConfig runningConfig;

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
