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;

/**
 * 节点类型元数据实体
 * 
 * 对应数据库表: node_types
 * 存储节点类型的字段定义、输出结构、实现类等元数据
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Table(name = "node_types")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(ignoreUnknown = true)
public class NodeType {

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

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

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

    /**
     * 节点分类：api, database, logic, notification, transform, other
     */
    @Column(name = "category", length = 50)
    @Enumerated(EnumType.STRING)
    private NodeCategory category;

    @Column(name = "icon", length = 100)
    private String icon;

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

    /**
     * 字段定义JSON
     * 包含节点配置表单的所有字段定义
     */
    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "fields", nullable = false, columnDefinition = "JSON")
    private JsonNode fields;

    /**
     * 输出结构定义JSON Schema
     * 用于前端字段映射组件展示可用字段
     */
    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "output_schema", columnDefinition = "JSON")
    private JsonNode outputSchema;

    /**
     * Java实现类的完整类名
     */
    @Column(name = "implementation_class", nullable = false)
    private String implementationClass;

    /**
     * 是否启用
     */
    @Column(name = "enabled", nullable = false)
    private Boolean enabled = true;
    
    /**
     * 显示顺序
     */
    @Column(name = "display_order")
    private Integer displayOrder;

    @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 NodeCategory {
        API("API接口"),
        DATABASE("数据库"),
        LOGIC("逻辑控制"),
        NOTIFICATION("通知消息"),
        TRANSFORM("数据转换"),
        OTHER("其他");

        private final String description;

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

        public String getDescription() {
            return description;
        }
    }
    
    /**
     * 检查是否启用
     */
    public boolean isEnabled() {
        return enabled != null && enabled;
    }
}