package de.effigenix.infrastructure.production.web.dto;

import de.effigenix.domain.production.Batch;

import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.List;

public record BatchResponse(
        String id,
        String batchNumber,
        String recipeId,
        String status,
        String plannedQuantity,
        String plannedQuantityUnit,
        String actualQuantity,
        String actualQuantityUnit,
        String waste,
        String wasteUnit,
        String remarks,
        LocalDate productionDate,
        LocalDate bestBeforeDate,
        List<ConsumptionResponse> consumptions,
        OffsetDateTime createdAt,
        OffsetDateTime updatedAt,
        OffsetDateTime completedAt,
        String cancellationReason,
        OffsetDateTime cancelledAt
) {
    public static BatchResponse from(Batch batch) {
        var consumptions = batch.consumptions().stream()
                .map(ConsumptionResponse::from)
                .toList();

        return new BatchResponse(
                batch.id().value(),
                batch.batchNumber().value(),
                batch.recipeId().value(),
                batch.status().name(),
                batch.plannedQuantity().amount().toPlainString(),
                batch.plannedQuantity().uom().name(),
                batch.actualQuantity() != null ? batch.actualQuantity().amount().toPlainString() : null,
                batch.actualQuantity() != null ? batch.actualQuantity().uom().name() : null,
                batch.waste() != null ? batch.waste().amount().toPlainString() : null,
                batch.waste() != null ? batch.waste().uom().name() : null,
                batch.remarks(),
                batch.productionDate(),
                batch.bestBeforeDate(),
                consumptions,
                batch.createdAt(),
                batch.updatedAt(),
                batch.completedAt(),
                batch.cancellationReason(),
                batch.cancelledAt()
        );
    }
}
