package at.procon.eventhub.processing.eventprocessing.profile;

import at.procon.eventhub.processing.eventprocessing.dto.RuntimeEventProcessingApiRequest;
import at.procon.eventhub.processing.eventprocessing.dto.RuntimeEventProcessingResultDto;
import at.procon.eventhub.processing.eventprocessing.partition.RuntimeEventPartitioningStrategy;
import at.procon.eventhub.processing.eventprocessing.plan.RuntimeProcessingExecutionApiRequest;
import at.procon.eventhub.processing.eventprocessing.plan.RuntimeProcessingExecutionResultDto;
import at.procon.eventhub.processing.eventprocessing.plan.DriverWorkingTimeRuntimeProcessingPlan;
import at.procon.eventhub.processing.service.RuntimeDriverWorkingTimeScopeProcessingService;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TachographDriverEsperRuntimeEventProcessingProfile implements RuntimeEventProcessingProfile {

    /**
     * Legacy compatibility key. New clients should use processingPlanKey=driver-working-time-v1
     * via /api/eventhub/runtime-processing/executions.
     */
    public static final String PROFILE_KEY = DriverWorkingTimeRuntimeProcessingPlan.LEGACY_PROFILE_ALIAS;

    private final DriverWorkingTimeRuntimeProcessingPlan plan;

    @Autowired
    public TachographDriverEsperRuntimeEventProcessingProfile(DriverWorkingTimeRuntimeProcessingPlan plan) {
        this.plan = plan;
    }

    public TachographDriverEsperRuntimeEventProcessingProfile(RuntimeDriverWorkingTimeScopeProcessingService scopeProcessingService) {
        this(new DriverWorkingTimeRuntimeProcessingPlan(scopeProcessingService));
    }

    @Override
    public String profileKey() {
        return PROFILE_KEY;
    }

    @Override
    public RuntimeEventPartitioningStrategy defaultPartitioningStrategy() {
        return plan.defaultPartitioningStrategy();
    }

    @Override
    public String displayName() {
        return "Tachograph Driver Esper Processing";
    }

    @Override
    public String description() {
        return "Compatibility adapter for legacy profileKey=" + PROFILE_KEY
                + ". New clients should use processingPlanKey=" + plan.processingPlanKey() + ". "
                + plan.description();
    }

    @Override
    public List<RuntimeEventPartitioningStrategy> supportedPartitioningStrategies() {
        return plan.supportedPartitioningStrategies();
    }

    @Override
    public Set<String> optionalParameters() {
        return plan.optionalParameters();
    }

    @Override
    public Set<String> requiredParameters() {
        return plan.requiredParameters();
    }

    @Override
    public RuntimeEventProcessingResultDto process(RuntimeEventProcessingApiRequest request) {
        RuntimeProcessingExecutionResultDto executionResult = plan.execute(new RuntimeProcessingExecutionApiRequest(
                plan.processingPlanKey(),
                request.scope(),
                request.partitioning(),
                List.of(),
                request.parameters()
        ));
        return RuntimeEventProcessingResultDto.fromExecution(executionResult, profileKey());
    }
}
