/* GCCReady — central runtime configuration.
 * EDIT THIS FILE TO GO LIVE.
 *
 * This is the only place to update for launch. All 6 form-submission flows and
 * the WhatsApp FAB read these globals. Loaded first (before any component or
 * page) in index.html, so every later script can rely on window.GR_CONFIG.
 */

window.GR_CONFIG = {
  // ▼▼▼ EDIT THESE 4 VALUES TO GO LIVE ▼▼▼

  /** Google Apps Script web-app URL. Paste the deployed URL from apps_script.gs.
   *  While empty, all forms gracefully fall back to mailto: CONTACT_EMAIL. */
  SHEETS_ENDPOINT: '',

  /** WhatsApp business number (international format, no spaces, no +). */
  WHATSAPP_NUM: '919667744073',

  /** Default contact email shown on Contact + Press + footer. */
  CONTACT_EMAIL: 'info@gccready.com',

  /** Site origin used in Schema.org canonical URLs + share links. */
  SITE_ORIGIN: 'https://gccready.com',

  // ▲▲▲ END OF FIELDS YOU NEED TO EDIT ▲▲▲

  // Routed inboxes — currently all routed to info@ until role-based aliases are set up.
  // To split later: change each line to mentors@, hire@, campuses@, etc.
  INBOXES: {
    student:    'info@gccready.com',
    parent:     'info@gccready.com',
    mentor:     'info@gccready.com',
    practice:   'info@gccready.com',
    college:    'info@gccready.com',
    employer:   'info@gccready.com',
    minigcc:    'info@gccready.com',
    press:      'info@gccready.com',
    careers:    'info@gccready.com',
    privacy:    'info@gccready.com',
    refunds:    'info@gccready.com',
  },

  // Analytics — fill in when ready. Empty = no-op.
  GA4_ID:        '',  // e.g. 'G-XXXXXXXXXX'
  PLAUSIBLE_DOMAIN: '',  // e.g. 'gccready.com'

  // Feature flags
  SHOW_COHORT_STRIP: true,
  SHOW_NEWSLETTER:   true,
  ENABLE_EXIT_INTENT: true,
};

// Convenience accessors so existing code reads cleanly
window.GR_SHEETS_ENDPOINT = window.GR_CONFIG.SHEETS_ENDPOINT;
window.GR_WHATSAPP_NUM    = window.GR_CONFIG.WHATSAPP_NUM;
window.GR_CONTACT_EMAIL   = window.GR_CONFIG.CONTACT_EMAIL;
window.GR_SITE_ORIGIN     = window.GR_CONFIG.SITE_ORIGIN;

// Helper: post a form payload to the Sheets endpoint with a graceful mailto fallback.
// Used by every form on the site.
window.GR_postForm = async function (data, fallbackEmail) {
  const cfg = window.GR_CONFIG || {};
  const endpoint = cfg.SHEETS_ENDPOINT;
  const dest = fallbackEmail || cfg.CONTACT_EMAIL || 'hello@gccready.com';

  // Always cache locally as a safety net
  try { localStorage.setItem('gr_form_last', JSON.stringify(data)); } catch (_) {}

  if (endpoint) {
    try {
      const params = new URLSearchParams();
      Object.entries(data).forEach(([k, v]) => params.set(k, String(v == null ? '' : v)));
      const res = await fetch(endpoint, {
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' },
        body: params.toString(),
      });
      if (!res.ok) throw new Error('http ' + res.status);
      return { ok: true, sent: 'sheets' };
    } catch (err) {
      // fall through to mailto
    }
  }

  // mailto fallback
  const subject = `[${data.form_type || 'enquiry'}] ${data.name || ''} ${data.topic ? '· ' + data.topic : ''}`.trim();
  const body = Object.entries(data).map(([k, v]) => `${k}: ${v}`).join('\n');
  window.location.href = `mailto:${dest}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;
  return { ok: true, sent: 'mailto' };
};
