package com.mvrent.carrental.web;

import com.mvrent.carrental.model.Booking;

import java.math.BigDecimal;

/** Flat JSON view of a Booking for the AJAX Manage Bookings screen. */
public record BookingDto(
        Long id,
        String customerName,
        String customerEmail,
        String carName,
        boolean withDriver,
        String driverName,
        String revenueOwner,
        String vendorName,
        String pickupDate,
        String returnDate,
        BigDecimal totalAmount,
        BigDecimal amountPaid,
        BigDecimal balance,
        String status
) {
    public static BookingDto from(Booking b) {
        return from(b, BigDecimal.ZERO);
    }

    public static BookingDto from(Booking b, BigDecimal paid) {
        BigDecimal total = b.getTotalAmount() != null ? b.getTotalAmount() : BigDecimal.ZERO;
        BigDecimal amountPaid = paid != null ? paid : BigDecimal.ZERO;
        return new BookingDto(
                b.getId(),
                b.getUser() != null ? b.getUser().getFullName() : null,
                b.getUser() != null ? b.getUser().getEmail() : null,
                b.getCar() != null ? b.getCar().getMake() + " " + b.getCar().getModel() : null,
                b.isWithDriver(),
                (b.isWithDriver() && b.getDriver() != null) ? b.getDriver().getName() : null,
                b.getRevenueOwner() != null ? b.getRevenueOwner().name() : "OWNED",
                b.getVendor() != null ? b.getVendor().getName() : null,
                String.valueOf(b.getPickupDate()),
                String.valueOf(b.getReturnDate()),
                b.getTotalAmount(),
                amountPaid,
                total.subtract(amountPaid),
                b.getStatus() != null ? b.getStatus().name() : "PENDING"
        );
    }
}
