package com.flowable.devops.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import jakarta.persistence.*;
import java.time.LocalDateTime;

/**
 * 工作流执行记录实体
 * 
 * 对应数据库表: workflow_executions
 * 扩展Flowable的流程实例，记录业务相关的执行信息
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Table(name = "workflow_executions")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(ignoreUnknown = true)
public class WorkflowExecution {

    @Id
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    @GeneratedValue(generator = "uuid")
    @Column(name = "id", length = 64)
    private String id;

    /**
     * 关联的工作流定义ID
     */
    @Column(name = "workflow_definition_id", nullable = false, length = 64)
    private String workflowDefinitionId;

    /**
     * Flowable流程实例ID
     */
    @Column(name = "flowable_process_instance_id", nullable = false, length = 64)
    private String flowableProcessInstanceId;

    /**
     * 执行输入参数
     */
    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "input", columnDefinition = "JSON")
    private JsonNode input;

    /**
     * 执行状态：running, completed, failed, cancelled
     */
    @Column(name = "status", length = 20, nullable = false)
    @Enumerated(EnumType.STRING)
    private ExecutionStatus status = ExecutionStatus.RUNNING;

    /**
     * 开始时间
     */
    @CreatedDate
    @Column(name = "started_at", nullable = false, updatable = false)
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    private LocalDateTime startedAt;

    /**
     * 结束时间
     */
    @Column(name = "ended_at")
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    private LocalDateTime endedAt;

    /**
     * 错误信息
     */
    @Column(name = "error_message", columnDefinition = "TEXT")
    private String errorMessage;

    /**
     * 错误堆栈
     */
    @Column(name = "error_stack", columnDefinition = "TEXT")
    private String errorStack;

    /**
     * 触发方式：manual, cron, webhook
     */
    @Column(name = "trigger_type", length = 20)
    @Enumerated(EnumType.STRING)
    private TriggerType triggerType = TriggerType.MANUAL;

    /**
     * 触发者
     */
    @Column(name = "triggered_by", length = 100)
    private String triggeredBy;

    /**
     * 关联工作流定义
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "workflow_definition_id", insertable = false, updatable = false)
    private WorkflowDefinition workflowDefinition;

    /**
     * 执行状态枚举
     */
    public enum ExecutionStatus {
        RUNNING("运行中"),
        COMPLETED("已完成"),
        FAILED("失败"),
        CANCELLED("已取消");

        private final String description;

        ExecutionStatus(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    /**
     * 触发方式枚举
     */
    public enum TriggerType {
        MANUAL("手动触发"),
        CRON("定时触发"),
        WEBHOOK("Webhook触发");

        private final String description;

        TriggerType(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }
}