// data.jsx — 17-section onboarding schema + helpers.
// Field types: text, email, tel, url, textarea, radio, checkbox, limited-checkbox, file, color
// Width: "full" | "half"  (half fields rendered side-by-side on desktop)

const SECTIONS = [
  // ─────────────────────────────────────────────────────────────────────────
  {
    id: "basic_info",
    num: "01",
    title: "Basic information",
    eyebrow: "Let's get acquainted",
    blurb: "The essentials — who you are, what you do, and who you serve.",
    minutes: 4,
    fields: [
      { id: "first_name",       type: "text",     label: "First name *",          required: true,  width: "half" },
      { id: "last_name",        type: "text",     label: "Last name *",           required: true,  width: "half" },
      { id: "email",            type: "email",    label: "Email *",               required: true,  width: "half", placeholder: "you@company.com" },
      { id: "phone",            type: "tel",      label: "Phone",                 required: false, width: "half" },
      { id: "company",          type: "text",     label: "Company *",             required: true,  width: "full" },
      { id: "current_url",      type: "url",      label: "Website *",             required: true,  width: "full", placeholder: "https://yourcompany.com" },
      { id: "industry",         type: "text",     label: "Industry / niche *",    required: true,  width: "half" },
      { id: "org_type",         type: "radio",    label: "Type of organization *", required: true,
        options: ["For-profit business", "Nonprofit / 501(c)(3)", "Government / public sector", "Faith-based", "Educational", "Other"] },
      { id: "target_audience",  type: "textarea", label: "Target audience *",     required: true,  rows: 3,
        hint: "Who is your primary customer? Be specific — demographics, situation, mindset." },
      { id: "usp",              type: "textarea", label: "Unique selling proposition (USP) *", required: true, rows: 3,
        hint: "The one thing that makes you the obvious choice over everyone else." },
      { id: "brand_promise",    type: "textarea", label: "What is your brand promise? *", required: true, rows: 3,
        hint: "The commitment you make to every customer, every time." },
      { id: "products_services",type: "textarea", label: "What products / services do you provide? *", required: true, rows: 4 },
    ],
  },

  // ─────────────────────────────────────────────────────────────────────────
  {
    id: "heart_of_business",
    num: "02",
    title: "The heart of your business",
    eyebrow: "Your 'why'",
    blurb: "The beliefs, standards, and origin story that drive everything you do.",
    minutes: 5,
    fields: [
      { id: "why_started",      type: "textarea", label: "Why did you start this business? *", required: true, rows: 5,
        hint: "Was it a problem you saw, a personal experience, a gap in the market, or something that frustrated you about your industry?" },
      { id: "industry_belief",  type: "textarea", label: "What do you believe about your industry that many others don't? *", required: true, rows: 4,
        hint: "Something competitors misunderstand, something customers deserve but rarely get, or something the industry consistently gets wrong." },
      { id: "non_negotiable",   type: "textarea", label: "What standard or philosophy do you refuse to compromise on? *", required: true, rows: 3,
        hint: "Quality, honesty, transparency, customer experience, expertise — what's non-negotiable for you?" },
    ],
  },

  // ─────────────────────────────────────────────────────────────────────────
  {
    id: "problem_you_solve",
    num: "03",
    title: "The problem you solve",
    eyebrow: "The real pain",
    blurb: "What brings customers to you and what makes them finally choose your brand.",
    minutes: 6,
    fields: [
      { id: "main_problem",     type: "textarea", label: "What problem brings customers to you most often? *", required: true, rows: 4,
        hint: "Describe the real-life situation they're experiencing when they reach out." },
      { id: "tried_before",     type: "textarea", label: "What have customers usually tried before coming to you? *", required: true, rows: 3,
        hint: "Other providers, DIY solutions, ignoring the problem, cheaper options, etc." },
      { id: "frustration_before",type:"textarea", label: "What frustrated them about those previous solutions? *", required: true, rows: 3,
        hint: "Inconsistent results, lack of expertise, poor service, complicated process, etc." },
      { id: "trigger_moment",   type: "textarea", label: "What moment makes someone finally decide they need a solution like yours? *", required: true, rows: 3,
        hint: "Pain becomes noticeable, something stops working, embarrassment, a recommendation, curiosity, preparing for an event." },
      { id: "choice_reason",    type: "textarea", label: "What usually convinces them to choose YOUR brand specifically? *", required: true, rows: 3,
        hint: "Recommendation, reputation, packaging/branding, quality, brand story, alignment with values." },
    ],
  },

  // ─────────────────────────────────────────────────────────────────────────
  {
    id: "life_before_after",
    num: "04",
    title: "Life before & after your brand",
    eyebrow: "The transformation",
    blurb: "The emotional and practical journey your customers experience.",
    minutes: 5,
    fields: [
      { id: "life_before",         type: "textarea", label: "What does life feel like for customers BEFORE they find you? *", required: true, rows: 3,
        hint: "Frustrated, embarrassed, overwhelmed, stuck, confused — describe their emotional state." },
      { id: "practical_improvement",type:"textarea", label: "What practical improvement does your product or service create? *", required: true, rows: 3,
        hint: "Clearer skin, better taste, relief from pain, improved performance, easier routine." },
      { id: "emotional_improvement",type:"textarea", label: "How does that improvement make them FEEL? *", required: true, rows: 3,
        hint: "Confident, proud, relieved, empowered, relaxed, happy, energized." },
      { id: "customer_feedback",    type: "textarea", label: "What do customers typically say after experiencing your product or service? *", required: true, rows: 4,
        hint: "Things they say in reviews, comments to your team, phrases they repeat, feedback you hear often." },
    ],
  },

  // ─────────────────────────────────────────────────────────────────────────
  {
    id: "your_difference",
    num: "05",
    title: "Your difference",
    eyebrow: "What sets you apart",
    blurb: "What makes your business genuinely different — in your own words.",
    minutes: 4,
    fields: [
      { id: "differentiators",          type: "textarea", label: "What makes your business truly different from competitors? *", required: true, rows: 4,
        hint: "Experience, philosophy, process, service style, quality commitment — be honest and specific." },
      { id: "one_sentence_description", type: "text",     label: "If a customer described you in one sentence, what would you hope they'd say? *", required: true,
        placeholder: 'e.g. "The company that always delivers quality."' },
      { id: "top_three_advantages",     type: "textarea", label: "What are 3 things you consistently do better than competitors? *", required: true, rows: 4,
        hint: "One per line. Be specific — not 'great customer service' but 'we respond within 2 hours, always.'" },
    ],
  },

  // ─────────────────────────────────────────────────────────────────────────
  {
    id: "ideal_customer",
    num: "06",
    title: "Your ideal customer",
    eyebrow: "Who you serve best",
    blurb: "A vivid portrait of the customers who get the most value from you.",
    minutes: 5,
    fields: [
      { id: "ideal_customer_description", type: "textarea", label: "Describe your best type of customer *", required: true, rows: 5,
        hint: "Age range · Profession & income · Life stage · What they care about most · Lifestyle." },
      { id: "customer_values",             type: "textarea", label: "What do these customers value most? *", required: true, rows: 3,
        hint: "Convenience, quality, expertise, trust, results, speed, relationship, etc." },
      { id: "choice_motivation",           type: "textarea", label: "What motivates them to choose one company over another? *", required: true, rows: 3,
        hint: "Trust, expertise, price, reviews, past experience, referrals, etc." },
    ],
  },

  // ─────────────────────────────────────────────────────────────────────────
  {
    id: "identity",
    num: "07",
    title: "Identity — why people choose your brand",
    eyebrow: "Who they become",
    blurb: "The self-image customers feel when they choose your brand.",
    minutes: 2,
    fields: [
      { id: "brand_identity", type: "textarea", label: "What type of person do customers feel like when they choose your brand? *", required: true, rows: 4,
        hint: "Someone who chooses quality · values expertise · supports local · starts trends · is ahead of the curve." },
    ],
  },

  // ─────────────────────────────────────────────────────────────────────────
  {
    id: "industry_perspective",
    num: "08",
    title: "Industry perspective",
    eyebrow: "Your point of view",
    blurb: "The opinions and convictions that shape how you operate.",
    minutes: 4,
    fields: [
      { id: "industry_frustration",  type: "textarea", label: "What frustrates you about how your industry usually operates? *", required: true, rows: 3,
        hint: "Outdated practices, poor customer service, lack of transparency, low standards." },
      { id: "customer_misconception",type: "textarea", label: "What misconception do customers commonly have about your industry? *", required: true, rows: 3,
        hint: "Believing something is 'normal', misunderstanding the process, assuming all providers are the same." },
      { id: "category_redefinition", type: "textarea", label: "If you could redefine your category, what would you want your company known for? *", required: true, rows: 3,
        hint: "The most trusted · the most reliable · the expert authority · the innovative leader." },
      { id: "industry_disagreement", type: "textarea", label: "What belief about your industry do you strongly disagree with? *", required: true, rows: 3,
        hint: '"Customers only care about price." · "People should just live with this problem." · "More options are always better."' },
    ],
  },

  // ─────────────────────────────────────────────────────────────────────────
  {
    id: "brand_personality",
    num: "09",
    title: "Brand personality",
    eyebrow: "Voice & tone",
    blurb: "How your brand shows up — its energy, tone, and character.",
    minutes: 3,
    fields: [
      { id: "brand_as_person", type: "radio", label: "If your brand were a person, how would you describe it? *", required: true,
        options: ["Friendly", "Authoritative", "Disciplined", "Compassionate", "Innovative", "Dependable"] },
      { id: "brand_tone",      type: "radio", label: "What tone should your brand communicate with customers? *", required: true,
        options: ["Professional", "Warm", "Confident", "Educational", "Bold"] },
      { id: "avoid_tone",      type: "radio", label: "What tone should your brand avoid? *", required: true,
        options: ["Overly salesy", "Corporate / stiff", "Too casual", "Aggressive"] },
    ],
  },

  // ─────────────────────────────────────────────────────────────────────────
  {
    id: "language_messaging",
    num: "10",
    title: "Language & messaging",
    eyebrow: "The words that work",
    blurb: "The specific language your brand should own.",
    minutes: 3,
    fields: [
      { id: "brand_words",            type: "textarea", label: "What words should people associate with your brand? *", required: true, rows: 3,
        hint: "Confidence · trust · relief · strength · reliability · empowerment — or your own." },
      { id: "brand_promise_statement",type: "textarea", label: "If your brand had a simple promise to customers, what would it be? *", required: true, rows: 3,
        hint: '"You can always trust the quality." · "You\'ll feel better immediately." · "You\'ll never be dismissed here."' },
      { id: "anything_else_brand",    type: "textarea", label: "Is there anything else you'd like to share about your brand that we haven't covered?", rows: 4 },
    ],
  },

  // ─────────────────────────────────────────────────────────────────────────
  {
    id: "brand_style",
    num: "11",
    title: "Brand style",
    eyebrow: "Adjectives, sharpened",
    blurb: "Up to 3 per category. Tight choices force clarity — be honest.",
    minutes: 4,
    fields: [
      { id: "visual_style", type: "limited-checkbox", max: 3, label: "Visual style (choose up to 3) *", required: true,
        options: [
          { label: "Minimalist",  desc: "Simplicity — less is more." },
          { label: "Elegant",     desc: "Graceful and stylish." },
          { label: "Bold",        desc: "Striking and confident." },
          { label: "Vintage",     desc: "Nostalgic, prior era." },
          { label: "Modern",      desc: "Contemporary or futuristic." },
          { label: "Rustic",      desc: "Natural, countryside feel." },
          { label: "Artistic",    desc: "Creative and expressive." },
          { label: "Luxurious",   desc: "Rich, lavish, superior quality." },
        ] },
      { id: "visual_style_other",       type: "text",     label: "Other visual style (describe)" },
      { id: "emotional_appeal", type: "limited-checkbox", max: 3, label: "Emotional appeal (choose up to 3) *", required: true,
        options: [
          { label: "Empowering",    desc: "Inspires confidence." },
          { label: "Playful",       desc: "Fun and light-hearted." },
          { label: "Sophisticated", desc: "Worldly and refined." },
          { label: "Innovative",    desc: "Original, forward-thinking." },
          { label: "Trustworthy",   desc: "Reliable and honest." },
          { label: "Approachable",  desc: "Friendly and welcoming." },
          { label: "Edgy",          desc: "Daring and trend-setting." },
          { label: "Passionate",    desc: "Driven by strong belief." },
        ] },
      { id: "emotional_appeal_other",    type: "text",     label: "Other emotional appeal (describe)" },
      { id: "values_ethos", type: "limited-checkbox", max: 3, label: "Values & ethos (choose up to 3) *", required: true,
        options: [
          { label: "Sustainable",  desc: "Eco-friendly." },
          { label: "Ethical",      desc: "Morally grounded." },
          { label: "Inclusive",    desc: "Open to everyone." },
          { label: "Transparent",  desc: "Open and honest." },
          { label: "Dynamic",      desc: "Energetic and active." },
          { label: "Authentic",    desc: "Genuine and real." },
          { label: "Pioneering",   desc: "Trailblazing." },
          { label: "Holistic",     desc: "Whole-system thinking." },
        ] },
      { id: "values_ethos_other",        type: "text",     label: "Other values & ethos (describe)" },
      { id: "audience_connection", type: "limited-checkbox", max: 3, label: "Audience connection (choose up to 3) *", required: true,
        options: [
          { label: "Youthful",          desc: "Appeals to the young." },
          { label: "Family-friendly",   desc: "Suitable for all ages." },
          { label: "Professional",      desc: "For working professionals." },
          { label: "Luxury",            desc: "High-end clientele." },
          { label: "Budget-conscious",  desc: "Value-focused." },
          { label: "Tech-savvy",        desc: "Technologically proficient." },
          { label: "Health-conscious",  desc: "Wellness-oriented." },
          { label: "Adventure-seeking", desc: "Loves new experiences." },
        ] },
      { id: "audience_connection_other", type: "text",     label: "Other audience connection (describe)" },
      { id: "emotional_triggers",        type: "textarea", label: "What emotional triggers resonate most with your audience?", rows: 2,
        hint: "Trust, convenience, status, belonging, achievement, safety, etc." },
    ],
  },

  // ─────────────────────────────────────────────────────────────────────────
  {
    id: "design_branding",
    num: "12",
    title: "Design & branding",
    eyebrow: "Your assets",
    blurb: "Logo, colours, design frustrations, and what you envy about competitors.",
    minutes: 4,
    fields: [
      { id: "design_frustration", type: "textarea", label: "From a DESIGN perspective, what frustrates you about your CURRENT website? *", required: true, rows: 3,
        hint: "Be specific. 'Hero looks cluttered.' 'Colours don't match our vibe.' 'Mobile menu is broken.'" },
      { id: "competitor_envy",    type: "textarea", label: "What do your competitors' websites have that yours currently doesn't?", rows: 3 },
      { id: "logo_files",         type: "file",     label: "Company logo — all digital formats you have",
        hint: "PDF, PNG, JPEG or JPG — upload after the call in the review screen." },
      { id: "logo_link",          type: "url",      label: "Link to vector logo files (Dropbox, Google Drive, etc.)" },
      { id: "palette_status",     type: "radio",    label: "Do you have an existing brand colour palette or style guide?",
        options: ["No specific colours", "Yes, we have specific colours we use", "We have a full style guide"] },
      { id: "primary_color",      type: "color",    label: "Primary colour (HEX or RGB)",   width: "half" },
      { id: "secondary_color",    type: "color",    label: "Secondary / accent colour",      width: "half" },
      { id: "brand_assets",       type: "textarea", label: "Any fonts, graphic elements, icon packs, or brand imagery to carry over?", rows: 3 },
      { id: "style_guide_files",  type: "file",     label: "Brand style guide or other assets",
        hint: "PNG, PDF, JPEG or JPG — upload after the call in the review screen." },
      { id: "style_guide_link",   type: "url",      label: "Link to style guide / other assets (Dropbox, Google Drive, etc.)" },
    ],
  },

  // ─────────────────────────────────────────────────────────────────────────
  {
    id: "reference_sites",
    num: "13",
    title: "Reference websites",
    eyebrow: "Show us your taste",
    blurb: "3 websites you love, any industry. Be specific — we read every word.",
    minutes: 5,
    fields: [
      { id: "ref1_url",   type: "url",      label: "Website example #1 — URL",                   placeholder: "https://" },
      { id: "ref1_likes", type: "checkbox", label: "What specifically do you like about #1?",
        options: ["Header layout (logo / menu)", "Hero section of homepage", "Use of video", "Use of colour", "Use of graphics / backgrounds / shapes", "Footer layout", "Fonts / typography", "Wording / content / copy / headlines", "Overall layout / look & feel"] },
      { id: "ref1_notes", type: "textarea", label: "What else do you love about #1?",            rows: 3 },
      { id: "ref2_url",   type: "url",      label: "Website example #2 — URL",                   placeholder: "https://" },
      { id: "ref2_likes", type: "checkbox", label: "What specifically do you like about #2?",
        options: ["Header layout (logo / menu)", "Hero section of homepage", "Use of video", "Use of colour", "Use of graphics / backgrounds / shapes", "Footer layout", "Fonts / typography", "Wording / content / copy / headlines", "Overall layout / look & feel"] },
      { id: "ref2_notes", type: "textarea", label: "What else do you love about #2?",            rows: 3 },
      { id: "ref3_url",   type: "url",      label: "Website example #3 — URL",                   placeholder: "https://" },
      { id: "ref3_notes", type: "textarea", label: "What do you love about #3?",                 rows: 3 },
    ],
  },

  // ─────────────────────────────────────────────────────────────────────────
  {
    id: "content_functions",
    num: "14",
    title: "Content & functions",
    eyebrow: "Map the site",
    blurb: "Goals, conversions, pages, features, and the homepage experience.",
    minutes: 6,
    fields: [
      { id: "primary_goal",   type: "textarea", label: "What is the #1 goal you want your website to accomplish? *", required: true, rows: 3,
        hint: "One sentence. 'Generate 20 qualified leads a month.' 'Convert visitors into booked appointments.'" },
      { id: "primary_cta",    type: "radio",    label: "What is the very next step you want your customer to take after visiting the site?",
        options: ["Request a consultation", "Visit a physical location", "Schedule a meeting", "Buy something", "Get a quote", "Request pricing", "Donate now"] },
      { id: "secondary_cta",  type: "radio",    label: "If the customer isn't ready, what do you want them to do instead?",
        options: ["Visit a physical location", "Fill out a form for more information", "Call for more information", "Take a self-qualifying quiz", "Download a brochure or guide", "Join email list", "Follow us on social media"] },
      { id: "secondary_goals",type: "textarea", label: "What are the secondary goals for your website?", rows: 3,
        hint: "Brand awareness, SEO, recruiting, partner outreach — anything beyond the primary conversion." },
      { id: "pages",          type: "checkbox", label: "Pages you'd like added to your site",
        options: ["Services","Practice Areas","Features / Benefits","Pricing","Products (non e-commerce)","Contact Us","Free Consultation Page","Get A Quote Form","Join Our Newsletter","FAQs","Resources","Blog","News / Press","Glossary","About Us","Mission / Vision / Values","Statement of Faith","Board of Directors","Testimonials / Reviews","Careers","Community Service","Social Media Links","Helpdesk Link (3rd party)","Portal Link (3rd party)","Donate Now","Show Page / Survey Form"] },
      { id: "pages_other",    type: "textarea", label: "Other pages you'd like added", rows: 2, hint: "One per line." },
      { id: "features",       type: "checkbox", label: "Special features to add to your site",
        options: ["E-commerce store","Video gallery","Photo gallery","Project portfolio — individual pages","Team / staff bios — individual pages","Locations — individual pages","Job board (integrated)","Brand assets / Guide page","Google reviews feed","Social feed (e.g. Instagram)","Restaurant menu (integrated)","Customer support / helpdesk (integrated)","Member portal (integrated)","Events calendar (integrated)","Donation or payment forms","GDPR cookie manager / opt-out"] },
      { id: "features_other", type: "textarea", label: "Other features to include", rows: 2 },
      { id: "practice_areas", type: "textarea", label: "Please list all your practice areas or services",
        hint: "One per line — we'll use this to scaffold your service pages.", rows: 6 },
      { id: "portal_urls",    type: "textarea", label: "Customer portal or helpdesk URLs to include", rows: 2 },
      { id: "homepage_blocks",type: "checkbox", label: "Content / functions you'd like on the homepage",
        options: ["Chat feature popup","Features / services bullet list","Quick contact form","Testimonial carousel or feed","Product carousel (e-com only)","Our 3-step process","Video","Team member carousel or leader spotlight","Blog posts feed"] },
      { id: "homepage_other", type: "textarea", label: "Other homepage content or functions", rows: 2 },
      { id: "notification_emails", type: "textarea", label: "What email address(es) should form notifications be sent TO? *", required: true, rows: 2,
        hint: "Comma-separated for multiple recipients." },
    ],
  },

  // ─────────────────────────────────────────────────────────────────────────
  {
    id: "dev_logistics",
    num: "15",
    title: "Development & logistics",
    eyebrow: "Plumbing",
    blurb: "Where the site lives and who controls the keys.",
    minutes: 3,
    fields: [
      { id: "final_url",      type: "url",      label: "What will be the final, live URL of the new site?", placeholder: "https://", width: "full" },
      { id: "redirect_domains",type:"text",     label: "Other domains to be redirected to the new site",    width: "half" },
      { id: "registrar",      type: "text",     label: "Domain name registrar / hosting location",
        hint: "GoDaddy, Namecheap, Cloudflare, Squarespace, etc." },
      { id: "admin_control",  type: "radio",    label: "Who has admin control of your domain & hosting?",
        options: ["I have admin control of my domain only", "I have admin control of my domain AND hosting", "Domain / hosting controlled by another company", "I don't know"] },
      { id: "admin_contact",  type: "textarea", label: "Contact info for the person / company with admin access",
        hint: "Name, company, email, phone — whatever you have.", rows: 3 },
    ],
  },

  // ─────────────────────────────────────────────────────────────────────────
  {
    id: "public_info",
    num: "16",
    title: "Public info for website",
    eyebrow: "On-page details",
    blurb: "Details that will appear publicly. Put 'n/a' for anything you don't want visible.",
    minutes: 2,
    fields: [
      { id: "hours",           type: "textarea", label: "Hours of operation", rows: 4,
        hint: "Mon–Fri 9am–5pm, Sat 10am–2pm, Sun closed — whatever applies." },
      { id: "public_phone",    type: "tel",      label: "Contact phone number",     width: "half" },
      { id: "public_email",    type: "email",    label: "Contact email",            width: "half" },
      { id: "physical_address",type: "textarea", label: "Physical address",         rows: 3 },
      { id: "mailing_address", type: "textarea", label: "Mailing address (if different)", rows: 3 },
      { id: "social_urls",     type: "textarea", label: "All social media profile URLs to include", rows: 5,
        hint: "One per line — full URL for each platform." },
    ],
  },

  // ─────────────────────────────────────────────────────────────────────────
  {
    id: "legal",
    num: "17",
    title: "Legal considerations",
    eyebrow: "Cover the bases",
    blurb: "Privacy, accessibility, jurisdictions — quick answers.",
    minutes: 2,
    fields: [
      { id: "serves_eu",          type: "radio",    label: "Do you serve customers in the EU? *",                           required: true,  options: ["Yes", "No"], width: "half" },
      { id: "serves_ca",          type: "radio",    label: "Do you serve customers in California? *",                       required: true,  options: ["Yes", "No"], width: "half" },
      { id: "has_privacy",        type: "radio",    label: "Do you have an existing website privacy policy & terms of use?",                options: ["Yes", "No"], width: "half" },
      { id: "needs_a11y",         type: "radio",    label: "Do you need an accessibility review for your new site?",
        hint: "Recommended for B2C businesses, physical locations, or audiences that may include people with disabilities.",
        options: ["Yes", "No"], width: "half" },
      { id: "legalese_files",     type: "file",     label: "Upload your specific legalese (privacy, terms, etc.)",
        hint: "PDF, DOCX, or DOC — upload after the call in the review screen." },
      { id: "legalese_text",      type: "textarea", label: "…or paste your legalese here", rows: 5 },
      { id: "footer_disclaimers", type: "textarea", label: "Any licence numbers, logos, parent company links, or disclaimers required in the footer? *",
        required: true, rows: 3 },
      { id: "additional_comments",type: "textarea", label: "Anything else we should take into account before we begin the design?", rows: 5 },
    ],
  },
];

// ─── Helpers shared with agent + submission logic ─────────────────────────────

function flattenFields(sections) {
  const out = [];
  for (const s of sections)
    for (const f of s.fields) out.push({ ...f, sectionId: s.id, sectionTitle: s.title });
  return out;
}

function fieldById(sections, id) {
  return flattenFields(sections).find((f) => f.id === id) || null;
}

function isFilled(v) {
  return Array.isArray(v) ? v.length > 0 : v != null && String(v).trim() !== "";
}

// Compact schema handed to the LLM / analysis plan.
function buildSchemaForLLM(sections) {
  return sections.map((s) => ({
    sectionId: s.id,
    title: s.title,
    fields: s.fields.map((f) => {
      const out = { id: f.id, label: f.label, type: f.type };
      if (f.required) out.required = true;
      if (f.type === "limited-checkbox") out.max = f.max || 3;
      if (f.options) out.options = f.options.map((o) => (typeof o === "string" ? o : o.label));
      if (f.type === "file") out.note = "Upload manually in review screen — cannot fill by voice.";
      return out;
    }),
  }));
}

// Build a plain-text summary of pre-filled data for the Vapi system prompt.
function buildPrefilledSummary(data) {
  const flat = flattenFields(SECTIONS);
  const filled = flat.filter((f) => isFilled(data[f.id]) && f.type !== "file");
  if (!filled.length) return "No data pre-filled yet.";
  return filled
    .map((f) => {
      const v = data[f.id];
      const display = Array.isArray(v) ? v.join(", ") : String(v);
      return `${f.label.replace(/\s*\*$/, "").trim()}: ${display}`;
    })
    .join("\n");
}

window.SECTIONS         = SECTIONS;
window.flattenFields    = flattenFields;
window.fieldById        = fieldById;
window.isFilled         = isFilled;
window.buildSchemaForLLM   = buildSchemaForLLM;
window.buildPrefilledSummary = buildPrefilledSummary;
