package org.micord.config;

import com.arangodb.ArangoDB;
import com.arangodb.ArangoDBException;
import com.arangodb.ArangoDatabase;
import org.micord.models.AqlConnectionParams;

/**
 * @author Maksim Tereshin
 */
public class ArangoDBConnection {

	public static ArangoDatabase getConnection(AqlConnectionParams params) {
		try {
			ArangoDB arangoDB = new ArangoDB.Builder()
				.host(params.getHost(), params.getPort())
				.user(params.getUsername())
				.password(params.getPassword())
				.build();

			ArangoDatabase db = arangoDB.db(params.getDatabase());

			if (!db.exists()) {
				throw new ArangoDBException("Database does not exist: " + params.getDatabase());
			}

			return db;

		} catch (ArangoDBException e) {
			throw new RuntimeException("Failed to connect to ArangoDB", e);
		}
	}

}
