package com.cf.imes.module.system.controller.admin.organ.vo.organ;

import cn.hutool.core.util.ObjectUtil;
import com.cf.imes.framework.common.validation.Mobile;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.time.LocalDateTime;

@Schema(description = "管理后台 - 组织创建/修改 Request VO")
@Data
public class OrganSaveReqVO {

    @Schema(description = "组织编号", example = "1024")
    private Long id;

    @Schema(description = "组织名", requiredMode = Schema.RequiredMode.REQUIRED, example = "晨丰")
    @NotNull(message = "组织名不能为空")
    private String name;

    @Schema(description = "联系人", requiredMode = Schema.RequiredMode.REQUIRED, example = "晨丰")
    @NotNull(message = "联系人不能为空")
    private String contactName;

    @Schema(description = "联系手机", example = "15601691300")
    @Mobile
    private String contactMobile;

    @Schema(description = "组织状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    @NotNull(message = "组织状态")
    private Integer status;

    @Schema(description = "绑定域名", example = "https://www.cf.com")
    private String website;

    @Schema(description = "组织套餐编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
    @NotNull(message = "组织套餐编号不能为空")
    private Long packageId;

    @Schema(description = "过期时间", requiredMode = Schema.RequiredMode.REQUIRED)
    @NotNull(message = "过期时间不能为空")
    private LocalDateTime expireTime;

    @Schema(description = "账号数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
    @NotNull(message = "账号数量不能为空")
    private Integer accountCount;

    // ========== 仅【创建】时，需要传递的字段 ==========

    @Schema(description = "用户账号", requiredMode = Schema.RequiredMode.REQUIRED, example = "chenfeng")
    @Pattern(regexp = "^[a-zA-Z0-9]{4,30}$", message = "用户账号由 数字、字母 组成")
    @Size(min = 4, max = 30, message = "用户账号长度为 4-30 个字符")
    private String username;

    @Schema(description = "密码", requiredMode = Schema.RequiredMode.REQUIRED, example = "123456")
    @Length(min = 4, max = 16, message = "密码长度为 4-16 位")
    private String password;

    @AssertTrue(message = "用户账号、密码不能为空")
    @JsonIgnore
    public boolean isUsernameValid() {
        return id != null // 修改时，不需要传递
                || (ObjectUtil.isAllNotEmpty(username, password)); // 新增时，必须都传递 username、password
    }

    @Schema(description = "是否大型组织")
    private Boolean large;

    @Schema(description = "组织编号")
    private String code;

    @Schema(description = "地址")
    private String address;

    @Schema(description = "备注")
    @Length(min = 0, max = 200, message = "备注不可太长")
    private String remark;

}
