package com.agent.management.model.entity;

import jakarta.persistence.*;
import lombok.Data;

import java.time.LocalDateTime;

/**
 * 工作流执行记录
 */
@Data
@Entity
@Table(name = "workflow_run")
public class WorkflowRun {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "workflow_id", nullable = false)
    private Long workflowId;

    /**
     * 工作流唯一标识（kebab-case name）冗余列，便于按 name 查询运行历史。
     * 旧数据可能为 null，按 workflowId 反查 Workflow.name 补齐。
     */
    @Column(name = "workflow_name")
    private String workflowName;

    @Column(name = "run_id", nullable = false, unique = true)
    private String runId;

    /** RUNNING, SUCCESS, FAILED */
    @Column(nullable = false)
    private String status;

    /** 输入参数 JSON */
    @Column(columnDefinition = "TEXT")
    private String inputs;

    /** 输出结果 JSON */
    @Column(columnDefinition = "TEXT")
    private String outputs;

    private String error;

    @Column(name = "started_at")
    private LocalDateTime startedAt;

    @Column(name = "completed_at")
    private LocalDateTime completedAt;

    @PrePersist
    protected void onCreate() {
        if (startedAt == null) startedAt = LocalDateTime.now();
    }
}
