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.JdbcTypeCode;
import org.hibernate.type.SqlTypes;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

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

/**
 * 工作流定义实体
 * 
 * 对应数据库表: workflow_definitions
 * 存储前端编辑器生成的JSON工作流定义
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Table(name = "workflow_definitions")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(ignoreUnknown = true)
public class WorkflowDefinition {

    @Id
    @Column(name = "id", length = 64)
    private String id;

    @Column(name = "name", nullable = false)
    private String name;

    @Column(name = "description", columnDefinition = "TEXT")
    private String description;

    /**
     * 工作流定义JSON
     * 包含nodes、edges、variables等完整定义
     */
    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "definition", nullable = false, columnDefinition = "JSON")
    private JsonNode definition;

    /**
     * Flowable流程定义ID（转换后生成）
     */
    @Column(name = "flowable_process_definition_id", length = 64)
    private String flowableProcessDefinitionId;

    /**
     * Flowable部署ID
     */
    @Column(name = "flowable_deployment_id", length = 64)
    private String flowableDeploymentId;

    /**
     * 状态：draft, active, archived
     */
    @Column(name = "status", length = 20)
    @Enumerated(EnumType.STRING)
    private WorkflowStatus status = WorkflowStatus.DRAFT;

    /**
     * 创建者
     */
    @Column(name = "created_by", length = 100)
    private String createdBy;

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

    /**
     * 更新时间
     */
    @LastModifiedDate
    @Column(name = "updated_at", nullable = false)
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    private LocalDateTime updatedAt;

    /**
     * 工作流状态枚举
     */
    public enum WorkflowStatus {
        DRAFT("草稿"),
        ACTIVE("激活"),
        ARCHIVED("归档");

        private final String description;

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

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