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

import de.effigenix.domain.masterdata.customer.Customer;
import de.effigenix.domain.masterdata.customer.CustomerPreference;
import io.swagger.v3.oas.annotations.media.Schema;

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

@Schema(requiredProperties = {"id", "name", "type", "billingAddress", "contactInfo", "deliveryAddresses", "preferences", "status", "createdAt", "updatedAt"})
public record CustomerResponse(
        String id,
        String name,
        String type,
        AddressResponse billingAddress,
        ContactInfoResponse contactInfo,
        @Schema(nullable = true) PaymentTermsResponse paymentTerms,
        List<DeliveryAddressResponse> deliveryAddresses,
        @Schema(nullable = true) FrameContractResponse frameContract,
        List<String> preferences,
        String status,
        OffsetDateTime createdAt,
        OffsetDateTime updatedAt
) {
    public static CustomerResponse from(Customer customer) {
        return new CustomerResponse(
                customer.id().value(),
                customer.name().value(),
                customer.type().name(),
                AddressResponse.from(customer.billingAddress()),
                ContactInfoResponse.from(customer.contactInfo()),
                PaymentTermsResponse.from(customer.paymentTerms()),
                customer.deliveryAddresses().stream().map(DeliveryAddressResponse::from).toList(),
                FrameContractResponse.from(customer.frameContract()),
                customer.preferences().stream().map(CustomerPreference::name).toList(),
                customer.status().name(),
                customer.createdAt(),
                customer.updatedAt()
        );
    }
}
