package com.qqchen.deploy.backend.deploy.enums;

import lombok.Getter;

/**
 * 部署记录状态枚举
 *
 * @author qqchen
 * @since 2025-11-02
 */
@Getter
public enum DeployRecordStatusEnums {

    /**
     * 已创建
     */
    CREATED("CREATED", "已创建"),

    /**
     * 待审批
     */
    PENDING_APPROVAL("PENDING_APPROVAL", "待审批"),

    /**
     * 运行中
     */
    RUNNING("RUNNING", "运行中"),

    /**
     * 部署成功
     */
    SUCCESS("SUCCESS", "部署成功"),

    /**
     * 部署失败
     */
    FAILED("FAILED", "部署失败"),

    /**
     * 部分成功（工作流完成但存在失败的节点）
     */
    PARTIAL_SUCCESS("PARTIAL_SUCCESS", "部分成功"),

    /**
     * 审批被拒绝（终态）
     */
    REJECTED("REJECTED", "审批被拒绝"),

    /**
     * 已取消（终态）
     * 用于：手动取消部署、超时自动取消等场景
     */
    CANCELLED("CANCELLED", "已取消"),

    /**
     * 已终止（终态）
     */
    TERMINATED("TERMINATED", "已终止");

    private final String code;
    private final String description;

    DeployRecordStatusEnums(String code, String description) {
        this.code = code;
        this.description = description;
    }

    /**
     * 将工作流状态转换为部署记录状态
     */
    public static DeployRecordStatusEnums fromWorkflowStatus(String workflowStatus) {
        return switch (workflowStatus) {
            case "CREATED" -> CREATED;
            case "RUNNING" -> RUNNING;
            case "COMPLETED" -> SUCCESS;
            case "COMPLETED_WITH_ERRORS" -> PARTIAL_SUCCESS;
            case "FAILED" -> FAILED;
            case "TERMINATED" -> TERMINATED;
            case "SUSPENDED" -> RUNNING; // 工作流暂停时，部署记录仍视为运行中（可恢复）
            default -> CREATED;
        };
    }
}

