package space.nstart.pcp.routepassportconsumer.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 com.fasterxml.jackson.databind.JsonNode
import org.hibernate.annotations.JdbcTypeCode
import org.hibernate.type.SqlTypes
import java.time.LocalDateTime
import java.util.UUID

@Entity
@Table(name = "route_passport_outbox")
class RoutePassportOutboxEntity(
    @Id
    @Column(name = "id", nullable = false)
    var id: UUID,

    @Column(name = "route_id", nullable = false)
    var routeId: UUID,

    @Column(name = "event_type", nullable = false, length = 128)
    var eventType: String,

    @Column(name = "topic", nullable = false, length = 255)
    var topic: String,

    @Column(name = "message_key", nullable = false, length = 255)
    var messageKey: String,

    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "payload", nullable = false, columnDefinition = "jsonb")
    var payload: JsonNode,

    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "headers", columnDefinition = "jsonb")
    var headers: JsonNode? = null,

    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 32)
    var status: RoutePassportOutboxStatus,

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

    @Column(name = "max_attempts", nullable = false)
    var maxAttempts: Int,

    @Column(name = "next_attempt_at", nullable = false)
    var nextAttemptAt: LocalDateTime,

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

    @Column(name = "last_error", columnDefinition = "TEXT")
    var lastError: String? = null,

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

    @Column(name = "updated_at", nullable = false)
    var updatedAt: LocalDateTime,

    @Column(name = "published_at")
    var publishedAt: LocalDateTime? = null,
)
