
package org.jboss.pnc.coordinator.maintenance;

import java.util.HashSet;
import java.util.Set;
import org.jboss.pnc.enums.ArtifactQuality;
import org.jboss.pnc.enums.ResultStatus;
import org.jboss.pnc.model.Artifact;
import org.jboss.pnc.model.BuildConfigSetRecord;
import org.jboss.pnc.model.BuildRecord;
import org.jboss.pnc.spi.coordinator.Result;
import org.jboss.pnc.spi.datastore.repositories.ArtifactRepository;
import org.jboss.pnc.spi.datastore.repositories.BuildConfigSetRecordRepository;
import org.jboss.pnc.spi.datastore.repositories.BuildRecordRepository;
import org.jboss.pnc.spi.exception.ValidationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.inject.Inject;

/**
 * Bean providing an interface to delete temporary builds
 *
 * @author Jakub Bartecek
 */
@Stateless
public class TemporaryBuildsCleaner {
    private final Logger log = LoggerFactory.getLogger(TemporaryBuildsCleaner.class);

    private BuildRecordRepository buildRecordRepository;

    private BuildConfigSetRecordRepository buildConfigSetRecordRepository;

    private ArtifactRepository artifactRepository;

    private RemoteBuildsCleaner remoteBuildsCleaner;

    @Deprecated
    public TemporaryBuildsCleaner() {
    }

    @Inject
    public TemporaryBuildsCleaner(
            BuildRecordRepository buildRecordRepository,
            BuildConfigSetRecordRepository buildConfigSetRecordRepository,
            ArtifactRepository artifactRepository,
            RemoteBuildsCleaner remoteBuildsCleaner) {
        this.buildRecordRepository = buildRecordRepository;
        this.buildConfigSetRecordRepository = buildConfigSetRecordRepository;
        this.artifactRepository = artifactRepository;
        this.remoteBuildsCleaner = remoteBuildsCleaner;
    }

    /**
     * Deletes a temporary build and artifacts created during the build or orphan dependencies used
     *
     * @param buildRecordId BuildRecord to be deleted
     * @param authToken
     * @return true if success
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public Result deleteTemporaryBuild(Integer buildRecordId, String authToken) throws ValidationException {
        BuildRecord buildRecord = buildRecordRepository.findByIdFetchAllProperties(buildRecordId);

        if (buildRecord == null) {
            throw new ValidationException(
                    "Cannot delete temporary build with id " + buildRecordId + " as no build with this id exists");
        }

        if (!buildRecord.isTemporaryBuild()) {
            throw new ValidationException("Only deletion of the temporary builds is allowed");
        }
        log.info(
                "Starting deletion of a temporary build " + buildRecord + "; Built artifacts: "
                        + buildRecord.getBuiltArtifacts() + "; Dependencies: " + buildRecord.getDependencies());

        Result result = remoteBuildsCleaner.deleteRemoteBuilds(buildRecord, authToken);
        if (!result.isSuccess()) {
            log.error("Failed to delete remote temporary builds for BR.id:{}.", buildRecord.getId());
            return new Result(
                    buildRecordId.toString(),
                    ResultStatus.FAILED,
                    "Failed to delete remote temporary builds.");
        }

        removeBuiltArtifacts(buildRecord);

        deleteDependencies(buildRecord);

        buildRecordRepository.delete(buildRecord.getId());
        log.info("Deletion of the temporary build {} finished successfully.", buildRecord);
        return new Result(buildRecordId.toString(), ResultStatus.SUCCESS);
    }

    private void deleteDependencies(BuildRecord buildRecord) {
        for (Artifact artifact : buildRecord.getDependencies()) {
            // check if the BR in which this artifact was produced has been already deleted
            if (ArtifactQuality.DELETED.equals(artifact.getArtifactQuality())) {
                if (artifact.getDependantBuildRecords().size() == 0) {
                    artifactRepository.delete(artifact.getId());
                }
            }
        }
    }

    private void deleteArtifact(Artifact artifact) {
        if (artifact.getDependantBuildRecords().size() > 0) {
            log.info("Marking temporary artifact as DELETED: " + artifact.getDescriptiveString());
            artifact.setArtifactQuality(ArtifactQuality.DELETED);
            artifactRepository.save(artifact);
        } else {
            log.info("Deleting temporary artifact: " + artifact.getDescriptiveString());
            artifactRepository.delete(artifact.getId());
        }
    }

    /**
     * Deletes a BuildConfigSetRecord and BuildRecords produced in the build
     * 
     * @param buildConfigSetRecordId BuildConfigSetRecord to be deleted
     * @param authToken
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public Result deleteTemporaryBuildConfigSetRecord(Integer buildConfigSetRecordId, String authToken)
            throws ValidationException {
        BuildConfigSetRecord buildConfigSetRecord = buildConfigSetRecordRepository.queryById(buildConfigSetRecordId);

        if (buildConfigSetRecord == null) {
            throw new ValidationException(
                    "Cannot delete temporary BuildConfigSetRecord with id " + buildConfigSetRecordId
                            + " as no BuildConfigSetRecord with this id exists");
        }

        if (!buildConfigSetRecord.isTemporaryBuild()) {
            throw new ValidationException("Only deletion of the temporary builds is allowed");
        }
        log.info("Starting deletion of a temporary build record set " + buildConfigSetRecord);

        for (BuildRecord br : buildConfigSetRecord.getBuildRecords()) {
            Result result = deleteTemporaryBuild(br.getId(), authToken);
            if (!result.isSuccess()) {
                return result;
            }
        }
        buildConfigSetRecordRepository.delete(buildConfigSetRecord.getId());

        log.info("Deletion of a temporary build record set {} finished successfully.", buildConfigSetRecord);
        return new Result(buildConfigSetRecordId.toString(), ResultStatus.SUCCESS);
    }

    private void removeBuiltArtifacts(BuildRecord buildRecord) {
        Set<Artifact> toDelete = new HashSet<>(buildRecord.getBuiltArtifacts());
        for (Artifact artifact : toDelete) {
            log.debug(
                    String.format(
                            "Deleting relation BR-Artifact. BR=%s, artifact=%s",
                            buildRecord,
                            artifact.getDescriptiveString()));

            if (!artifact.getDistributedInProductMilestones().isEmpty()) {
                log.error(
                        "Temporary artifact was distributed in milestone! Artifact: " + artifact.toString()
                                + "\n Milestones: " + artifact.getDistributedInProductMilestones().toString());
                continue;
            }

            artifact.setBuildRecord(null);
            deleteArtifact(artifact);
        }
    }
}
