// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// 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 google.registry.model.domain;

import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;
import google.registry.model.BackupGroupRoot;
import google.registry.model.Buildable;
import google.registry.model.reporting.HistoryEntry;
import google.registry.model.reporting.HistoryEntry.Type;

/**
 * An entity representing a token distributed to eligible LRP registrants.
 */
@Entity
public class LrpToken extends BackupGroupRoot implements Buildable {

  /**
   * The token's assignee (a unique identifier).
   */
  @Id
  String assignee;

  /**
   * The secret token assigned to a registrant for the purposes of LRP registration.
   */
  @Index
  String token;

  /**
   * The key of the history entry for which the token was used. Given LRP is a domain application
   * phase, this should always be a {@link Type#DOMAIN_APPLICATION_CREATE}.
   */
  Key<HistoryEntry> redemptionHistoryEntry;
  
  public String getToken() {
    return token;
  }

  public String getAssignee() {
    return assignee;
  }

  public Key<HistoryEntry> getRedemptionHistoryEntry() {
    return redemptionHistoryEntry;
  }

  public boolean isRedeemed() {
    return redemptionHistoryEntry != null;
  }

  @Override
  public Builder asBuilder() {
    return new Builder(clone(this));
  }

  /** A builder for constructing {@link LrpToken} objects, since they are immutable. */
  public static class Builder extends Buildable.Builder<LrpToken> {
    public Builder() {}

    private Builder(LrpToken instance) {
      super(instance);
    }
    
    public Builder setAssignee(String assignee) {
      getInstance().assignee = assignee;
      return this;
    }

    public Builder setToken(String token) {
      getInstance().token = checkArgumentNotNull(token);
      return this;
    }
    
    public Builder setRedemptionHistoryEntry(Key<HistoryEntry> redemptionHistoryEntry) {
      getInstance().redemptionHistoryEntry = checkArgumentNotNull(redemptionHistoryEntry);
      return this;
    }
  }
}

@@ -659,16 +659,93 @@
 omain;%0A%0A
+import static google.registry.util.CollectionUtils.nullToEmptyImmutableCopy;%0A
 import s
@@ -812,16 +812,16 @@
 otNull;%0A
-
 %0Aimport 
@@ -1196,16 +1196,39 @@
 .Type;%0A%0A
+import java.util.Set;%0A%0A
 /**%0A * A
@@ -1590,16 +1590,109 @@
 token;%0A%0A
+  /**%0A   * A list of TLDs for which this LRP token is valid.%0A   */%0A  Set%3CString%3E validTlds;%0A%0A
   /**%0A  
@@ -2203,16 +2203,107 @@
 l;%0A  %7D%0A%0A
+  public Set%3CString%3E getValidTlds() %7B%0A    return nullToEmptyImmutableCopy(validTlds);%0A  %7D%0A%0A
   @Overr
@@ -2968,24 +2968,24 @@
 oryEntry) %7B%0A
-
       getIns
@@ -3087,15 +3087,141 @@
 s;%0A    %7D
+%0A%0A    public Builder setValidTlds(Set%3CString%3E validTlds) %7B%0A      getInstance().validTlds = validTlds;%0A      return this;%0A    %7D
 %0A  %7D%0A%7D%0A

Add set of valid TLDs to LrpToken