package com.google.sps.utilities;

import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.Mutation;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.sps.data.User;
import java.util.ArrayList;
import java.util.List;

public class DatabaseWrapper {
  private String instanceID;
  private String databaseID;
  private static final String USER_TABLE = "Users";

  public DatabaseWrapper(String instanceID, String databaseID) {
    this.instanceID = instanceID;
    this.databaseID = databaseID;
  }
  
  /** Given a user, insert a row with all available fields into the DB
   *  @param user the user to be inserted; user's ID field should not exist in DB
  */
  public void insertUser(User user) {
    SpannerOptions options = SpannerOptions.newBuilder().build();
    Spanner spanner = options.getService();
    DatabaseId db = DatabaseId.of(options.getProjectId(), instanceID, databaseID);
    DatabaseClient dbClient = spanner.getDatabaseClient(db);

    List<Mutation> mutations = newMutationFromUser(user, false);
    dbClient.write(mutations);
    spanner.close();
  }

  /** Given a user, insert a row with all available fields into the DB
   *  @param user the user to be updated; user's ID field should already exist in DB
  */
  public void updateUser(User user) {
    // Given a user, update its corresponding row's new fields in DB
    SpannerOptions options = SpannerOptions.newBuilder().build();
    Spanner spanner = options.getService();
    DatabaseId db = DatabaseId.of(options.getProjectId(), instanceID, databaseID);
    DatabaseClient dbClient = spanner.getDatabaseClient(db);

    List<Mutation> mutations = newMutationFromUser(user, true);
    dbClient.write(mutations);
    spanner.close();
  }

  private static List<Mutation> newMutationFromUser(User user, boolean update) {
    List<Mutation> mutations = new ArrayList<>();
    Mutation.WriteBuilder builder =
        update ? Mutation.newUpdateBuilder(USER_TABLE) : Mutation.newInsertBuilder(USER_TABLE);
    builder
        .set("UserID")
        .to(user.getUserId())
        .set("Name")
        .to(user.getName())
        .set("Email")
        .to(user.getEmail())
        .set("Interests")
        .toStringArray(user.getInterests())
        .set("Skills")
        .toStringArray(user.getSkills())
        .set("EventsHosting")
        .toInt64Array(user.getEventsHostingIDs())
        .set("EventsParticipating")
        .toInt64Array(user.getEventsParticipatingIDs())
        .set("EventsVolunteering")
        .toInt64Array(user.getEventsVolunteeringIDs());
    mutations.add(builder.build());
    return mutations;
  }
}

@@ -619,16 +619,19 @@
   %7D%0A
-  
 %0A  /**
+%0A   *
  Giv
@@ -688,33 +688,37 @@
 into the DB%0A   *
- 
+%0A   *
  @param user the
@@ -770,32 +770,33 @@
 not exist in DB%0A
+ 
   */%0A  public vo
@@ -1204,16 +1204,21 @@
 %7D%0A%0A  /**
+%0A   *
  Given a
@@ -1271,24 +1271,29 @@
 into the DB%0A
+   *%0A
    *  @param
@@ -1365,16 +1365,17 @@
 t in DB%0A
+ 
   */%0A  p

Fix style