/**
 * NR BOOTH · FORM BUILDER · one run creates the whole booking form
 * ----------------------------------------------------------------
 * WHAT THIS DOES
 *   Creates the "Book your preview" Google Form with every field,
 *   validation and consent line from the campaign spec, optionally
 *   binds responses to the live register spreadsheet, and sets the
 *   confirmation message that hands people to the calendar.
 *
 * HOW TO RUN (once, ~3 minutes, in the STUDIO Google account)
 *   1. Go to script.google.com → New project.
 *   2. Delete the empty stub, paste this whole file, save.
 *   3. Fill in the two constants below (or leave blank to skip).
 *   4. Run buildBoothForm() → authorise when asked.
 *   5. Open the log (View → Logs): it prints the form's edit URL
 *      and the public URL. Put the PUBLIC url behind the landing
 *      page flow; never share the edit URL.
 *
 * AFTER RUNNING
 *   · If REGISTER_ID was blank: open the Form → Responses →
 *     link icon → choose "Select existing spreadsheet" → pick
 *     "NR Booth Referral · REGISTER".
 *   · Replace CALENDAR_URL below (or later in the Form's settings)
 *     with the final /book target once the studio schedule exists.
 */

// ── fill these in ──────────────────────────────────────────────
const REGISTER_ID  = "";   // the register spreadsheet's ID (from its URL); blank = link manually later
const CALENDAR_URL = "https://YOUR-SLUG.netlify.app/book";  // where the confirmation sends them
// ───────────────────────────────────────────────────────────────

function buildBoothForm() {
  const form = FormApp.create("Book your preview · No Regrets")
    .setDescription(
      "A few details and you are booked in. Free, no needle, and no decision asked of you: " +
      "the preview goes home with you as a print, and the timing is yours."
    )
    .setCollectEmail(false)
    .setAllowResponseEdits(false)
    .setLimitOneResponsePerUser(false);

  // 01 · Name
  form.addTextItem()
    .setTitle("Your name")
    .setRequired(true);

  // 02 · Email, validated
  const email = form.addTextItem()
    .setTitle("Email")
    .setHelpText("Your booking confirmation lands here.")
    .setRequired(true);
  email.setValidation(
    FormApp.createTextValidation()
      .requireTextIsEmail()
      .setHelpText("That does not look like an email address.")
      .build()
  );

  // 03 · Mobile: the WhatsApp and Spine key
  form.addTextItem()
    .setTitle("Mobile number (WhatsApp)")
    .setHelpText("We will WhatsApp you the draw film here. Include the country code if not UK.")
    .setRequired(true);

  // 04 · Referral code, optional, exactly 7 characters
  const code = form.addTextItem()
    .setTitle("Referral code (if a friend sent you)")
    .setHelpText("Seven characters, on the link or message your friend shared. Leave blank if you found us yourself.")
    .setRequired(false);
  code.setValidation(
    FormApp.createTextValidation()
      .requireTextMatchesPattern("^[A-HJ-NP-Z2-9]{7}$")
      .setHelpText("Codes are exactly seven characters, letters and numbers, no zeroes or ones.")
      .build()
  );

  // 05 · Required consent
  form.addCheckboxItem()
    .setTitle("The necessary bit")
    .setChoiceValues([
      "I understand my details are used to run my appointment and the £500 voucher prize draw, as described in the privacy notice."
    ])
    .setRequired(true);

  // 06 · Optional marketing opt-in
  form.addCheckboxItem()
    .setTitle("Entirely optional")
    .setChoiceValues([
      "Keep me posted about No Regrets offers and events."
    ])
    .setRequired(false);

  // Confirmation: hand them to the calendar
  form.setConfirmationMessage(
    "Done. Now pick your time: " + CALENDAR_URL +
    "\n\nSame name, please, so we can match the two."
  );

  // Bind responses to the register, if the ID was provided
  if (REGISTER_ID) {
    form.setDestination(FormApp.DestinationType.SPREADSHEET, REGISTER_ID);
    Logger.log("Responses bound to register: " + REGISTER_ID);
  } else {
    Logger.log("REGISTER_ID blank: link the spreadsheet manually via Responses → link icon.");
  }

  Logger.log("EDIT URL (keep private): " + form.getEditUrl());
  Logger.log("PUBLIC URL (this one goes live): " + form.getPublishedUrl());
}
