package com.google.sps.servlets;

import com.google.sps.data.VolunteeringOpportunity;
import com.google.sps.utilities.CommonUtils;
import com.google.sps.utilities.SpannerTasks;
import java.io.IOException;
import java.util.Set;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** Servlet that handles submission of the form for creating volunteering opportunities. */
@WebServlet("/volunteering-form-handler")
public class VolunteeringFormHandlerServlet extends HttpServlet {
  private static final String OPPORTUNITY_ID = "opportunity-id";
  private static final String NAME = "name";
  private static final String NUM_SPOTS_LEFT = "num-spots-left";
  private static final String REQUIRED_SKILL = "required-skill";
  private static final String HARDCODED_EVENT_ID = "0883de79-17d7-49a3-a866-dbd5135062a8";

  /**
   * Inserts volunteering opportunity with parameter values for attributes into the database if
   * opportunity ID is not given as a request parameter and updates volunteering opportunity with
   * the opportunity ID if given as a request parameter.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws IOException if Input/Output error occurs
   */
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String opportunityId = request.getParameter(OPPORTUNITY_ID);
    String name = CommonUtils.getParameter(request, NAME, /* DefaultValue= */ "");
    long numSpotsLeft =
        Long.parseLong(CommonUtils.getParameter(request, NUM_SPOTS_LEFT, /* DefaultValue= */ "0"));
    Set<String> requiredSkills = CommonUtils.getParameterValues(request, REQUIRED_SKILL);

    // If opportunityId is not passed as a parameter, perform an insert else perform an update
    if (opportunityId == null) {
      insertVolunteeringOpportunityInDB(name, numSpotsLeft, requiredSkills);
    } else {
      updateVolunteeringOpportunityInDB(opportunityId, name, numSpotsLeft, requiredSkills);
    }
    response.sendRedirect("/events-feed.html");
  }

  private static void insertVolunteeringOpportunityInDB(
      String name, long numSpotsLeft, Set<String> requiredSkills) {
    VolunteeringOpportunity opportunity =
        new VolunteeringOpportunity.Builder(HARDCODED_EVENT_ID, name, numSpotsLeft)
            .setRequiredSkills(requiredSkills)
            .build();
    // TO DO: change eventId to parameter value
    SpannerTasks.insertVolunteeringOpportunity(opportunity);
  }

  private static void updateVolunteeringOpportunityInDB(
      String opportunityId, String name, long numSpotsLeft, Set<String> requiredSkills) {
    VolunteeringOpportunity opportunity =
        new VolunteeringOpportunity.Builder(HARDCODED_EVENT_ID, name, numSpotsLeft)
            .setOpportunityId(opportunityId)
            .setRequiredSkills(requiredSkills)
            .build();
    // TO DO: change eventId to parameter value
    SpannerTasks.updateVolunteeringOpportunity(opportunity);
  }
}

@@ -648,16 +648,17 @@
 D = %22opp
+p
 ortunity
@@ -868,26 +868,16 @@
  String 
-HARDCODED_
 EVENT_ID
@@ -884,44 +884,16 @@
  = %22
-0883de79-17d7-49a3-a866-dbd5135062a8
+event-id
 %22;%0A%0A
@@ -1400,16 +1400,69 @@
 ption %7B%0A
+    String eventId = request.getParameter(EVENT_ID);%0A
     Stri
@@ -1536,27 +1536,24 @@
  name = 
-CommonUtils
+ request
 .getPara
@@ -1562,45 +1562,12 @@
 ter(
-request, NAME, /* DefaultValue= */ %22%22
+NAME
 );%0A 
@@ -1778,16 +1778,287 @@
 _SKILL);
+%0A    %0A    if (name == null) %7B%0A      response.sendError(HttpServletResponse.SC_BAD_REQUEST, %22Name not specified.%22);%0A      return;%0A    %7D else if (eventId == null) %7B%0A      response.sendError(HttpServletResponse.SC_BAD_REQUEST, %22Event ID not specified.%22);%0A      return;%0A    %7D
 %0A%0A    //
@@ -2219,16 +2219,25 @@
 ityInDB(
+eventId, 
 name, nu
@@ -2332,16 +2332,25 @@
 unityId,
+ eventId,
  name, n
@@ -2415,27 +2415,64 @@
 ect(
-%22/events-feed.html%22
+String.format(%22/event-details.html?eventId=%25s%22, eventId)
 );%0A 
@@ -2529,32 +2529,48 @@
 unityInDB(%0A     
+ String eventId,
  String name, lo
@@ -2702,34 +2702,23 @@
 Builder(
-HARDCODED_EVENT_ID
+eventId
 , name, 
@@ -2998,16 +2998,32 @@
 unityId,
+ String eventId,
  String 
@@ -3167,26 +3167,15 @@
 der(
-HARDCODED_EVENT_ID
+eventId
 , na

Change redirect URL and add error checking to servlet.