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

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

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 java.util.List;
import java.util.Set;
import org.junit.jupiter.api.Test;

class RuntimeEventProcessingProfileRegistryTest {

    @Test
    void exposesProfileDescriptorsForDiscovery() {
        RuntimeEventProcessingProfileRegistry registry = new RuntimeEventProcessingProfileRegistry(List.of(new TestProfile()));

        var descriptors = registry.profileDescriptors();

        assertThat(descriptors).hasSize(1);
        assertThat(descriptors.get(0).profileKey()).isEqualTo("test-profile-v1");
        assertThat(descriptors.get(0).displayName()).isEqualTo("Test Profile");
        assertThat(descriptors.get(0).defaultPartitioningStrategy()).isEqualTo(RuntimeEventPartitioningStrategy.DRIVER);
        assertThat(descriptors.get(0).supportedPartitioningStrategies()).containsExactly(RuntimeEventPartitioningStrategy.DRIVER);
        assertThat(descriptors.get(0).optionalParameters()).containsExactly("thresholdMinutes");
    }

    @Test
    void rejectsDuplicateProfileKeys() {
        assertThatThrownBy(() -> new RuntimeEventProcessingProfileRegistry(List.of(new TestProfile(), new TestProfile())))
                .isInstanceOf(IllegalStateException.class)
                .hasMessageContaining("Duplicate runtime event processing profileKey");
    }

    private static final class TestProfile implements RuntimeEventProcessingProfile {

        @Override
        public String profileKey() {
            return "test-profile-v1";
        }

        @Override
        public String displayName() {
            return "Test Profile";
        }

        @Override
        public String description() {
            return "Test profile descriptor.";
        }

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

        @Override
        public Set<String> optionalParameters() {
            return Set.of("thresholdMinutes");
        }

        @Override
        public RuntimeEventProcessingResultDto process(RuntimeEventProcessingApiRequest request) {
            throw new UnsupportedOperationException("Not needed by this test");
        }
    }
}
