package at.procon.eventhub.camel;

import at.procon.eventhub.dto.EventHubEventDto;
import at.procon.eventhub.dto.EventHubPackageIngestRequest;
import at.procon.eventhub.dto.EventHubPackageRequest;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class ManualEventHubInputRoute extends RouteBuilder {

    @Override
    public void configure() {
        from("direct:eventhub-package-input")
                .routeId("eventhub-package-input-route")
                .process(exchange -> {
                    EventHubPackageIngestRequest request = exchange.getMessage().getBody(EventHubPackageIngestRequest.class);
                    exchange.getMessage().setHeader(EventHubHeaders.PACKAGE_INFO, request.packageInfo());
                    exchange.getMessage().setBody(request.events());
                })
                .split(body())
                    .process(exchange -> {
                        EventHubEventDto event = exchange.getMessage().getBody(EventHubEventDto.class);
                        EventHubPackageRequest packageInfo = exchange.getMessage().getHeader(EventHubHeaders.PACKAGE_INFO, EventHubPackageRequest.class);
                        exchange.getMessage().setBody(event.withPackageInfo(packageInfo));
                    })
                    .to("direct:eventhub-normalized-input")
                .end();

        from("direct:eventhub-manual-input")
                .routeId("eventhub-manual-input-route")
                .split(body())
                    .to("direct:eventhub-normalized-input")
                .end();
    }
}
