package com.qqchen.deploy.backend.workflow.entity;

import com.fasterxml.jackson.databind.JsonNode;
import com.qqchen.deploy.backend.framework.annotation.LogicDelete;
import com.qqchen.deploy.backend.workflow.dto.definition.workflow.WorkflowDefinitionGraph;
import com.qqchen.deploy.backend.workflow.enums.WorkflowCategoryEnum;
import com.qqchen.deploy.backend.workflow.enums.WorkflowDefinitionStatusEnums;
import com.qqchen.deploy.backend.workflow.enums.WorkflowTriggerTypeEnum;
import com.qqchen.deploy.backend.workflow.entity.converter.WorkflowGraphType;
import com.qqchen.deploy.backend.framework.domain.Entity;
import com.vladmihalcea.hibernate.type.json.JsonType;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.Type;

import java.util.List;

/**
 * 工作流定义实体
 */
@Data
@Table(name = "workflow_definition")
@jakarta.persistence.Entity
@EqualsAndHashCode(callSuper = true)
@LogicDelete
public class WorkflowDefinition extends Entity<Long> {

    /**
     * 流程名称
     */
    @Column(nullable = false)
    private String name;

    /**
     * 流程标识
     */
    @Column(name = "`key`", nullable = false)
    private String key;

    /**
     * 流程分类
     */
    @Column(name = "category", nullable = false)
    @Enumerated(EnumType.STRING)
    private WorkflowCategoryEnum category;


    @Column(name = "triggers", nullable = false)
    @Convert(converter = WorkflowTriggerTypeListConverter.class)
    private List<WorkflowTriggerTypeEnum> triggers;

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

    /**
     * 流程版本
     */
    @Column(name = "flow_version", nullable = false)
    private Integer flowVersion;

    /**
     * BPMN XML内容
     */
    @Column(name = "bpmn_xml", columnDefinition = "TEXT")
    private String bpmnXml;

    /**
     * 流程图数据，包含节点和连线的位置、样式等信息
     */
    @Type(WorkflowGraphType.class)
    @Column(name = "graph", columnDefinition = "json")
    private WorkflowDefinitionGraph graph;

    /**
     * 表单配置
     */
    @Type(JsonType.class)
    @Column(name = "local_variables_schema", columnDefinition = "json")
    private JsonNode localVariablesSchema;

    /**
     * 流程状态
     */
    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    private WorkflowDefinitionStatusEnums status;

    /**
     * 流程描述
     */
    @Column(columnDefinition = "TEXT")
    private String description;

    /**
     * 是否可执行
     */
    @Column(name = "is_executable", nullable = false)
    private Boolean isExecutable = true;

    /**
     * 目标命名空间
     */
    @Column(name = "target_namespace")
    private String targetNamespace = "http://www.flowable.org/test";


    /**
     * 流程标签，用于分组和过滤
     */
    @Type(JsonType.class)
    @Column(name = "tags", columnDefinition = "json")
    private JsonNode tags;
}
