package de.effigenix.infrastructure.production.event;

import de.effigenix.domain.production.event.BatchCompleted;
import de.effigenix.domain.production.event.ConsumptionRecorded;
import de.effigenix.infrastructure.integration.event.BatchCompletedIntegrationEvent;
import de.effigenix.infrastructure.integration.event.ConsumptionRecordedIntegrationEvent;
import de.effigenix.shared.event.DomainEvent;
import de.effigenix.shared.event.DomainEventPublisher;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

@Component
public class ProductionDomainEventPublisher implements DomainEventPublisher {

    private final ApplicationEventPublisher publisher;

    public ProductionDomainEventPublisher(ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }

    @Override
    public void publish(DomainEvent event) {
        switch (event) {
            case BatchCompleted bc -> publisher.publishEvent(
                    new BatchCompletedIntegrationEvent(
                            bc.batchId().value(),
                            bc.batchNumber().value(),
                            bc.articleId(),
                            bc.actualQuantity().amount().toPlainString(),
                            bc.actualQuantity().uom().name(),
                            bc.bestBeforeDate().toString()
                    )
            );
            case ConsumptionRecorded cr -> publisher.publishEvent(
                    new ConsumptionRecordedIntegrationEvent(
                            cr.inputBatchId(),
                            cr.articleId(),
                            cr.quantity().amount().toPlainString(),
                            cr.quantity().uom().name()
                    )
            );
            default -> { }
        }
    }
}
