package com.mvrent.carrental.model;

import jakarta.persistence.*;
import lombok.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * An aggregated settlement statement for a vendor over a period:
 * gross rental revenue, organization commission, vendor driver charges and the
 * resulting net payable.
 */
@Entity
@Table(name = "vendor_settlements")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class VendorSettlement {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "vendor_id")
    private Vendor vendor;

    private LocalDate periodStart;
    private LocalDate periodEnd;

    @Builder.Default
    private int tripCount = 0;

    private BigDecimal grossRevenue;
    private BigDecimal commissionAmount;
    private BigDecimal driverCharges;
    private BigDecimal netPayable;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    @Builder.Default
    private SettlementStatus status = SettlementStatus.DRAFT;

    @Column(nullable = false, updatable = false)
    private LocalDateTime generatedAt;

    @PrePersist
    void onCreate() {
        if (generatedAt == null) generatedAt = LocalDateTime.now();
    }
}
