package com.atlassian.stash.internal.johnson.handler.exception;

import com.atlassian.bitbucket.Product;
import com.atlassian.stash.internal.db.Database;
import com.atlassian.stash.internal.db.UnsupportedDatabaseException;
import com.atlassian.stash.internal.johnson.AbstractHelpPathProvider;
import com.atlassian.stash.internal.johnson.handler.EventDetails;

import javax.annotation.Nonnull;

/**
 * @since 2.8.2
 */
public class UnsupportedDatabaseHandler extends AbstractHelpPathProvider implements ExceptionFailureHandler {

    @Override
    public EventDetails handle(@Nonnull Throwable t) {
        Database database = ((UnsupportedDatabaseException) t).getDatabase();

        HelpDetails help = getHelp("bitbucket.help.supportedplatforms", "our documentation",
                "https://confluence.atlassian.com/x/pQqTE");
        StringBuilder message = new StringBuilder(Product.NAME)
                .append(" is connected to ")
                .append(database.getName())
                .append(" ")
                .append(database.getVersion())
                .append(". There are known issues with this database, and as a result it is unsupported. ")
                .append(Product.NAME)
                .append(" cannot be started with this database, as it cannot function correctly.<br/><br/>")
                .append("See <a href=\"")
                .append(help.getUrl())
                .append("\">")
                .append(help.getTitle())
                .append("</a> for more details on supported databases.");

        return new EventDetails("unsupported-database", message.toString(), t);
    }
}
