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

import cn.reghao.bnt.web.devops.build.model.provider.RepoAuthGroupSequenceProvider;
import cn.reghao.bnt.web.devops.builder.model.RepoAuth;
import cn.reghao.bnt.web.devops.builder.model.constant.RepoAuthType;
import cn.reghao.bnt.web.devops.builder.model.constant.RepoType;
import cn.reghao.bnt.web.util.BaseEntity;
import cn.reghao.jutil.jdk.web.validator.ValidEnum;
import lombok.Getter;
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.Table;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;

/**
 * 仓库认证配置
 *
 * @author reghao
 * @date 2020-05-13 22:50:38
 */
@GroupSequenceProvider(RepoAuthGroupSequenceProvider.class)
@Getter
@Setter
@Entity
@Table(name = "devops_repo_auth_config")
public class RepoAuthConfig extends BaseEntity {
    @ValidEnum(value = RepoType.class, message = "请选择正确的仓库类型")
    private String type;
    @Pattern(regexp = "^\\S*$", message = "认证名字不能包含空白符")
    @Length(max = 255, message = "认证名字的最大长度不能超过 255 个字符")
    @Column(nullable = false, unique = true)
    private String name;
    @ValidEnum(value = RepoAuthType.class, message = "请选择的仓库认证类型")
    private String authType;

    @NotBlank(groups = {HttpRepoAuth.class}, message = "认证用户名不能为空")
    @Length(max = 255, message = "认证用户名的最大长度不能超过 255 个字符")
    private String username;
    @NotBlank(groups = {HttpRepoAuth.class}, message = "认证密码不能为空")
    @Length(max = 255, message = "认证密码的最大长度不能超过 255 个字符")
    private String password;

    @NotBlank(groups = {SshRepoAuth.class}, message = "认证私钥不能为空")
    @Column(columnDefinition="text")
    private String rsaPrikey;

    public interface NoneRepoAuth {
    }

    public interface HttpRepoAuth {
    }

    public interface SshRepoAuth {
    }

    public RepoAuth getRepoAuth() {
        return new RepoAuth(this.type, this.name, this.authType, this.username, this.password, this.rsaPrikey);
    }
}
