package com.agent.management.model.entity;

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

import java.time.LocalDateTime;

/**
 * v1.1：工作流暂停点（clarify_request 触发时持久化）。
 *
 * <p>用于：</p>
 * <ul>
 *   <li>前端断线重连后通过 runId 查询当前是否有未答 clarify，恢复 Askfor UI</li>
 *   <li>审计：记录每次 clarify 的问题、选项、用户回答</li>
 *   <li>兜底清理：长期未答（&gt;30 天）的记录由定时任务清理</li>
 * </ul>
 *
 * <p>状态流转：{@code UNANSWERED} → {@code ANSWERED}（用户提交答案）/
 * {@code UNANSWERED} → {@code TIMEOUT}（工作流超时 abort 后由引擎标记）</p>
 */
@Data
@Entity
@Table(name = "workflow_pause",
        indexes = {
                @Index(name = "idx_workflow_pause_run_id", columnList = "run_id"),
                @Index(name = "idx_workflow_pause_clarify_id", columnList = "clarify_id", unique = true)
        })
public class WorkflowPause {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /** 关联工作流运行 ID（与 workflow_run.run_id 对应） */
    @Column(name = "run_id", nullable = false)
    private String runId;

    /** 触发 clarify 的节点 ID */
    @Column(name = "node_id", nullable = false)
    private String nodeId;

    /** clarify_gateway 生成的唯一 ID，Bridge 侧用于匹配 answer */
    @Column(name = "clarify_id", nullable = false)
    private String clarifyId;

    /** 模型抛出的问题文本 */
    @Column(nullable = false, columnDefinition = "TEXT")
    private String question;

    /** 选项 JSON 数组（如 ["是","否","自定义"]） */
    @Column(columnDefinition = "TEXT")
    private String choices;

    /** 暂停触发时间 */
    @Column(name = "created_at")
    private LocalDateTime createdAt;

    /** 用户回答时间；UNANSWERED 时为 null */
    @Column(name = "answered_at")
    private LocalDateTime answeredAt;

    /** 用户的回答文本（选项值或自定义输入） */
    @Column(columnDefinition = "TEXT")
    private String answer;

    /** UNANSWERED / ANSWERED / TIMEOUT */
    @Column(nullable = false)
    private String status;

    @PrePersist
    protected void onCreate() {
        if (createdAt == null) createdAt = LocalDateTime.now();
        if (status == null) status = "UNANSWERED";
    }
}
