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

import jakarta.persistence.*;

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

@Entity
@Table(name = "reservations")
public class ReservationEntity {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "stock_id", nullable = false)
    private StockEntity stock;

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

    @Column(name = "reference_id", nullable = false, length = 100)
    private String referenceId;

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

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

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

    @Column(name = "reserved_at", nullable = false)
    private Instant reservedAt;

    @OneToMany(mappedBy = "reservation", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
    private List<StockBatchAllocationEntity> allocations = new ArrayList<>();

    protected ReservationEntity() {}

    public ReservationEntity(String id, StockEntity stock, String referenceType, String referenceId,
                             BigDecimal quantityAmount, String quantityUnit, String priority,
                             Instant reservedAt) {
        this.id = id;
        this.stock = stock;
        this.referenceType = referenceType;
        this.referenceId = referenceId;
        this.quantityAmount = quantityAmount;
        this.quantityUnit = quantityUnit;
        this.priority = priority;
        this.reservedAt = reservedAt;
    }

    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    public StockEntity getStock() { return stock; }
    public void setStock(StockEntity stock) { this.stock = stock; }
    public String getReferenceType() { return referenceType; }
    public void setReferenceType(String referenceType) { this.referenceType = referenceType; }
    public String getReferenceId() { return referenceId; }
    public void setReferenceId(String referenceId) { this.referenceId = referenceId; }
    public BigDecimal getQuantityAmount() { return quantityAmount; }
    public void setQuantityAmount(BigDecimal quantityAmount) { this.quantityAmount = quantityAmount; }
    public String getQuantityUnit() { return quantityUnit; }
    public void setQuantityUnit(String quantityUnit) { this.quantityUnit = quantityUnit; }
    public String getPriority() { return priority; }
    public void setPriority(String priority) { this.priority = priority; }
    public Instant getReservedAt() { return reservedAt; }
    public void setReservedAt(Instant reservedAt) { this.reservedAt = reservedAt; }
    public List<StockBatchAllocationEntity> getAllocations() { return allocations; }
    public void setAllocations(List<StockBatchAllocationEntity> allocations) { this.allocations = allocations; }
}
