package com.pressing.domain.ironing;

import com.pressing.common.entity.SiteScopedEntity;
import com.pressing.domain.customer.Customer;
import com.pressing.domain.invoice.PaymentStatus;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.ColumnDefault;

// En-tête (Maître) d'un reçu de repassage. Peut regrouper plusieurs articles
// (IroningInvoiceLine) sous un même numéro de reçu, comme Invoice/InvoiceLine.
@Getter
@Setter
@Entity
@Table(name = "ironing_invoices")
public class IroningInvoice extends SiteScopedEntity {

    @Column(nullable = false, unique = true, length = 40)
    private String invoiceNumber;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "customer_id")
    private Customer customer;

    @Column(nullable = false)
    private LocalDateTime invoiceDateTime;

    // @ColumnDefault permet a Hibernate (ddl-auto: update) de generer un
    // "ADD COLUMN ... DEFAULT ... NOT NULL" valide meme si la table contient deja
    // des lignes (sans defaut, Postgres rejette l'ajout d'une colonne NOT NULL).
    @Column(nullable = false, length = 40)
    @ColumnDefault("'VALIDATED'")
    private String status = "VALIDATED";

    @Column(length = 255)
    private String notes;

    @Column(nullable = false, precision = 12, scale = 2)
    @ColumnDefault("0")
    private BigDecimal amountTtc = BigDecimal.ZERO;

    @Column(nullable = false, precision = 12, scale = 2)
    @ColumnDefault("0")
    private BigDecimal amountPaid = BigDecimal.ZERO;

    @Column(nullable = false, precision = 12, scale = 2)
    @ColumnDefault("0")
    private BigDecimal amountDue = BigDecimal.ZERO;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false, length = 40)
    @ColumnDefault("'UNPAID'")
    private PaymentStatus paymentStatus = PaymentStatus.UNPAID;

    @OneToMany(mappedBy = "ironingInvoice", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<IroningInvoiceLine> lines = new ArrayList<>();
}
