(()=>{
const NX=window.NURVEXAIDesignSystem_4750bb;const {useState,useRef,useEffect}=React;
const QA=[['What does NURVEX AI build?','We build websites, applications, AI agents, voice systems and business automations. Most projects connect several of them into one system.'],
  ['How would an AI agent help my business?','An AI agent can answer customer questions, qualify enquiries and take actions such as booking appointments or updating your CRM, at any hour.'],
  ['Can you automate my follow-up?','Yes. New leads can get an instant reply, be qualified by AI and enter the right follow-up by email or WhatsApp automatically.']];
function useTimers(){const ts=useRef([]);useEffect(()=>()=>ts.current.forEach(clearTimeout),[]);return (fn,ms)=>ts.current.push(setTimeout(fn,ms));}
function Head({n,title,live}){return <div className="lab-top"><div className="lab-title"><span className="mono" style={{color:'var(--text-muted)'}}>{n}</span>{title}</div>{live?<NX.Badge tone="online" dot pulse>Running</NX.Badge>:<NX.Badge tone="demo">Demonstration</NX.Badge>}</div>}
function Chat(){
  const [msgs,setMsgs]=useState([{r:'ai',t:'Hello. Ask me anything about building an AI system for your business.'}]);const [busy,setBusy]=useState(false);const after=useTimers();
  const ask=([q,a])=>{if(busy)return;setBusy(true);setMsgs(m=>[...m.slice(-2),{r:'user',t:q},{r:'ai',typing:true}]);after(()=>{setMsgs(m=>[...m.slice(0,-1),{r:'ai',t:a}]);setBusy(false)},1300)};
  return <div className="lab-cell"><Head n="01" title="AI Chat" live={busy}/><div className="lab-body">
    <div className="lab-chat">{msgs.map((m,i)=><NX.ChatMessage key={i+m.t} role={m.r} typing={m.typing}>{m.t}</NX.ChatMessage>)}</div>
    <div className="lab-chips">{QA.map(x=><NX.Tag key={x[0]} onClick={()=>ask(x)}>{x[0]}</NX.Tag>)}</div></div></div>;
}
const CALL=[['AI','Good afternoon, thank you for calling. How can I help?'],['Caller','I would like to book a consultation next week.'],['AI','Of course. I have Tuesday at 11:00 or Thursday at 15:00. Which suits you?'],['Caller','Thursday, please.'],['AI','Booked. A confirmation text is on its way.']];
function Voice(){
  const [on,setOn]=useState(false);const [n,setN]=useState(0);const after=useTimers();
  const start=()=>{setOn(true);setN(0);CALL.forEach((_,i)=>after(()=>setN(i+1),900+i*1700));after(()=>setOn(false),900+CALL.length*1700)};
  return <div className="lab-cell"><Head n="02" title="AI Voice" live={on}/><div className="lab-body">
    <NX.Waveform active={on} bars={44} height={56}/>
    <div className="lab-tx">{CALL.slice(Math.max(0,n-3),n).map(([w,t])=><p key={t} className={w==='AI'?'':'c'}><b>{w}</b>{t}</p>)}{n===0?<p style={{color:'var(--text-muted)'}}>Start the call to hear how an AI receptionist handles a booking.</p>:null}
      {n>=CALL.length?<p className="mono" style={{color:'var(--status-online)'}}>✓ Appointment booked  ✓ CRM updated</p>:null}</div>
    <div><NX.Button size="sm" variant={on?'secondary':'primary'} onClick={on?undefined:start} disabled={on}>{on?'Call in progress':'Start call'}</NX.Button></div></div></div>;
}
const QS=[['Need',['Website','AI agent','Automation']],['Timeline',['This month','This quarter','Exploring']],['Scope',['One system','Several systems']]];
const W=[[2,3,3],[3,2,1],[2,3]];
function Lead(){
  const [sel,setSel]=useState([null,null,null]);const done=sel.every(x=>x!==null);
  const score=sel.reduce((s,v,i)=>s+(v===null?0:W[i][v]),0);const pct=done?Math.round(score/9*100):sel.filter(x=>x!==null).length*12;
  const out=!done?'Awaiting answers':score>=7?'High intent — route to strategy call':score>=5?'Qualified — send proposal outline':'Nurture — send case studies';
  return <div className="lab-cell"><Head n="03" title="Lead Qualifier" live={done}/><div className="lab-body">
    {QS.map(([q,opts],i)=><div key={q} className="lab-q"><span className="mono" style={{color:'var(--text-muted)'}}>{q}</span><div className="lab-chips">{opts.map((o,j)=><NX.Tag key={o} active={sel[i]===j} onClick={()=>setSel(s=>s.map((v,k)=>k===i?j:v))}>{o}</NX.Tag>)}</div></div>)}
    <div style={{marginTop:'auto',display:'flex',flexDirection:'column',gap:10}}><span className="mono" style={{color:'var(--text-accent)'}}>Demonstration analysis</span><div className="lab-meter"><i style={{width:pct+'%'}}></i></div><div className="lab-out" style={{color:done?'var(--text-primary)':'var(--text-muted)'}}>{out}</div></div></div></div>;
}
const STEPS=['New lead','AI qualifies','CRM record','Email','WhatsApp','Sales notified'];
function Auto(){
  const [a,setA]=useState(-1);const after=useTimers();const run=a>=0&&a<STEPS.length;
  const go=()=>{STEPS.concat(['']).forEach((_,i)=>after(()=>setA(i),i*800))};
  return <div className="lab-cell"><Head n="04" title="Automation" live={run}/><div className="lab-body">
    <NX.WorkflowFlow compact steps={STEPS} activeIndex={run?a:-1}/>
    <div className="v-log" style={{flex:1,justifyContent:'flex-end',minHeight:110}}>{STEPS.slice(0,Math.max(0,a)).slice(-4).map(s=><span key={s}><b>✓</b>{s}</span>)}</div>
    <div><NX.Button size="sm" onClick={run?undefined:()=>{setA(0);go()}} disabled={run}>{run?'Running':'Run workflow'}</NX.Button></div></div></div>;
}
function Lab({onCta=()=>window.NXH.openForm()}){
  return <section className="sec lab lab-page" id="lab" data-screen-label="AI Systems"><div className="wrap">
    <div className="lab-head rv"><NX.SectionHeader label="NURVEX AI Systems" size="hero" title={["Don't just read about AI.",<span style={{color:'var(--nx-blue-400)'}}>Experience it.</span>]}/><NX.Button size="lg" iconRight="→" onClick={onCta}>Build my AI system</NX.Button></div>
    <window.NXShowcase/>
    <div className="lab-grid">{[Chat,Voice,Lead,Auto].map((C,i)=><div key={i} className="rv" style={{transitionDelay:i*90+'ms'}}><NX.Card interactive padding={0} style={{height:'100%'}}><C/></NX.Card></div>)}</div>
    <p className="mono rv" style={{color:'var(--text-muted)',marginTop:24}}>All four previews are scripted demonstrations.</p>
  </div></section>;
}
window.NXLab=Lab;
})();
