package app.mealsmadeeasy.api.job;

import com.fasterxml.jackson.databind.JsonNode;
import io.hypersistence.utils.hibernate.type.json.JsonBinaryType;
import jakarta.persistence.*;
import lombok.Data;
import org.hibernate.annotations.Type;
import org.jetbrains.annotations.Nullable;

import java.time.OffsetDateTime;
import java.util.UUID;

@Entity
@Table(name = "job")
@Data
public class Job {

    public enum State {
        QUEUED, RUNNING, DONE, FAILED, DEAD
    }

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    @Column(nullable = false, updatable = false)
    private UUID id;

    @Column(nullable = false, updatable = false)
    private OffsetDateTime created;

    private @Nullable OffsetDateTime modified;

    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    private State state;

    @Column(nullable = false)
    private String jobKey;

    @Type(JsonBinaryType.class)
    @Column(nullable = false, columnDefinition = "jsonb")
    private JsonNode payload;

    @Column(nullable = false)
    private Integer attempts;

    @Column(nullable = false)
    private Integer maxAttempts;

    @Column(nullable = false)
    private OffsetDateTime runAfter;

    private @Nullable String lockedBy;

    private @Nullable OffsetDateTime lockedAt;

    @Column(columnDefinition = "TEXT")
    private @Nullable String lastError;

}
