package de.effigenix.infrastructure.production.persistence.entity;

import jakarta.persistence.*;

import java.math.BigDecimal;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "recipes",
        uniqueConstraints = @UniqueConstraint(name = "uq_recipe_name_version", columnNames = {"name", "version"}))
public class RecipeEntity {

    @Id
    @Column(name = "id", nullable = false, length = 36)
    private String id;

    @Column(name = "name", nullable = false, length = 200)
    private String name;

    @Column(name = "version", nullable = false)
    private int version;

    @Column(name = "type", nullable = false, length = 30)
    private String type;

    @Column(name = "description", length = 2000)
    private String description;

    @Column(name = "yield_percentage", nullable = false)
    private int yieldPercentage;

    @Column(name = "shelf_life_days")
    private Integer shelfLifeDays;

    @Column(name = "output_quantity", nullable = false, precision = 19, scale = 6)
    private BigDecimal outputQuantity;

    @Column(name = "output_uom", nullable = false, length = 20)
    private String outputUom;

    @Column(name = "article_id", nullable = false, length = 36)
    private String articleId;

    @Column(name = "status", nullable = false, length = 20)
    private String status;

    @Column(name = "created_at", nullable = false, updatable = false)
    private OffsetDateTime createdAt;

    @Column(name = "updated_at", nullable = false)
    private OffsetDateTime updatedAt;

    @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
    @OrderBy("position ASC")
    private List<IngredientEntity> ingredients = new ArrayList<>();

    @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
    @OrderBy("stepNumber ASC")
    private List<ProductionStepEntity> productionSteps = new ArrayList<>();

    protected RecipeEntity() {}

    public RecipeEntity(String id, String name, int version, String type, String description,
                        int yieldPercentage, Integer shelfLifeDays, BigDecimal outputQuantity,
                        String outputUom, String articleId, String status, OffsetDateTime createdAt, OffsetDateTime updatedAt) {
        this.id = id;
        this.name = name;
        this.version = version;
        this.type = type;
        this.description = description;
        this.yieldPercentage = yieldPercentage;
        this.shelfLifeDays = shelfLifeDays;
        this.outputQuantity = outputQuantity;
        this.outputUom = outputUom;
        this.articleId = articleId;
        this.status = status;
        this.createdAt = createdAt;
        this.updatedAt = updatedAt;
    }

    public String getId() { return id; }
    public String getName() { return name; }
    public int getVersion() { return version; }
    public String getType() { return type; }
    public String getDescription() { return description; }
    public int getYieldPercentage() { return yieldPercentage; }
    public Integer getShelfLifeDays() { return shelfLifeDays; }
    public BigDecimal getOutputQuantity() { return outputQuantity; }
    public String getOutputUom() { return outputUom; }
    public String getArticleId() { return articleId; }
    public String getStatus() { return status; }
    public OffsetDateTime getCreatedAt() { return createdAt; }
    public OffsetDateTime getUpdatedAt() { return updatedAt; }
    public List<IngredientEntity> getIngredients() { return ingredients; }
    public List<ProductionStepEntity> getProductionSteps() { return productionSteps; }

    public void setId(String id) { this.id = id; }
    public void setName(String name) { this.name = name; }
    public void setVersion(int version) { this.version = version; }
    public void setType(String type) { this.type = type; }
    public void setDescription(String description) { this.description = description; }
    public void setYieldPercentage(int yieldPercentage) { this.yieldPercentage = yieldPercentage; }
    public void setShelfLifeDays(Integer shelfLifeDays) { this.shelfLifeDays = shelfLifeDays; }
    public void setOutputQuantity(BigDecimal outputQuantity) { this.outputQuantity = outputQuantity; }
    public void setOutputUom(String outputUom) { this.outputUom = outputUom; }
    public void setArticleId(String articleId) { this.articleId = articleId; }
    public void setStatus(String status) { this.status = status; }
    public void setCreatedAt(OffsetDateTime createdAt) { this.createdAt = createdAt; }
    public void setUpdatedAt(OffsetDateTime updatedAt) { this.updatedAt = updatedAt; }
    public void setIngredients(List<IngredientEntity> ingredients) { this.ingredients = ingredients; }
    public void setProductionSteps(List<ProductionStepEntity> productionSteps) { this.productionSteps = productionSteps; }
}
