package space.nstart.pcp_tgu_service.entity

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.EnumType
import jakarta.persistence.Enumerated
import jakarta.persistence.Id
import jakarta.persistence.Table
import java.time.LocalDateTime
import java.util.UUID
import space.nstart.pcp.pcp_types_lib.utils.UtcDateTimes

/**
 * Одна попытка выдачи persisted plan через один BPMN process instance.
 */
@Entity
@Table(name = "plan_issue_attempt")
class PlanIssueAttemptEntity(
    @Id
    @Column(name = "attempt_id", nullable = false, updatable = false)
    var attemptId: UUID,

    @Column(name = "plan_id", nullable = false)
    var planId: UUID,

    @Column(name = "attempt_no", nullable = false)
    var attemptNo: Int,

    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false)
    var status: PlanIssueAttemptStatus = PlanIssueAttemptStatus.STARTING,

    @Column(name = "process_instance_key")
    var processInstanceKey: Long? = null,

    @Column(name = "decision_event_id")
    var decisionEventId: UUID? = null,

    @Column(name = "decision_reason", columnDefinition = "TEXT")
    var decisionReason: String? = null,

    @Column(name = "started_at")
    var startedAt: LocalDateTime? = null,

    @Column(name = "finished_at")
    var finishedAt: LocalDateTime? = null,

    @Column(name = "created_at", nullable = false)
    var createdAt: LocalDateTime = UtcDateTimes.now(),

    @Column(name = "updated_at", nullable = false)
    var updatedAt: LocalDateTime = UtcDateTimes.now()
) {
    /** Создаёт пустой экземпляр сущности, необходимый для JPA. */
    constructor() : this(attemptId = UUID.randomUUID(), planId = UUID.randomUUID(), attemptNo = 0)
}
