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 job на запуск короткого BPMN-процесса обновления слотов accepted plan.
 */
@Entity
@Table(name = "accepted_plan_slots_update_job")
class AcceptedPlanSlotsUpdateJobEntity(
    @Id
    @Column(name = "plan_id", nullable = false, updatable = false)
    var planId: UUID,

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

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

    @Column(name = "locked_by")
    var lockedBy: String? = null,

    @Column(name = "locked_until")
    var lockedUntil: LocalDateTime? = null,

    @Column(name = "attempts", nullable = false)
    var attempts: Int = 0,

    @Column(name = "last_error", columnDefinition = "TEXT")
    var lastError: String? = 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(planId = UUID.randomUUID())
}
