package at.procon.eventhub.yellowfox.service;

import at.procon.eventhub.config.EventHubProperties;
import at.procon.eventhub.dto.AcquisitionStrategy;
import at.procon.eventhub.dto.EventFamily;
import at.procon.eventhub.importing.ImportChunkPlanner;
import at.procon.eventhub.importing.ImportPlanDto;
import at.procon.eventhub.importing.ImportPlanItemDto;
import at.procon.eventhub.yellowfox.dto.YellowFoxD8ImportRequest;
import java.util.List;
import org.springframework.stereotype.Service;

@Service
public class YellowFoxD8ImportPlanService {

    public static final String SOURCE_KIND = "TELEMATICS_PLATFORM";
    public static final String EXTRACTION_CODE = "YELLOWFOX_D8_BOOKING";

    private final EventHubProperties properties;
    private final ImportChunkPlanner chunkPlanner;

    public YellowFoxD8ImportPlanService(EventHubProperties properties, ImportChunkPlanner chunkPlanner) {
        this.properties = properties;
        this.chunkPlanner = chunkPlanner;
    }

    public ImportPlanDto createPlan(YellowFoxD8ImportRequest request) {
        return new ImportPlanDto(
                request.tenantKey(),
                request.mode(),
                request.acquisitionStrategy(),
                request.refreshMasterDataFirst(),
                request.importScope(),
                request.sourceGroup(),
                request.eventSource(),
                chunkPlanner.chunksFor(request, properties.getYellowFox().getDefaultChunkDays()),
                List.of(item(request.acquisitionStrategy()))
        );
    }

    private ImportPlanItemDto item(AcquisitionStrategy strategy) {
        return new ImportPlanItemDto(
                EventFamily.DRIVER_ACTIVITY,
                SOURCE_KIND,
                EXTRACTION_CODE,
                List.of("data.d8_booking", "data.vehicle", "data.driver", "data.fleet", "data.telematic_provider"),
                "VEHICLE",
                "YellowFox D8 booking card/activity events with derived ignition transitions",
                strategy
        );
    }
}
