package com.qqchen.deploy.backend.deploy.dto;

import com.qqchen.deploy.backend.deploy.enums.BuildTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;

import java.util.Date;
import java.util.List;

/**
 * 部署执行请求
 *
 * @author qqchen
 * @since 2025-11-02
 */
@Data
@Schema(description = "部署执行请求")
public class DeployExecuteRequest {

    @Schema(description = "构建类型", required = true)
    @NotNull(message = "构建类型不能为空")
    private BuildTypeEnum buildType;

    @Schema(description = "Jenkins配置（buildType为JENKINS时必填）")
    @Valid
    private JenkinsConfig jenkins;

    @Schema(description = "团队ID", required = true)
    @NotNull(message = "团队ID不能为空")
    private Long teamId;

    @Schema(description = "团队应用关联ID", required = true)
    @NotNull(message = "团队应用关联ID不能为空")
    private Long teamApplicationId;

    @Schema(description = "应用ID", required = true)
    @NotNull(message = "应用ID不能为空")
    private Long applicationId;

    @Schema(description = "应用编码", required = true)
    @NotBlank(message = "应用编码不能为空")
    private String applicationCode;

    @Schema(description = "应用名称", required = true)
    @NotBlank(message = "应用名称不能为空")
    private String applicationName;

    @Schema(description = "环境ID", required = true)
    @NotNull(message = "环境ID不能为空")
    private Long environmentId;

    @Schema(description = "环境编码", required = true)
    @NotBlank(message = "环境编码不能为空")
    private String environmentCode;

    @Schema(description = "环境名称", required = true)
    @NotBlank(message = "环境名称不能为空")
    private String environmentName;

    @Schema(description = "审批配置", required = true)
    @NotNull(message = "审批配置不能为空")
    @Valid
    private ApprovalConfig approval;

    @Schema(description = "通知配置", required = true)
    @NotNull(message = "通知配置不能为空")
    @Valid
    private NotificationConfig notification;

    @Schema(description = "执行时间", required = true)
    private Date deployDate = new Date();

    @Schema(description = "部署备注")
    private String deployRemark;

    private String deployUser;

    @Schema(description = "源Git仓库配置")
    @Valid
    private GitRepositoryConfig sourceRepository;

    @Schema(description = "目标Git仓库配置")
    @Valid
    private GitRepositoryConfig targetRepository;

    /**
     * Git仓库配置
     */
    @Data
    @Schema(description = "Git仓库配置")
    public static class GitRepositoryConfig {
        @Schema(description = "Git系统ID")
        private Long systemId;

        @Schema(description = "Git项目ID（外部系统的项目ID）")
        private Long projectId;

        @Schema(description = "分支名称")
        private String branch;
    }

    /**
     * Jenkins配置
     */
    @Data
    @Schema(description = "Jenkins配置")
    public static class JenkinsConfig {
        @Schema(description = "Jenkins服务器ID", required = true)
        @NotNull(message = "Jenkins服务器ID不能为空")
        private Long serverId;

        @Schema(description = "Jenkins任务名称", required = true)
        @NotBlank(message = "Jenkins任务名称不能为空")
        private String jobName;

        @Schema(description = "Git分支")
        private String branch;
    }

    /**
     * 审批配置
     */
    @Data
    @Schema(description = "审批配置")
    public static class ApprovalConfig {
        @Schema(description = "是否需要审批", required = true)
        @NotNull(message = "是否需要审批不能为空")
        private Boolean required;

        @Schema(description = "审批人ID列表（逗号分隔）")
        private List<String> userNames;
    }

    /**
     * 通知配置
     */
    @Data
    @Schema(description = "通知配置")
    public static class NotificationConfig {

        @Schema(description = "通知渠道ID")
        private Long notificationChannelId;

        @Schema(description = "是否启用审批前提醒")
        private Boolean preApprovalNotificationEnabled;

        @Schema(description = "审批前提醒模板ID")
        private Long preApprovalNotificationTemplateId;

        @Schema(description = "是否启用构建通知")
        private Boolean buildNotificationEnabled;

        @Schema(description = "构建通知模板ID")
        private Long buildNotificationTemplateId;

        @Schema(description = "构建失败时是否发送日志文件")
        private Boolean buildFailureFileEnabled;
    }
}

