package de.retest.recheck.persistence;

import static de.retest.recheck.XmlTransformerUtil.getXmlTransformer;
import static org.apache.commons.lang3.StringUtils.abbreviate;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

import de.retest.recheck.RecheckProperties;
import de.retest.recheck.Rehub;
import de.retest.recheck.persistence.bin.KryoPersistence;
import de.retest.recheck.persistence.xml.XmlFolderPersistence;
import de.retest.recheck.report.SuiteReplayResult;
import de.retest.recheck.report.TestReport;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CloudPersistence<T extends Persistable> implements Persistence<T> {
	private static final int MAX_REPORT_NAME_LENGTH = 50;
	private static final String SERVICE_ENDPOINT = "https://marvin.prod.cloud.retest.org/api/report";
	private final KryoPersistence<T> kryoPersistence = new KryoPersistence<>();
	private final XmlFolderPersistence<T> folderPersistence = new XmlFolderPersistence<>( getXmlTransformer() );

	public static final String RECHECK_API_KEY = "RECHECK_API_KEY";

	@Override
	public void save( final URI identifier, final T element ) throws IOException {
		kryoPersistence.save( identifier, element );

		if ( isAggregatedReport( identifier ) && element instanceof TestReport ) {
			saveToCloud( identifier, (TestReport) element );
		}
	}

	private boolean isAggregatedReport( final URI identifier ) {
		return identifier.getPath().endsWith( RecheckProperties.AGGREGATED_TEST_REPORT_FILE_NAME );
	}

	private List<String> getTestClasses( final TestReport report ) {
		return report.getSuiteReplayResults().stream() //
				.map( SuiteReplayResult::getName ) //
				.collect( Collectors.toList() );
	}

	private void saveToCloud( final URI identifier, final TestReport report ) throws IOException {
		final HttpResponse<String> uploadUrlResponse = getUploadUrl();

		final ReportUploadMetadata metadata = ReportUploadMetadata.builder() //
				.location( identifier ) //
				.uploadUrl( uploadUrlResponse.getBody() ) //
				.testClasses( getTestClasses( report ) ) //
				.build();

		if ( uploadUrlResponse.isSuccess() ) {
			uploadReport( metadata );
		}
	}

	private void uploadReport( final ReportUploadMetadata metadata ) throws IOException {
		final String reportName = metadata.getTestClasses() //
				.stream() //
				.collect( Collectors.joining( ", " ) );
		final long start = System.currentTimeMillis();

		final HttpResponse<?> uploadResponse = Unirest.put( metadata.getUploadUrl() ) //
				.header( "x-amz-meta-report-name", abbreviate( reportName, MAX_REPORT_NAME_LENGTH ) ) //
				.body( Files.readAllBytes( Paths.get( metadata.getLocation() ) ) ) //
				.asEmpty();

		if ( uploadResponse.isSuccess() ) {
			final long duration = System.currentTimeMillis() - start;
			log.info( "Successfully uploaded report to rehub in {} ms", duration );
		}
	}

	private HttpResponse<String> getUploadUrl() {
		final String token = String.format( "Bearer %s", Rehub.getAccessToken() );

		return Unirest.post( SERVICE_ENDPOINT ) //
				.header( "Authorization", token )//
				.asString();
	}

	@Override
	public T load( final URI identifier ) throws IOException {
		if ( Paths.get( identifier ).toFile().isDirectory() ) {
			return folderPersistence.load( identifier );
		}
		return kryoPersistence.load( identifier );
	}

}

@@ -1434,16 +1434,26 @@
 ort ) %7B%0A
+%09%09%09try %7B%0A%09
 %09%09%09saveT
@@ -1492,24 +1492,388 @@
  element );%0A
+%09%09%09%7D catch ( final Exception e ) %7B%0A%09%09%09%09if ( ((TestReport) element).containsChanges() ) %7B%0A%09%09%09%09%09log.error( %22The upload of the test report failed. The test report contains changes.%22 );%0A%09%09%09%09%09throw e;%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09log.warn(%0A%09%09%09%09%09%09%09%22The upload of the test report failed. The test report does not contain any changes (excluding metadata differences).%22 );%0A%09%09%09%09%7D%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A%09pri

Add exception handling for report upload