// Copyright 2020 Google LLC
//
// 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
//
//     https://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 com.google.graphgeckos.dashboard.datatypes;

import com.google.cloud.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.cloud.gcp.data.datastore.core.mapping.Entity;
import org.springframework.cloud.gcp.data.datastore.core.mapping.Field;
import org.springframework.cloud.gcp.data.datastore.core.mapping.Unindexed;
import org.springframework.data.annotation.Id;
import org.springframework.lang.NonNull;

/**
 * This class encapsulates all the information gathered about a specific revision.
 * Information included: 
 *    * commit hash
 *    * timestamp
 *    * branch
 *    * information from builders:
 *          - builder name
 *          - compile logs
 *          - compilation status
 *
 * This class is used to facilitate Datastore I/O operations through Spring Cloud API,
 * and to transfer the data from the DataRepository to the REST API component.
 */
@Entity(name = "revision")
public class BuildInfo {

  @Id
  @Field(name = "commitHash")
  private String commitHash;

  @Field(name = "timestamp")
  private Timestamp timestamp;

  @Unindexed
  @Field(name = "branch")
  private String branch;

  @Unindexed
  @Field(name = "builders")
  private List<BuildBotData> builders;

  public BuildInfo() { }

  /**
   * Converts a {@link GitHubData} object to a BuildInfo object.
   * This is used for adding entries to the Google Cloud Datastore, from the Git commit
   * information received, and leaving the {@code builders} field empty, for later updates.
   */
  public BuildInfo(GitHubData creationData) {
    this.commitHash = creationData.getCommitHash();
    this.timestamp = Timestamp.of(new Date(creationData.getTimestamp()));
    this.branch = creationData.getBranch();
    this.builders = new ArrayList<>();
  }

  /**
   * Getter for the Git commit hash of the associated revision. Cannot be null.
   */
  public String getCommitHash() {
    return commitHash;
  }

  /**
   * Returns time of when the commit was pushed as a {@link com.google.cloud#Timestamp
   * Timestamp} value. Cannot be null.
   */
  public Timestamp getTimestamp() {
    return timestamp;
  }

  /**
   * Returns name of the Git branch where the revision was pushed. Cannot be null.
   */
  public String getBranch() {
    return branch;
  }

  /**
   * Returns builders which attempted the compilation of the revision. Cannot be null, and
   * neither it's elements.
   */
  public List<BuildBotData> getBuilders() {
    return builders;
  }

  /**
   * Sets the Git commit hash of the revision.
   *
   * @param commitHash the new value of the {@code commitHash} field. Should not be null.
   */
  public void setCommitHash(@NonNull String commitHash) {
    this.commitHash = commitHash;
  }

  /**
   * Sets the timestamp of the build.
   *
   * @param timestamp the new value of the {@code timestamp} field. Should not be null.
   */ 
  public void setTimestamp(@NonNull Timestamp timestamp) {
    this.timestamp = timestamp;
  }

  /**
   * Sets the branch where the revision was pushed.
   *
   * @param commitHash the new value of the {@code commitHash} field. Should not be null.
   */
  public void setBranch(@NonNull String branch) {
    this.branch = branch;
  }

  /**
   * Sets the list of builders which attempted compilation of the revision.
   *
   * @param builders the new value of the {@code builders} field. Should not be null.
   */
  public void setBuilders(@NonNull List<BuildBotData> builders) {
    this.builders = new ArrayList<>(builders);
  }

  /**
   * Adds a BuildBotData to the list of builders which attempted compilation of the revision.
   *
   * @param update the new value of the {@code update} field. Should not be null.
   */
  public void addBuilder(@NonNull BuildBotData update) {
    builders.add(update);
  }

  public boolean equals(BuildInfo other) {
    return this.commitHash == other.commitHash && this.timestamp.equals(other.timestamp) &&
           this.branch == other.branch && this.builders.equals(other.builders);
  }

}

@@ -2399,18 +2399,15 @@
   * 
-Getter for
+@return
  the
@@ -2465,32 +2465,43 @@
  be null.%0A   */%0A
+  @NonNull%0A
   public String 
@@ -2557,23 +2557,27 @@
 **%0A   * 
-R
+@r
 eturn
-s
+ the
  time of
@@ -2680,32 +2680,43 @@
  be null.%0A   */%0A
+  @NonNull%0A
   public Timesta
@@ -2773,23 +2773,27 @@
 **%0A   * 
-R
+@r
 eturn
-s
+ the
  name of
@@ -2853,32 +2853,43 @@
  be null.%0A   */%0A
+  @NonNull%0A
   public String 
@@ -2941,15 +2941,27 @@
   * 
-R
+@r
 eturn
-s
+ the list of
  bui
@@ -3006,32 +3006,37 @@
 of the revision.
+%0A   *
  Cannot be null,
@@ -3039,21 +3039,16 @@
 ull, and
-%0A   *
  neither
@@ -3061,32 +3061,43 @@
 elements.%0A   */%0A
+  @NonNull%0A
   public List%3CBu
@@ -3160,60 +3160,8 @@
 /**%0A
-   * Sets the Git commit hash of the revision.%0A   *%0A
    *
@@ -3359,51 +3359,8 @@
 /**%0A
-   * Sets the timestamp of the build.%0A   *%0A
    *
@@ -3556,65 +3556,8 @@
 /**%0A
-   * Sets the branch where the revision was pushed.%0A   *%0A
    *
@@ -3739,89 +3739,8 @@
 /**%0A
-   * Sets the list of builders which attempted compilation of the revision.%0A   *%0A
    *
@@ -3955,107 +3955,8 @@
 /**%0A
-   * Adds a BuildBotData to the list of builders which attempted compilation of the revision.%0A   *%0A
    *

update documentation