package app.mealsmadeeasy.api.recipe;

import app.mealsmadeeasy.api.image.Image;
import app.mealsmadeeasy.api.user.User;
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.List;
import java.util.UUID;

@Entity
@Table(name = "recipe_draft")
@Data
public class RecipeDraft {

    public enum State {
        INFER,
        ENTER_DATA
    }

    @Data
    public static class RecipeDraftInference {
        private OffsetDateTime inferredAt;
        private String title;
        private String rawText;
    }

    @Data
    public static class IngredientDraft {
        private @Nullable String amount;
        private String name;
        private @Nullable String notes;
    }

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

    @Column(nullable = false)
    private OffsetDateTime created;
    private @Nullable OffsetDateTime modified;

    @Column(nullable = false)
    private State state;

    private @Nullable String slug;
    private @Nullable String title;
    private @Nullable Integer preparationTime;
    private @Nullable Integer cookingTime;
    private @Nullable Integer totalTime;

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

    @Type(JsonBinaryType.class)
    @Column(columnDefinition = "jsonb")
    private @Nullable List<IngredientDraft> ingredients;

    @ManyToOne(optional = false)
    @JoinColumn(name = "owner_id", nullable = false)
    private User owner;

    @ManyToOne
    @JoinColumn(name = "main_image_id")
    private @Nullable Image mainImage;

    @Type(JsonBinaryType.class)
    @Column(columnDefinition = "jsonb")
    private @Nullable List<RecipeDraftInference> inferences;

    @PrePersist
    private void prePersist() {
        this.created = OffsetDateTime.now();
    }

    @PreUpdate
    private void preUpdate() {
        this.modified = OffsetDateTime.now();
    }

}
