/* global React, I */
const { useState: useStateS } = React;

function Sidebar({ engagement, view, setView, counts, scanRunning }) {
  const [collapsed, setCollapsed] = useStateS({});
  const [expanded, setExpanded] = useStateS({ web: true, recon: false });

  const toggleGroup = (k) => setCollapsed(c => ({ ...c, [k]: !c[k] }));
  const toggleExp = (k) => setExpanded(c => ({ ...c, [k]: !c[k] }));

  const Item = ({ id, icon, label, count, sub, badge }) => {
    const isParent = !!sub;
    const active = view === id;
    const isExp = expanded[id];
    return (
      <>
        <div
          className={`item ${active ? 'active' : ''} ${isExp ? 'expanded' : ''}`}
          onClick={() => isParent ? toggleExp(id) : setView(id)}
        >
          <span className="ic">{icon}</span>
          <span className="label">{label}</span>
          {badge ? <span className="count" style={{ color: 'var(--accent)', borderColor: 'var(--accent-line)' }}>{badge}</span> : null}
          {count != null ? <span className="count">{count}</span> : null}
          {isParent ? <span className="chev-r"><I.chevR/></span> : null}
        </div>
        {isParent && isExp && sub.map(s => (
          <div
            key={s.id}
            className={`sub ${view === s.id ? 'active' : ''}`}
            onClick={() => setView(s.id)}
          >
            <span className="ic">{s.icon}</span>
            <span>{s.label}</span>
            {s.count != null ? <span className="count">{s.count}</span> : null}
          </div>
        ))}
      </>
    );
  };

  return (
    <aside className="sidebar">
      <div className="engagement-card">
        <div className="eng-title">
          <span style={{ color: 'var(--accent)' }}><I.diamond/></span>
          {engagement.client}
        </div>
        <div className="eng-id">{engagement.id} · {engagement.scope}</div>
        <div className="eng-meta">
          {scanRunning ? <span className="live-dot"/> : <span style={{ width: 6, height: 6, borderRadius: '50%', background: 'var(--text-3)' }}/>}
          {scanRunning ? 'TRACK A · LIVE' : 'TRACK A · IDLE'}
          <span style={{ marginLeft: 'auto' }}>day 03/14</span>
        </div>
      </div>

      <nav className="nav">
        <div className={`group ${collapsed.overview ? 'collapsed' : ''}`} onClick={() => toggleGroup('overview')}>
          <span>OVERVIEW</span>
          <span className="chev"><I.chev/></span>
        </div>
        {!collapsed.overview && <>
          <Item id="dashboard" icon={<I.target/>} label="Engagement" />
          <Item id="surface" icon={<I.radar/>} label="Surface Map" count={counts.surface} />
        </>}

        <div className={`group ${collapsed.discovery ? 'collapsed' : ''}`} onClick={() => toggleGroup('discovery')}>
          <span>DISCOVERY</span>
          <span className="chev"><I.chev/></span>
        </div>
        {!collapsed.discovery && <>
          <Item id="recon" icon={<I.globe/>} label="ATLAS Recon"
            sub={[
              { id: 'recon.subs', icon: <I.fork/>, label: 'Subdomains', count: 47 },
              { id: 'recon.dns',  icon: <I.bracket/>, label: 'DNS history' },
              { id: 'recon.js',   icon: <I.curl/>, label: 'JS artefacts', count: 312 },
              { id: 'recon.arch', icon: <I.folder/>, label: 'Archive', count: 2840 },
            ]}/>
          <Item id="endpoints" icon={<I.search/>} label="Endpoints" count={counts.endpoints} />
          <Item id="params" icon={<I.bracket/>} label="Parameters" count={184}/>
        </>}

        <div className={`group ${collapsed.testing ? 'collapsed' : ''}`} onClick={() => toggleGroup('testing')}>
          <span>ACTIVE TESTING</span>
          <span className="chev"><I.chev/></span>
        </div>
        {!collapsed.testing && <>
          <Item id="vulns" icon={<I.bug/>} label="Vulnerabilities" badge={counts.vulns}
            sub={[
              { id: 'vulns.injection', icon: <I.bolt/>, label: 'Injection (sqlmap)', count: 4 },
              { id: 'vulns.xss', icon: <I.fork/>, label: 'XSS / DOM', count: 7 },
              { id: 'vulns.ssrf', icon: <I.globe/>, label: 'SSRF / RCE', count: 2 },
              { id: 'vulns.idor', icon: <I.key/>, label: 'IDOR', count: 5 },
              { id: 'vulns.cve', icon: <I.shield/>, label: 'Known CVEs', count: 3 },
            ]}/>
          <Item id="auth" icon={<I.key/>} label="Auth & Logic"
            sub={[
              { id: 'auth.bypass', icon: <I.flag/>, label: 'Auth bypass', count: 2 },
              { id: 'auth.authz', icon: <I.shield/>, label: 'Authz / IDOR' },
              { id: 'auth.session', icon: <I.refresh/>, label: 'Session flaws' },
              { id: 'auth.jwt', icon: <I.curl/>, label: 'JWT analysis', count: 1 },
              { id: 'auth.workflow', icon: <I.fork/>, label: 'Workflow logic' },
            ]}/>
          <Item id="api" icon={<I.curl/>} label="API & GraphQL"
            sub={[
              { id: 'api.rest', icon: <I.bracket/>, label: 'REST surface' },
              { id: 'api.gql', icon: <I.graph/>, label: 'GraphQL', count: 38 },
              { id: 'api.ws', icon: <I.fork/>, label: 'WebSockets' },
              { id: 'api.spec', icon: <I.doc/>, label: 'OpenAPI diff' },
            ]}/>
        </>}

        <div className={`group ${collapsed.cipher ? 'collapsed' : ''}`} onClick={() => toggleGroup('cipher')}>
          <span>CIPHER · AI TRIAGE</span>
          <span className="chev"><I.chev/></span>
        </div>
        {!collapsed.cipher && <>
          <Item id="triage" icon={<I.brain/>} label="Triage queue" badge={counts.triage} />
          <Item id="suppressed" icon={<I.filter/>} label="Suppressed" count={812} />
          <Item id="logic" icon={<I.flag/>} label="Logic candidates" count={9} />
        </>}

        <div className={`group ${collapsed.deliver ? 'collapsed' : ''}`} onClick={() => toggleGroup('deliver')}>
          <span>DELIVERABLES</span>
          <span className="chev"><I.chev/></span>
        </div>
        {!collapsed.deliver && <>
          <Item id="findings" icon={<I.list/>} label="Findings" count={counts.findings} />
          <Item id="evidence" icon={<I.folder/>} label="Evidence vault" />
          <Item id="report" icon={<I.doc/>} label="Report draft" />
        </>}
      </nav>

      <div className="footer">
        <div className="avatar">RS</div>
        <div className="who">
          <span>r.shibata</span>
          <small>Lead operator · Track B</small>
        </div>
        <span className="out" title="Sign out"><I.logout/></span>
      </div>
    </aside>
  );
}

window.Sidebar = Sidebar;
