/**
 *                                  Apache License
 *                            Version 2.0, January 2004
 *                         http://www.apache.org/licenses/
 *
 *    TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
 *
 *    1. Definitions.
 *
 *       "License" shall mean the terms and conditions for use, reproduction,
 *       and distribution as defined by Sections 1 through 9 of this document.
 *
 *       "Licensor" shall mean the copyright owner or entity authorized by
 *       the copyright owner that is granting the License.
 *
 *       "Legal Entity" shall mean the union of the acting entity and all
 *       other entities that control, are controlled by, or are under common
 *       control with that entity. For the purposes of this definition,
 *       "control" means (i) the power, direct or indirect, to cause the
 *       direction or management of such entity, whether by contract or
 *       otherwise, or (ii) ownership of fifty percent (50%) or more of the
 *       outstanding shares, or (iii) beneficial ownership of such entity.
 *
 *       "You" (or "Your") shall mean an individual or Legal Entity
 *       exercising permissions granted by this License.
 *
 *       "Source" form shall mean the preferred form for making modifications,
 *       including but not limited to software source code, documentation
 *       source, and configuration files.
 *
 *       "Object" form shall mean any form resulting from mechanical
 *       transformation or translation of a Source form, including but
 *       not limited to compiled object code, generated documentation,
 *       and conversions to other media types.
 *
 *       "Work" shall mean the work of authorship, whether in Source or
 *       Object form, made available under the License, as indicated by a
 *       copyright notice that is included in or attached to the work
 *       (an example is provided in the Appendix below).
 *
 *       "Derivative Works" shall mean any work, whether in Source or Object
 *       form, that is based on (or derived from) the Work and for which the
 *       editorial revisions, annotations, elaborations, or other modifications
 *       represent, as a whole, an original work of authorship. For the purposes
 *       of this License, Derivative Works shall not include works that remain
 *       separable from, or merely link (or bind by name) to the interfaces of,
 *       the Work and Derivative Works thereof.
 *
 *       "Contribution" shall mean any work of authorship, including
 *       the original version of the Work and any modifications or additions
 *       to that Work or Derivative Works thereof, that is intentionally
 *       submitted to Licensor for inclusion in the Work by the copyright owner
 *       or by an individual or Legal Entity authorized to submit on behalf of
 *       the copyright owner. For the purposes of this definition, "submitted"
 *       means any form of electronic, verbal, or written communication sent
 *       to the Licensor or its representatives, including but not limited to
 *       communication on electronic mailing lists, source code control systems,
 *       and issue tracking systems that are managed by, or on behalf of, the
 *       Licensor for the purpose of discussing and improving the Work, but
 *       excluding communication that is conspicuously marked or otherwise
 *       designated in writing by the copyright owner as "Not a Contribution."
 *
 *       "Contributor" shall mean Licensor and any individual or Legal Entity
 *       on behalf of whom a Contribution has been received by Licensor and
 *       subsequently incorporated within the Work.
 *
 *    2. Grant of Copyright License. Subject to the terms and conditions of
 *       this License, each Contributor hereby grants to You a perpetual,
 *       worldwide, non-exclusive, no-charge, royalty-free, irrevocable
 *       copyright license to reproduce, prepare Derivative Works of,
 *       publicly display, publicly perform, sublicense, and distribute the
 *       Work and such Derivative Works in Source or Object form.
 *
 *    3. Grant of Patent License. Subject to the terms and conditions of
 *       this License, each Contributor hereby grants to You a perpetual,
 *       worldwide, non-exclusive, no-charge, royalty-free, irrevocable
 *       (except as stated in this section) patent license to make, have made,
 *       use, offer to sell, sell, import, and otherwise transfer the Work,
 *       where such license applies only to those patent claims licensable
 *       by such Contributor that are necessarily infringed by their
 *       Contribution(s) alone or by combination of their Contribution(s)
 *       with the Work to which such Contribution(s) was submitted. If You
 *       institute patent litigation against any entity (including a
 *       cross-claim or counterclaim in a lawsuit) alleging that the Work
 *       or a Contribution incorporated within the Work constitutes direct
 *       or contributory patent infringement, then any patent licenses
 *       granted to You under this License for that Work shall terminate
 *       as of the date such litigation is filed.
 *
 *    4. Redistribution. You may reproduce and distribute copies of the
 *       Work or Derivative Works thereof in any medium, with or without
 *       modifications, and in Source or Object form, provided that You
 *       meet the following conditions:
 *
 *       (a) You must give any other recipients of the Work or
 *           Derivative Works a copy of this License; and
 *
 *       (b) You must cause any modified files to carry prominent notices
 *           stating that You changed the files; and
 *
 *       (c) You must retain, in the Source form of any Derivative Works
 *           that You distribute, all copyright, patent, trademark, and
 *           attribution notices from the Source form of the Work,
 *           excluding those notices that do not pertain to any part of
 *           the Derivative Works; and
 *
 *       (d) If the Work includes a "NOTICE" text file as part of its
 *           distribution, then any Derivative Works that You distribute must
 *           include a readable copy of the attribution notices contained
 *           within such NOTICE file, excluding those notices that do not
 *           pertain to any part of the Derivative Works, in at least one
 *           of the following places: within a NOTICE text file distributed
 *           as part of the Derivative Works; within the Source form or
 *           documentation, if provided along with the Derivative Works; or,
 *           within a display generated by the Derivative Works, if and
 *           wherever such third-party notices normally appear. The contents
 *           of the NOTICE file are for informational purposes only and
 *           do not modify the License. You may add Your own attribution
 *           notices within Derivative Works that You distribute, alongside
 *           or as an addendum to the NOTICE text from the Work, provided
 *           that such additional attribution notices cannot be construed
 *           as modifying the License.
 *
 *       You may add Your own copyright statement to Your modifications and
 *       may provide additional or different license terms and conditions
 *       for use, reproduction, or distribution of Your modifications, or
 *       for any such Derivative Works as a whole, provided Your use,
 *       reproduction, and distribution of the Work otherwise complies with
 *       the conditions stated in this License.
 *
 *    5. Submission of Contributions. Unless You explicitly state otherwise,
 *       any Contribution intentionally submitted for inclusion in the Work
 *       by You to the Licensor shall be under the terms and conditions of
 *       this License, without any additional terms or conditions.
 *       Notwithstanding the above, nothing herein shall supersede or modify
 *       the terms of any separate license agreement you may have executed
 *       with Licensor regarding such Contributions.
 *
 *    6. Trademarks. This License does not grant permission to use the trade
 *       names, trademarks, service marks, or product names of the Licensor,
 *       except as required for reasonable and customary use in describing the
 *       origin of the Work and reproducing the content of the NOTICE file.
 *
 *    7. Disclaimer of Warranty. Unless required by applicable law or
 *       agreed to in writing, Licensor provides the Work (and each
 *       Contributor provides its Contributions) on an "AS IS" BASIS,
 *       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 *       implied, including, without limitation, any warranties or conditions
 *       of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
 *       PARTICULAR PURPOSE. You are solely responsible for determining the
 *       appropriateness of using or redistributing the Work and assume any
 *       risks associated with Your exercise of permissions under this License.
 *
 *    8. Limitation of Liability. In no event and under no legal theory,
 *       whether in tort (including negligence), contract, or otherwise,
 *       unless required by applicable law (such as deliberate and grossly
 *       negligent acts) or agreed to in writing, shall any Contributor be
 *       liable to You for damages, including any direct, indirect, special,
 *       incidental, or consequential damages of any character arising as a
 *       result of this License or out of the use or inability to use the
 *       Work (including but not limited to damages for loss of goodwill,
 *       work stoppage, computer failure or malfunction, or any and all
 *       other commercial damages or losses), even if such Contributor
 *       has been advised of the possibility of such damages.
 *
 *    9. Accepting Warranty or Additional Liability. While redistributing
 *       the Work or Derivative Works thereof, You may choose to offer,
 *       and charge a fee for, acceptance of support, warranty, indemnity,
 *       or other liability obligations and/or rights consistent with this
 *       License. However, in accepting such obligations, You may act only
 *       on Your own behalf and on Your sole responsibility, not on behalf
 *       of any other Contributor, and only if You agree to indemnify,
 *       defend, and hold each Contributor harmless for any liability
 *       incurred by, or claims asserted against, such Contributor by reason
 *       of your accepting any such warranty or additional liability.
 *
 *    END OF TERMS AND CONDITIONS
 *
 *    APPENDIX: How to apply the Apache License to your work.
 *
 *       To apply the Apache License to your work, attach the following
 *       boilerplate notice, with the fields enclosed by brackets "{}"
 *       replaced with your own identifying information. (Don't include
 *       the brackets!)  The text should be enclosed in the appropriate
 *       comment syntax for the file format. We also recommend that a
 *       file or class name and description of purpose be included on the
 *       same "printed page" as the copyright notice for easier
 *       identification within third-party archives.
 *
 *    Copyright 2014 Edgar Espina
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.jooby.couchbase;

import com.couchbase.client.java.AsyncBucket;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.ConnectionString;
import com.couchbase.client.java.CouchbaseAsyncCluster;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.cluster.ClusterManager;
import com.couchbase.client.java.document.EntityDocument;
import com.couchbase.client.java.env.CouchbaseEnvironment;
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
import com.couchbase.client.java.repository.AsyncRepository;
import com.couchbase.client.java.repository.Repository;
import com.couchbase.client.java.repository.annotation.Field;
import com.couchbase.client.java.repository.mapping.EntityConverter;
import com.google.common.base.Splitter;
import com.google.common.collect.Sets;
import com.google.inject.Binder;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import static java.util.Objects.requireNonNull;
import org.jooby.Env;
import org.jooby.Env.ServiceKey;
import org.jooby.Jooby.Module;
import org.jooby.Session;
import org.jooby.funzy.Throwing;
import org.jooby.funzy.Try;
import org.jooby.internal.couchbase.AsyncDatastoreImpl;
import org.jooby.internal.couchbase.DatastoreImpl;
import org.jooby.internal.couchbase.IdGenerator;
import org.jooby.internal.couchbase.JacksonMapper;
import org.jooby.internal.couchbase.SetConverterHack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.Observable;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * <h1>couchbase</h1>
 * <p>
 * <a href="http://www.couchbase.com">Couchbase</a> is a NoSQL document database with a distributed
 * architecture for performance, scalability, and availability. It enables developers to build
 * applications easier and faster by leveraging the power of SQL with the flexibility of JSON.
 * </p>
 * <p>
 * This module provides <a href="http://www.couchbase.com">couchbase</a> access via
 * <a href="https://github.com/couchbase/couchbase-java-client">Java SDK</a>
 * </p>
 *
 * <h2>exports</h2>
 * <ul>
 * <li>{@link CouchbaseEnvironment}</li>
 * <li>{@link CouchbaseCluster}</li>
 * <li>{@link AsyncBucket}</li>
 * <li>{@link Bucket}</li>
 * <li>{@link AsyncDatastore}</li>
 * <li>{@link Datastore}</li>
 * <li>{@link AsyncRepository}</li>
 * <li>{@link Repository}</li>
 * <li>Optionally a couchbase {@link Session.Store}</li>
 * </ul>
 *
 * <h2>usage</h2>
 * <p>
 * Via couchbase connection string:
 * </p>
 *
 * <pre>{@code
 * {
 *   use(new Couchbase("couchbase://locahost/beers"));
 *
 *   get("/", req -> {
 *     Bucket beers = req.require(Bucket.class);
 *     // do with beer bucket
 *   });
 * }
 * }</pre>
 *
 * <p>
 * Via property with a couchbase connection string:
 * </p>
 *
 * <pre>{@code
 * {
 *   use(new Couchbase("db"));
 *
 *   get("/", req -> {
 *     Bucket beers = req.require(Bucket.class);
 *     // do with beer bucket
 *   });
 * }
 * }</pre>
 *
 * <p>
 * The <code>db</code> property is defined in the <code>application.conf</code> file as a couchbase
 * connection string.
 * </p>
 *
 * <h2>create, read, update and delete</h2>
 * <p>
 * Jooby provides a more flexible, easy to use and powerful CRUD operations via
 * {@link AsyncDatastore}/{@link Datastore} objects.
 * </p>
 *
 * <pre>{@code
 *
 * import org.jooby.couchbase.Couchbase;
 * import org.jooby.couchbase.N1Q;
 * ...
 * {
 *   use(new Couchbase("couchbase://localhost/beers"));
 *
 *   use("/api/beers")
 *     .get(req -> {
 *       Datastore ds = req.require(Datastore.class);
 *       return ds.query(N1Q.from(Beer.class));
 *     })
 *     .post(req -> {
 *       Datastore ds = req.require(Datastore.class);
 *       Beer beer = req.body().to(Beer.class);
 *       return ds.upsert(beer);
 *     })
 *     .get(":id", req -> {
 *       Datastore ds = req.require(Datastore.class);
 *       return ds.get(Beer.class, req.param("id").value());
 *     })
 *     .delete(":id", req -> {
 *       Datastore ds = req.require(Datastore.class);
 *       return ds.delete(Beer.class, req.param("id").value());
 *     });
 * }
 * }</pre>
 *
 * <p>
 * As you can see benefits over {@link AsyncRepository}/{@link Repository} are clear: you don't have
 * to deal with {@link EntityDocument} just send or retrieve POJOs.
 * </p>
 * <p>
 * Another good reason is that {@link AsyncDatastore}/{@link Datastore} supports query operations
 * with POJOs too.
 * </p>
 *
 * <h3>design and implementation choices</h3>
 * <p>
 * The {@link AsyncDatastore}/{@link Datastore} simplifies a lot the integration between Couchbase
 * and POJOs. This section describes how IDs are persisted and how mapping works.
 * </p>
 *
 * <p>
 * A document persisted by an {@link AsyncDatastore}/{@link Datastore} looks like:
 * </p>
 *
 * <pre>
 * {
 *   "model.Beer::1": {
 *     "name": "IPA",
 *     ...
 *     "id": 1,
 *     "_class": "model.Beer"
 *   }
 * }
 * </pre>
 *
 * <p>
 * The couchbase document ID contains the fully qualified name of the class, plus <code>::</code>
 * plus the entity/business ID: <code>mode.Beer::1</code>.
 * </p>
 * <p>
 * The business ID is part of the document, here the business ID is: <code>id:1</code>. The business
 * ID is required while creating POJO from couchbase queries.
 * </p>
 * <p>
 * Finally, a <code>_class</code> attribute is also part of the document. It contains the fully
 * qualified name of the class and its required while creating POJO from couchbase queries.
 * </p>
 *
 * <h3>mapping pojos</h3>
 * <p>
 * Mapping between document/POJOs is done internally with a custom {@link EntityConverter}. The
 * {@link EntityConverter} uses an internal copy of <code>ObjectMapper</code> object from
 * <code>Jackson</code>. So in <strong>theory</strong> anything that can be handle by
 * <code>Jackson</code> will work.
 * </p>
 *
 * <p>
 * In order to work with a POJO, you must defined an ID. There are two options:
 * </p>
 *
 * <p>
 * 1. Add an <code>id</code> field to your POJO:
 * </p>
 *
 * <pre>{@code
 * public class Beer {
 *   private String id;
 * }
 * }</pre>
 *
 * <p>
 * 2. Use a business name (not necessarily id) and add <code>Id</code> annotation:
 * </p>
 *
 * <pre>{@code
 *
 * import import com.couchbase.client.java.repository.annotation.Id;
 *
 * public class Beer {
 *   &#64;Id
 *   private String beerId;
 * }
 * }</pre>
 *
 * <p>
 * Auto-increment IDs are supported via {@link GeneratedValue}:
 * </p>
 *
 * <pre>{@code
 *
 * public class Beer {
 *   private Long id;
 * }
 * }</pre>
 *
 * <p>
 * Auto-increment IDs are generated using {@link Bucket#counter(String, long, long)} function
 * and they must be <code>Long</code>. We use the POJO fully qualified name as counter ID.
 * </p>
 *
 * <p>
 * Any other field will be mapped too, you don't need to annotate an attribute with {@link Field}.
 * If you don't want to persist an attribute, just ad the <code>transient</code> Java modifier:
 * </p>
 *
 * <pre>{@code
 * public class Beer {
 *   private String id;
 *
 *   private transient ignored;
 * }
 * }</pre>
 *
 * <p>
 * Keep in mind that if you annotated your POJO with <code>Jackson</code> annotations they will
 * be ignored... because we use an internal copy of <code>Jackson</code> that comes with
 * <code>Java Couchbase SDK</code>
 * </p>
 *
 * <h2>reactive usage</h2>
 * <p>
 * Couchbase SDK allows two programming model: <code>blocking</code> and <code>reactive</code>. We
 * already see how to use the blocking API, now is time to see how to use the <code>reactive</code>
 * API:
 * </p>
 *
 * <pre>{@code
 * {
 *   use(new Couchbase("couchbase://localhost/beers"));
 *
 *   get("/", req -> {
 *     AsyncBucket bucket = req.require(AsyncBucket.class);
 *     // do with async bucket ;)
 *   });
 * }
 * }</pre>
 *
 * <p>
 * Now, what to do with Observables? Do we have to block? Not necessarily if we use the
 * <code>Rx</code> module:
 * </p>
 *
 * <pre>{@code
 *
 * import org.jooby.rx.Rx;
 *
 * {
 *   // handle observable route responses
 *   use(new Rx());
 *
 *   use(new Couchbase("couchbase://localhost/beers"));
 *
 *   get("/api/beer/:id", req -> {
 *     AsyncDatastore ds = req.require(AsyncDatastore.class);
 *     String id = req.param("id").value();
 *     Observable<Beer> beer = ds.get(Beer.class, id);
 *     return beer;
 *   });
 * }
 * }</pre>
 *
 * <p>
 * The <code>Rx</code> module deal with observables so you can safely return {@link Observable} from
 * routes (Jooby rocks!).
 * </p>
 *
 * <h2>multiple buckets</h2>
 * <p>
 * If for any reason your application requires more than 1 bucket... then:
 * </p>
 *
 * <pre>{@code
 * {
 *   use(new Couchbase("couchbase://localhost/beers")
 *     .buckets("extra-bucket"));
 *
 *   get("/", req -> {
 *     Bucket bucket = req.require("beers", Bucket.class);
 *     Bucket extra = req.require("extra-bucket", Bucket.class);
 *   });
 * }
 * }</pre>
 *
 * <p>
 * Easy, right? Same principle apply for Async* objects
 * </p>
 *
 * <h2>multiple clusters</h2>
 * <p>
 * Again, if for any reason your application requires multiple clusters... then:
 * </p>
 *
 * <pre>{@code
 * {
 *   CouchbaseEnvironment env = ...;
 *
 *   use(new Couchbase("couchbase://192.168.56.1")
 *      .environment(env));
 *
 *   use(new Couchbase("couchbase://192.168.57.10")
 *      .environment(env));
 * }
 * }</pre>
 *
 * <p>
 * You must shared the {@link CouchbaseEnvironment} as documented <a href=
 * "http://developer.couchbase.com/documentation/server/4.0/sdks/java-2.2/managing-connections.html#story-h2-4">here</a>.
 * </p>
 *
 * <h2>options</h2>
 *
 * <h3>bucket password</h3>
 * <p>
 * You can set a global bucket password via: <code>couchbase.bucket.password</code> property, or
 * local bucket password (per bucket) via <code>couchbase.bucket.[name].password</code> property.
 * </p>
 *
 * <h3>environment configuration</h3>
 * <p>
 * Environment configuration is available via: <code>couchbase.env</code> namespace, here is an
 * example on how to setup <code>kvEndpoints</code>:
 * </p>
 *
 * <pre>
 * couchbase.env.kvEndpoints = 3
 * </pre>
 *
 * <h3>cluster manager</h3>
 * <p>
 * A {@link ClusterManager} service is available is you set an cluster username and password:
 * </p>
 *
 * <pre>
 * couchbase.cluster.username = foo
 * couchbase.cluster.password = bar
 * </pre>
 *
 * @author edgar
 * @since 1.0.0.CR7
 */
public class Couchbase implements Module {

  // FIXME: converter hack
  static final JacksonMapper CONVERTER = new JacksonMapper();

  /** The logging system. */
  private final Logger log = LoggerFactory.getLogger(getClass());

  private Function<Config, CouchbaseEnvironment> env = c -> DefaultCouchbaseEnvironment.create();

  private final String db;

  private final Set<String> buckets = new LinkedHashSet<>();

  private Optional<String> sessionBucket = Optional.empty();

  /**
   * Creates a new {@link Couchbase} module.
   *
   * <pre>{@code
   * {
   *   use(new Couchbase("couchbase://localhost/bucket"));
   * }
   * }</pre>
   *
   * Or add a <code>db</code> property to your <code>.conf</code> file:
   *
   * <pre>{@code
   * {
   *   use(new Couchbase("db"));
   * }
   * }</pre>
   *
   * @param db A couchbase connection string or a property with a connection string.
   */
  public Couchbase(final String db) {
    this.db = requireNonNull(db, "Connection String/Database key required.");
  }

  /**
   * Creates a new {@link Couchbase} module. You must add a <code>db</code> property to your
   * <code>.conf</code> file.
   */
  public Couchbase() {
    this("db");
  }

  /**
   * Set a shared {@link CouchbaseEnvironment}. The environment will shutdown automatically.
   *
   * @param env Environment provider.
   * @return This module.
   */
  public Couchbase environment(final Function<Config, CouchbaseEnvironment> env) {
    this.env = env;
    return this;
  }

  /**
   * Set a shared {@link CouchbaseEnvironment}. The environment will shutdown automatically.
   *
   * @param env Environment provider.
   * @return This module.
   */
  public Couchbase environment(final java.util.function.Supplier<CouchbaseEnvironment> env) {
    return environment(c -> env.get());
  }

  /**
   * Set a shared {@link CouchbaseEnvironment}. The environment will shutdown automatically.
   *
   * @param env Environment.
   * @return This module.
   */
  public Couchbase environment(final CouchbaseEnvironment env) {
    return environment(() -> env);
  }

  /**
   * List of buckets to open on startup.
   *
   * @param names Bucket names.
   * @return This module.
   */
  public Couchbase buckets(final String... names) {
    buckets.addAll(Arrays.asList(names));
    return this;
  }

  @SuppressWarnings({"unchecked", "rawtypes"})
  @Override
  public void configure(final Env env, final Config conf, final Binder binder) {
    String cstr = db.startsWith(ConnectionString.DEFAULT_SCHEME) ? db : conf.getString(db);
    String defbucket = defbucket(cstr);
    System.setProperty(N1Q.COUCHBASE_DEFBUCKET, defbucket);
    String dbname = cstr.equals(db) ? defbucket : db;

    // dump couchbase.env.* as system properties
    if (conf.hasPath("couchbase.env")) {
      conf.getConfig("couchbase.env").entrySet().forEach(e -> System.setProperty("com.couchbase." + e.getKey(), e.getValue().unwrapped().toString()));
    }

    log.debug("Starting {}", cstr);

    ServiceKey serviceKey = env.serviceKey();
    Throwing.Function3<Class, String, Object, Void> bind = (type, name, value) -> {
      serviceKey.generate(type, name, k -> binder.bind(k).toInstance(value));
      return null;
    };

    CouchbaseEnvironment cenv = this.env.apply(conf);

    // FIXME: ConnectionString doesn't work with bucket in the url
    String cstrworkaround = cstr.replace("/" + defbucket, "");
    CouchbaseCluster cluster = CouchbaseCluster.fromConnectionString(cenv, cstrworkaround);
    serviceKey.generate(CouchbaseCluster.class, dbname, k -> binder.bind(k).toInstance(cluster));

    // start cluster manager?
    if (conf.hasPath("couchbase.cluster.username")) {
      ClusterManager clusterManager = cluster
          .clusterManager(
              conf.getString("couchbase.cluster.username"),
              conf.getString("couchbase.cluster.password"));
      bind.apply(ClusterManager.class, dbname, clusterManager);
    }

    // configure buckets, repositories and datastores
    Set<String> buckets = Sets.newHashSet(defbucket);
    buckets.addAll(this.buckets);

    Function<String, String> password = name -> Stream.of(
        "couchbase.bucket." + name + ".password",
        "couchbase.bucket.password")
        .filter(conf::hasPath)
        .map(conf::getString)
        .findFirst()
        .orElse(null);
    buckets.forEach(name -> {
      Bucket bucket = cluster.openBucket(name, password.apply(name));
      log.debug("  bucket opened: {}", name);

      bind.apply(Bucket.class, name, bucket);
      AsyncBucket async = bucket.async();
      bind.apply(AsyncBucket.class, name, async);

      Repository repo = bucket.repository();
      AsyncRepository asyncrepo = repo.async();

      // FIXME: converter hack
      SetConverterHack.forceConverter(asyncrepo, CONVERTER);

      bind.apply(Repository.class, name, repo);
      bind.apply(AsyncRepository.class, name, asyncrepo);

      AsyncDatastoreImpl asyncds = new AsyncDatastoreImpl(async, asyncrepo, idGen(bucket),
          CONVERTER);
      bind.apply(AsyncDatastore.class, name, asyncds);
      bind.apply(Datastore.class, name, new DatastoreImpl(asyncds));

      buckets.add(name);
    });

    // special binding for session bucket: either the default bucket or custom
    this.sessionBucket.ifPresent(buckets::add);
    String session = this.sessionBucket.orElse(defbucket);
    bind.apply(Bucket.class, "session", cluster.openBucket(session, password.apply(session)));

    env.onStop(r -> {
      buckets.forEach(n -> Try.apply(() -> r.require(n, Bucket.class).close())
          .onFailure(x -> log.debug("bucket {} close operation resulted in exception", n, x))
          .orElse(false));
      Try.run(cluster::disconnect)
          .onFailure(x -> log.debug("disconnect operation resulted in exception", x));

      Try.run(cenv::shutdown)
          .onFailure(x -> log.debug("environment shutdown resulted in exception", x));
    });
  }

  /**
   * Use a custom bucket for HTTP Session, see {@link CouchbaseSessionStore}.
   *
   * @param bucket Bucket to use for HTTP Session.
   * @return This module.
   */
  public Couchbase sessionBucket(final String bucket) {
    this.sessionBucket = Optional.of(requireNonNull(bucket, "Session bucket required."));
    return this;
  }

  @Override
  public Config config() {
    return ConfigFactory.parseResources(getClass(), "couchbase.conf");
  }

  private static String defbucket(final String cstr) {
    List<String> segments = Splitter.on("/").trimResults().omitEmptyStrings()
        .splitToList(cstr.substring(0, Math.max(cstr.indexOf("?"), cstr.length()))
            .replace(ConnectionString.DEFAULT_SCHEME, ""));
    return segments.size() == 2 ? segments.get(1) : CouchbaseAsyncCluster.DEFAULT_BUCKET;
  }

  private static Function<Object, Object> idGen(final Bucket bucket) {
    return entity -> IdGenerator.getOrGenId(entity,
        () -> bucket.counter(entity.getClass().getName(), 1, 1).content());
  }

}
