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

import jakarta.persistence.*;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Entity
@Table(name = "articles")
public class ArticleEntity {

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

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

    @Column(name = "article_number", nullable = false, unique = true, length = 50)
    private String articleNumber;

    @Column(name = "category_id", nullable = false, length = 36)
    private String categoryId;

    @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 = "article", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
    private List<SalesUnitEntity> salesUnits = new ArrayList<>();

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "article_suppliers", joinColumns = @JoinColumn(name = "article_id"))
    @Column(name = "supplier_id")
    private Set<String> supplierIds = new HashSet<>();

    protected ArticleEntity() {}

    public ArticleEntity(String id, String name, String articleNumber, String categoryId,
                         String status, OffsetDateTime createdAt, OffsetDateTime updatedAt) {
        this.id = id;
        this.name = name;
        this.articleNumber = articleNumber;
        this.categoryId = categoryId;
        this.status = status;
        this.createdAt = createdAt;
        this.updatedAt = updatedAt;
    }

    public String getId() { return id; }
    public String getName() { return name; }
    public String getArticleNumber() { return articleNumber; }
    public String getCategoryId() { return categoryId; }
    public String getStatus() { return status; }
    public OffsetDateTime getCreatedAt() { return createdAt; }
    public OffsetDateTime getUpdatedAt() { return updatedAt; }
    public List<SalesUnitEntity> getSalesUnits() { return salesUnits; }
    public Set<String> getSupplierIds() { return supplierIds; }

    public void setId(String id) { this.id = id; }
    public void setName(String name) { this.name = name; }
    public void setArticleNumber(String articleNumber) { this.articleNumber = articleNumber; }
    public void setCategoryId(String categoryId) { this.categoryId = categoryId; }
    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 setSalesUnits(List<SalesUnitEntity> salesUnits) { this.salesUnits = salesUnits; }
    public void setSupplierIds(Set<String> supplierIds) { this.supplierIds = supplierIds; }
}
