package de.effigenix.infrastructure.masterdata.persistence.mapper;

import de.effigenix.domain.masterdata.*;
import de.effigenix.infrastructure.masterdata.persistence.entity.QualityCertificateEmbeddable;
import de.effigenix.infrastructure.masterdata.persistence.entity.SupplierEntity;
import de.effigenix.shared.common.Address;
import de.effigenix.shared.common.ContactInfo;
import de.effigenix.shared.common.PaymentTerms;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class SupplierMapper {

    public SupplierEntity toEntity(Supplier supplier) {
        var entity = new SupplierEntity();
        entity.setId(supplier.id().value());
        entity.setName(supplier.name().value());

        var contact = supplier.contactInfo();
        if (contact != null) {
            entity.setPhone(contact.phone());
            entity.setEmail(contact.email());
            entity.setContactPerson(contact.contactPerson());
        }

        var address = supplier.address();
        if (address != null) {
            entity.setStreet(address.street());
            entity.setHouseNumber(address.houseNumber());
            entity.setPostalCode(address.postalCode());
            entity.setCity(address.city());
            entity.setCountry(address.country());
        }

        var terms = supplier.paymentTerms();
        if (terms != null) {
            entity.setPaymentDueDays(terms.paymentDueDays());
            entity.setPaymentDescription(terms.description());
        }

        var rating = supplier.rating();
        if (rating != null) {
            entity.setQualityScore(rating.qualityScore());
            entity.setDeliveryScore(rating.deliveryScore());
            entity.setPriceScore(rating.priceScore());
        }

        entity.setStatus(supplier.status().name());
        entity.setCreatedAt(supplier.createdAt());
        entity.setUpdatedAt(supplier.updatedAt());

        List<QualityCertificateEmbeddable> certs = supplier.certificates().stream()
                .map(c -> new QualityCertificateEmbeddable(
                        c.certificateType(), c.issuer(), c.validFrom(), c.validUntil()))
                .collect(Collectors.toList());
        entity.setCertificates(certs);

        return entity;
    }

    public Supplier toDomain(SupplierEntity entity) {
        Address address = null;
        if (entity.getStreet() != null) {
            address = new Address(
                    entity.getStreet(), entity.getHouseNumber(),
                    entity.getPostalCode(), entity.getCity(), entity.getCountry()
            );
        }

        ContactInfo contactInfo = new ContactInfo(
                entity.getPhone(), entity.getEmail(), entity.getContactPerson()
        );

        PaymentTerms paymentTerms = null;
        if (entity.getPaymentDueDays() != null) {
            paymentTerms = new PaymentTerms(entity.getPaymentDueDays(), entity.getPaymentDescription());
        }

        SupplierRating rating = null;
        if (entity.getQualityScore() != null) {
            rating = new SupplierRating(
                    entity.getQualityScore(), entity.getDeliveryScore(), entity.getPriceScore()
            );
        }

        List<QualityCertificate> certificates = entity.getCertificates().stream()
                .map(c -> new QualityCertificate(
                        c.getCertificateType(), c.getIssuer(), c.getValidFrom(), c.getValidUntil()))
                .collect(Collectors.toList());

        return Supplier.reconstitute(
                SupplierId.of(entity.getId()),
                new SupplierName(entity.getName()),
                address,
                contactInfo,
                paymentTerms,
                certificates,
                rating,
                SupplierStatus.valueOf(entity.getStatus()),
                entity.getCreatedAt(),
                entity.getUpdatedAt()
        );
    }
}
