package br.gov.sigefp.treasury.domain;

import br.gov.sigefp.common.domain.BaseEntity;
import jakarta.persistence.*;
import lombok.*;
import lombok.experimental.SuperBuilder;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Entidade que representa um workflow de aprovação hierárquica de pagamentos.
 * Gerencia o processo de autorização de pagamentos por níveis.
 */
@Entity
@Table(name = "payment_authorization", indexes = {
    @Index(name = "idx_auth_payment_order", columnList = "payment_order_id"),
    @Index(name = "idx_auth_payment_batch", columnList = "payment_batch_id"),
    @Index(name = "idx_auth_status", columnList = "status"),
    @Index(name = "idx_auth_requested_by", columnList = "requested_by")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public class PaymentAuthorization extends BaseEntity {

    @Column(name = "payment_order_id")
    private UUID paymentOrderId; // Ordem de pagamento individual

    @Column(name = "payment_batch_id")
    private UUID paymentBatchId; // Lote de pagamento

    @Column(name = "requested_by", nullable = false)
    private UUID requestedBy; // Usuário que solicitou

    @Column(name = "requested_at", nullable = false)
    private LocalDateTime requestedAt;

    @Column(name = "required_approval_level", nullable = false)
    private Integer requiredApprovalLevel; // Nível necessário (1, 2, 3...)

    @Column(name = "current_approval_level", nullable = false)
    @Builder.Default
    private Integer currentApprovalLevel = 1; // Nível atual

    @Column(nullable = false, length = 20)
    @Builder.Default
    private String status = "PENDING"; // PENDING, PARTIALLY_APPROVED, APPROVED, REJECTED

    @Column(name = "rejection_reason", columnDefinition = "TEXT")
    private String rejectionReason;

    @OneToMany(mappedBy = "authorization", cascade = CascadeType.ALL, orphanRemoval = true)
    @Builder.Default
    private List<Approval> approvals = new ArrayList<>(); // Histórico de aprovações
}
