package at.procon.eventhub.yellowfox.api;

import at.procon.eventhub.dto.AcquisitionStrategy;
import at.procon.eventhub.dto.ImportMode;
import at.procon.eventhub.dto.SchedulerTriggerMode;
import at.procon.eventhub.importing.masterdata.MasterDataRefreshResult;
import at.procon.eventhub.yellowfox.dto.ConfiguredYellowFoxD8ImportPlanDto;
import at.procon.eventhub.yellowfox.dto.YellowFoxD8ImportRequest;
import at.procon.eventhub.yellowfox.dto.YellowFoxD8ImportRunResultDto;
import at.procon.eventhub.yellowfox.dto.YellowFoxD8ImportTriggerResultDto;
import at.procon.eventhub.yellowfox.service.YellowFoxD8ConfiguredImportPlanService;
import at.procon.eventhub.yellowfox.service.YellowFoxD8ImportExecutionService;
import at.procon.eventhub.yellowfox.service.YellowFoxD8ImportPlanService;
import at.procon.eventhub.yellowfox.service.YellowFoxMasterDataRefreshService;
import jakarta.validation.Valid;
import java.time.OffsetDateTime;
import java.util.List;
import org.apache.camel.ProducerTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/eventhub/acquisition/yellowfox/d8")
public class YellowFoxD8ImportController {

    private final ProducerTemplate producerTemplate;
    private final YellowFoxD8ImportPlanService importPlanService;
    private final YellowFoxD8ConfiguredImportPlanService configuredImportPlanService;
    private final YellowFoxD8ImportExecutionService importExecutionService;
    private final YellowFoxMasterDataRefreshService masterDataRefreshService;

    public YellowFoxD8ImportController(
            ProducerTemplate producerTemplate,
            YellowFoxD8ImportPlanService importPlanService,
            YellowFoxD8ConfiguredImportPlanService configuredImportPlanService,
            YellowFoxD8ImportExecutionService importExecutionService,
            YellowFoxMasterDataRefreshService masterDataRefreshService
    ) {
        this.producerTemplate = producerTemplate;
        this.importPlanService = importPlanService;
        this.configuredImportPlanService = configuredImportPlanService;
        this.importExecutionService = importExecutionService;
        this.masterDataRefreshService = masterDataRefreshService;
    }

    @PostMapping("/imports/plan")
    public ResponseEntity<?> planYellowFoxD8Import(@Valid @RequestBody YellowFoxD8ImportRequest request) {
        return ResponseEntity.ok(importPlanService.createPlan(request));
    }

    @PostMapping("/imports/start")
    public ResponseEntity<YellowFoxD8ImportRunResultDto> startYellowFoxD8Import(
            @Valid @RequestBody YellowFoxD8ImportRequest request,
            @RequestParam(defaultValue = "false") boolean execute
    ) {
        YellowFoxD8ImportRunResultDto result = execute
                ? importExecutionService.startAndExecuteImport(request)
                : producerTemplate.requestBody("direct:yellowfox-d8-import-start", request, YellowFoxD8ImportRunResultDto.class);
        return ResponseEntity.accepted().body(result);
    }

    @PostMapping("/master-data/refresh")
    public ResponseEntity<MasterDataRefreshResult> refreshYellowFoxMasterData(
            @Valid @RequestBody YellowFoxD8ImportRequest request
    ) {
        return ResponseEntity.ok(masterDataRefreshService.refresh(request));
    }

    @GetMapping("/imports/configured-plans")
    public ResponseEntity<List<ConfiguredYellowFoxD8ImportPlanDto>> listConfiguredYellowFoxPlans() {
        return ResponseEntity.ok(configuredImportPlanService.listPlans());
    }

    @GetMapping("/imports/configured-plans/{planKey}")
    public ResponseEntity<ConfiguredYellowFoxD8ImportPlanDto> getConfiguredYellowFoxPlan(@PathVariable String planKey) {
        return ResponseEntity.ok(configuredImportPlanService.getPlan(planKey));
    }

    @PostMapping("/imports/configured-plans/{planKey}/start")
    public ResponseEntity<YellowFoxD8ImportTriggerResultDto> startConfiguredYellowFoxPlan(
            @PathVariable String planKey,
            @RequestParam(required = false) ImportMode mode,
            @RequestParam(required = false) AcquisitionStrategy strategy,
            @RequestParam(defaultValue = "PLAN_ONLY") SchedulerTriggerMode triggerMode
    ) {
        YellowFoxD8ImportRequest request = configuredImportPlanService.createRequest(planKey, mode, strategy);
        YellowFoxD8ImportRunResultDto result = triggerMode == SchedulerTriggerMode.EXECUTE
                ? importExecutionService.startAndExecuteImport(request)
                : importExecutionService.startImport(request);
        return ResponseEntity.accepted().body(new YellowFoxD8ImportTriggerResultDto(
                planKey,
                request.mode(),
                request.acquisitionStrategy(),
                triggerMode,
                OffsetDateTime.now(),
                result
        ));
    }

    @PostMapping("/imports/configured-plans/{planKey}/master-data/refresh")
    public ResponseEntity<MasterDataRefreshResult> refreshConfiguredYellowFoxMasterData(
            @PathVariable String planKey,
            @RequestParam(required = false) ImportMode mode,
            @RequestParam(required = false) AcquisitionStrategy strategy
    ) {
        YellowFoxD8ImportRequest request = configuredImportPlanService.createRequest(planKey, mode, strategy);
        return ResponseEntity.ok(masterDataRefreshService.refresh(request));
    }
}
