// 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;

/**
 * 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 final String commitHash;

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

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

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

  /**
   * 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;
  }

  public void addBuilder(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);
  }

}

@@ -3121,24 +3121,36 @@
 date);%0A  %7D%0A%0A
+  @Override%0A
   public boo
@@ -3165,26 +3165,212 @@
 als(
-BuildInfo other) %7B
+Object o) %7B%0A    if (o == this) %7B%0A      return true;%0A    %7D%0A    if (o == null) %7B%0A      return false;%0A    %7D%0A    if (!(o instanceof BuildInfo)) %7B%0A      return false;%0A    %7D%0A    BuildInfo other = (BuildInfo) o;
 %0A   
@@ -3377,21 +3377,16 @@
  return 
-this.
 commitHa
@@ -3387,20 +3387,24 @@
 mmitHash
- == 
+.equals(
 other.co
@@ -3411,25 +3411,21 @@
 mmitHash
+)
  && 
-this.
 timestam
@@ -3463,28 +3463,22 @@
     
-     this.branch == 
+branch.equals(
 othe
@@ -3489,17 +3489,13 @@
 anch
+)
  && 
-this.
 buil

change equals method