package at.procon.eventhub.yellowfox.camel;

import at.procon.eventhub.yellowfox.dto.YellowFoxD8ImportRequest;
import at.procon.eventhub.yellowfox.service.YellowFoxD8ImportExecutionService;
import org.apache.camel.builder.RouteBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class YellowFoxD8ImportRequestRoute extends RouteBuilder {

    private static final Logger log = LoggerFactory.getLogger(YellowFoxD8ImportRequestRoute.class);

    private final YellowFoxD8ImportExecutionService executionService;

    public YellowFoxD8ImportRequestRoute(YellowFoxD8ImportExecutionService executionService) {
        this.executionService = executionService;
    }

    @Override
    public void configure() {
        from("direct:yellowfox-d8-import-start")
                .routeId("yellowfox-d8-import-start-route")
                .process(exchange -> {
                    YellowFoxD8ImportRequest request = exchange.getMessage().getBody(YellowFoxD8ImportRequest.class);
                    var result = executionService.startImport(request);
                    log.info("Prepared YellowFox D8 import run importRunId={} plannedPackages={} status={}",
                            result.importRunId(), result.plannedPackageCount(), result.status());
                    exchange.getMessage().setBody(result);
                });
    }
}
