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

import de.effigenix.domain.inventory.InventoryCount;
import de.effigenix.shared.security.UserLookupPort;

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

public record InventoryCountResponse(
        String id,
        String storageLocationId,
        LocalDate countDate,
        String initiatedBy,
        String completedBy,
        String status,
        String cancellationReason,
        Instant createdAt,
        List<CountItemResponse> countItems
) {
    public static InventoryCountResponse from(InventoryCount count, UserLookupPort userLookup) {
        return new InventoryCountResponse(
                count.id().value(),
                count.storageLocationId().value(),
                count.countDate(),
                userLookup.resolveUsername(count.initiatedBy()).orElse(count.initiatedBy()),
                count.completedBy() != null
                        ? userLookup.resolveUsername(count.completedBy()).orElse(count.completedBy())
                        : null,
                count.status().name(),
                count.cancellationReason(),
                count.createdAt(),
                count.countItems().stream()
                        .map(CountItemResponse::from)
                        .toList()
        );
    }
}
