/* App root — Portal Maximiano */
const { useState:aS, useEffect:aE } = React;

const PAGE_META={
  dashboard:{title:'Dashboard Executivo',crumb:'Visão geral consolidada'},
  feed:{title:'Comunicação Interna',crumb:'Canais & comunicados'},
  faturamento:{title:'Faturamento Individual',crumb:'Meu desempenho'},
  juridico:{title:'Jurídico em Foco',crumb:'Novidades jurídicas'},
  gestao:{title:'Gestão',crumb:'Usuários & unidades'},
  config:{title:'Configurações',crumb:'Sistema'},
};
const ALLOWED={
  ceo:['dashboard','feed','faturamento','juridico','gestao','config'],
  admin:['dashboard','feed','faturamento','juridico','gestao','config'],
  gestor:['dashboard','feed','faturamento','juridico','gestao'],
  adv:['feed','faturamento','juridico'],
};

function Toast({msg}){
  if(!msg) return null;
  return (
    <div style={{position:'fixed',bottom:26,left:'50%',transform:'translateX(-50%)',zIndex:200,
      background:'#2d3245',color:'#fff',padding:'13px 20px',borderRadius:12,boxShadow:'0 12px 40px rgba(28,32,52,.3)',
      display:'flex',alignItems:'center',gap:11,fontSize:13.5,fontWeight:500,animation:'fadeUp .3s ease both',maxWidth:440}}>
      <span style={{width:26,height:26,borderRadius:7,background:'rgba(182,139,76,.25)',color:'#e4d2ad',display:'grid',placeItems:'center',flexShrink:0}}>
        <Icon name="check" size={15} stroke={2.6}/>
      </span>
      {msg}
    </div>
  );
}

function App(){
  const [profileId,setProfileId]=aS(()=>localStorage.getItem('mx_profile')||null);
  const [page,setPage]=aS(()=>localStorage.getItem('mx_page')||'dashboard');
  const [toast,setToast]=aS(null);
  const [sbOpen,setSbOpen]=aS(false);
  const profile = profileId? MX.perfis.find(p=>p.id===profileId) : null;

  aE(()=>{ if(profileId) localStorage.setItem('mx_profile',profileId); },[profileId]);
  aE(()=>{ localStorage.setItem('mx_page',page); },[page]);

  const showToast=(m)=>{ setToast(m); clearTimeout(window.__tt); window.__tt=setTimeout(()=>setToast(null),2800); };
  const nav=(p)=>{ setPage(p); setSbOpen(false); };

  const login=(pid)=>{ const allowed=ALLOWED[pid]; setProfileId(pid); setPage(allowed.includes(page)?page:allowed[0]); };
  const switchProfile=(pid)=>{ const allowed=ALLOWED[pid]; setProfileId(pid); if(!allowed.includes(page)) setPage(allowed[0]); showToast('Perfil alterado para '+MX.perfis.find(p=>p.id===pid).papel.split('·')[0].trim()+'.'); };
  const logout=()=>{ setProfileId(null); localStorage.removeItem('mx_profile'); setSbOpen(false); };

  if(!profile) return <Login onLogin={login}/>;

  const allowed=ALLOWED[profileId];
  const meta=PAGE_META[page]||PAGE_META.dashboard;

  let view;
  if(page==='dashboard') view=<Dashboard profile={profile} onToast={showToast}/>;
  else if(page==='feed') view=<FeedView profile={profile} onToast={showToast}/>;
  else if(page==='faturamento') view=<FaturamentoView profile={profile} onToast={showToast}/>;
  else if(page==='juridico') view=<JuridicoView onToast={showToast}/>;
  else if(page==='gestao') view=<GestaoView profile={profile} onToast={showToast}/>;
  else if(page==='config') view=<ConfigView onToast={showToast}/>;

  return (
    <div className="shell">
      <Sidebar active={page} onNav={nav} profile={profile} unreadFeed={3} onLogout={logout} allowed={allowed} open={sbOpen} onClose={()=>setSbOpen(false)}/>
      <div className="main">
        <Topbar title={meta.title} crumb={meta.crumb} profile={profile} onSwitch={switchProfile} onMenuOpen={()=>setSbOpen(true)}/>
        <div className="content" key={page+profileId}>
          {view}
        </div>
      </div>
      <Toast msg={toast}/>
    </div>
  );
}

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