package org.micord.service;

import org.micord.exceptions.FileNotModifiedException;
import org.micord.models.*;
import org.micord.utils.ConfigLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileNotFoundException;
import java.util.*;
import java.util.concurrent.CompletableFuture;

@Service
public class ApiService {

	@Autowired
	private ConfigLoader configLoader;

	@Autowired
	private S3Service s3Service;

	@Autowired
	private SqlAqlService sqlAndAqlService;

	public CompletableFuture<String> process(String methodName, List<String> ids) throws FileNotFoundException, FileNotModifiedException {
		Optional<Requests> optionalConfig = configLoader.loadConfigIfModified(methodName);

		if (optionalConfig.isEmpty()) {
			throw new FileNotFoundException("Configuration for method " + methodName + " could not be loaded.");
		}

		Requests config = optionalConfig.get();

		sqlAndAqlService.processSqlAndAqlRequests(config, ids);

		return CompletableFuture.runAsync(() -> {
			s3Service.processS3Requests(config.getS3Requests(), ids);
		}).thenApply(v -> "Processing complete");
	}

}