package at.procon.eventhub.tachograph.camel;

import at.procon.eventhub.tachograph.dto.TachographImportRequest;
import at.procon.eventhub.tachograph.service.TachographImportExecutionService;
import org.apache.camel.builder.RouteBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class TachographImportRequestRoute extends RouteBuilder {

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

    private final TachographImportExecutionService executionService;

    public TachographImportRequestRoute(TachographImportExecutionService executionService) {
        this.executionService = executionService;
    }

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