package enea.lecservice.services.mocks;

import enea.lecservice.models.dto.BaseRequest;
import enea.lecservice.models.dto.DataRequest;
import enea.lecservice.services.IDataService;
import enea.lecservice.utils.FileUtils;
import lombok.extern.log4j.Log4j2;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

@Log4j2
@Service
@Profile("dev")
public class MockedDataService implements IDataService {
    @Value("${MOCK_DATA_FOLDER:}")
    protected String dataFolder;

    @Override
    public String getListOfFunctions(BaseRequest request) {
        return FileUtils.getContentAsString("mocks/getListOfFunctions.json");
    }

    @Override
    public String getData(DataRequest request) {
        log.warn("MOCK_DATA_FOLDER is {}", dataFolder);
        String fileName = dataFolder != null && !dataFolder.isBlank() ?
                String.format("%s%sgetData_%s.json", dataFolder, File.separator, request.getQueryId()) :
                String.format("mocks%sgetData_%s.json", File.separator, request.getQueryId());
        String data = FileUtils.getContentAsString(fileName, dataFolder);
        return data == null || data.isBlank() ? "{\"metadata\": [], \"result\": []}" : data;
    }

	@Override
	public String getData(DataRequest request, Integer index) {
		return FileUtils.getContentAsString("mocks/getData_1.json");
	}
	@Override
	public String clearListOfFunctions(BaseRequest request) {
		return "true";
	}
}
