import { useEffect, useState } from 'react';
import WizardProgress from '@/components/newcover/WizardProgress';
import WizardStep from '@/components/newcover/WizardStep';
import { steps } from '@/components/newcover/wizardConfig';
import { customerService } from '@/services/customerService';
import { componentService } from '@/services/componentService';
import { projectService } from '@/services/projectService';
export default function NewCoverWizard({ onCreated }) { const [step,setStep]=useState(1); const [customers,setCustomers]=useState([]); const [limbs,setLimbs]=useState([]); const [components,setComponents]=useState([]); const [data,setData]=useState({component_uuids:[],status:'Awaiting measurements'}); const [busy,setBusy]=useState(false); const [error,setError]=useState(''); useEffect(()=>{Promise.all([customerService.list(),componentService.list()]).then(([c,x])=>{setCustomers(c.filter(v=>v.record_state==='active'));setComponents(x.filter(v=>v.record_state==='active'))})},[]); useEffect(()=>{if(data.customer_uuid&&data.customer_uuid!=='new')customerService.history(data.customer_uuid).then(h=>setLimbs(h.limbs.filter(x=>x.record_state==='active')));else setLimbs([])},[data.customer_uuid]); const create=async()=>{setBusy(true);setError('');try{let customerUuid=data.customer_uuid;if(customerUuid==='new'){const c=await customerService.create({...data.newCustomer,approval_status:'Not requested'},'Ray');customerUuid=c.uuid}let limbUuid=data.limb_profile_uuid;if(limbUuid==='new'){const l=await customerService.createLimb({...data.newLimb,customer_uuid:customerUuid,data_label:'WORKSHOP RECORD'},'Ray');limbUuid=l.uuid}const project=await projectService.create({...data,customer_uuid:customerUuid,limb_profile_uuid:limbUuid,newCustomer:undefined,newLimb:undefined},'Ray');onCreated(project)}catch(e){setError(e.message||'Project could not be created.')}finally{setBusy(false)}}; const validByStep=[true,!!data.customer_uuid&&(data.customer_uuid!=='new'||!!data.newCustomer?.full_name),!!data.limb_profile_uuid&&(data.limb_profile_uuid!=='new'||!!data.newLimb?.profile_name),!!data.prosthesis_configuration,(data.component_uuids||[]).length>0,!!data.measurement_method,!!data.cover_boundaries,!!data.sensitive_area_summary,!!data.design_preferences,true,!!data.safety_boundary_accepted]; const valid=validByStep[step]; return <div className="panel mx-auto max-w-3xl p-5 sm:p-8"><WizardProgress step={step} labels={steps}/><h2 className="mb-5 text-2xl font-semibold text-white">{steps[step-1]}</h2><WizardStep step={step} data={data} setData={setData} customers={customers} limbs={limbs} components={components}/>{!valid&&<p role="alert" className="mt-4 text-sm text-amber-300">Complete the required selection or information before continuing.</p>}{error&&<p role="alert" className="mt-4 text-sm text-red-300">{error}</p>}<div className="mt-8 flex justify-between"><button disabled={step===1||busy} onClick={()=>setStep(step-1)} className="btn-secondary">Back</button>{step<10?<button disabled={!valid||busy} onClick={()=>setStep(step+1)} className="btn-primary">Continue</button>:<button disabled={!valid||busy} onClick={create} className="btn-primary">{busy?'Creating…':'Create project'}</button>}</div></div> }