/* ============ App shell ============ */
const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "accent": "#191c1f",
  "radius": 22
}/*EDITMODE-END*/;

function App(){
  const [t,setTweak]=useTweaks(TWEAK_DEFAULTS);
  const [authed,setAuthed]=useState(()=>localStorage.getItem('sw_auth')==='1');
  const [view,setView]=useState(()=>{
    const v=localStorage.getItem('sw_view');
    const valid=['dashboard','cargoes','archive','applications','passes','documents','calculator','claims','notifications','admin','settings','support'];
    return valid.includes(v)?v:'dashboard';
  });
  const [search,setSearch]=useState('');
  const [cargo,setCargo]=useState(null);
  const [creating,setCreating]=useState(false);
  const [toast,setToast]=useState(null);

  useEffect(()=>{ localStorage.setItem('sw_view',view); setSearch(''); },[view]);
  const notify=m=>setToast(m);
  const go=v=>{setView(v);};

  if(!authed){
    return <Login onLogin={()=>{localStorage.setItem('sw_auth','1');setAuthed(true);}}/>;
  }

  const titles={dashboard:'Дашборд',cargoes:'Реестр грузов',archive:'Архив',applications:'Заявки',passes:'Пропуска',documents:'Документы',calculator:'Калькулятор',claims:'Претензии',notifications:'Уведомления',admin:'Администрирование',settings:'Настройки',support:'Поддержка'};
  const badges={
    cargoes:SW.current.filter(c=>c.status==='ready').length,
    applications:SW.applications.filter(a=>a.status==='processing').length,
    notifications:SW.notifications.filter(n=>!n.read).length,
    passes:SW.passes.filter(p=>p.status==='pending').length,
  };
  const rootStyle={'--accent':t.accent,'--r-lg':t.radius+'px','--r-md':Math.max(10,t.radius-6)+'px'};
  const showCreate=['dashboard','cargoes','archive','applications'].includes(view);

  return (
    <div className="app" style={rootStyle}>
      <Sidebar view={view} setView={setView} badges={badges}/>
      <div className="main">
        <TopBar title={titles[view]} search={search} setSearch={setSearch}
          onCreate={showCreate?(view==='applications'?()=>setCreating(true):()=>setView('applications')):null}
          createLabel="Создать заявку"/>

        {view==='dashboard' && <Dashboard setView={go} openCargo={setCargo} notify={notify}/>}
        {view==='cargoes' && <Cargoes search={search} openCargo={setCargo} notify={notify}/>}
        {view==='archive' && <Cargoes search={search} openCargo={setCargo} notify={notify} archived/>}
        {view==='applications' && <Applications search={search} openCreate={()=>setCreating(true)} notify={notify}/>}
        {view==='passes' && <Passes search={search} notify={notify}/>}
        {view==='documents' && <Documents search={search} notify={notify}/>}
        {view==='calculator' && <Calculator openCreate={()=>setCreating(true)} notify={notify}/>}
        {view==='claims' && <Claims search={search} notify={notify}/>}
        {view==='notifications' && <Notifications notify={notify}/>}
        {view==='admin' && <Admin notify={notify}/>}
        {view==='settings' && <Settings notify={notify}/>}
        {view==='support' && <Support notify={notify}/>}
      </div>

      {cargo && <CargoCard cargo={cargo} onClose={()=>setCargo(null)} setView={go} notify={notify}/>}
      {creating && <CreateApplication onClose={()=>setCreating(false)} notify={notify}/>}
      {toast && <Toast msg={toast} onDone={()=>setToast(null)}/>}

      <TweaksPanel>
        <TweakSection label="Акцент"/>
        <TweakColor label="Цвет действий" value={t.accent} options={['#191c1f','#494fdf','#00a87e','#e23b4a']} onChange={v=>setTweak('accent',v)}/>
        <TweakSection label="Форма"/>
        <TweakSlider label="Скругление карточек" value={t.radius} min={12} max={28} unit="px" onChange={v=>setTweak('radius',v)}/>
        <TweakSection label="Сессия"/>
        <TweakButton label="Выйти из кабинета" onClick={()=>{localStorage.removeItem('sw_auth');setAuthed(false);}}/>
      </TweaksPanel>
    </div>
  );
}

ReactDOM.createRoot(document.getElementById('root')).render(<App/>);
