/* global React */
const { useState } = React;

function NavItem({ t, active, onNavigate, status }) {
  const isActive = t.id === active;
  const isSoon = !!t.soon;
  return (
    <button
      onClick={() => !isSoon && onNavigate(t.id)}
      disabled={isSoon}
      style={{
        display: 'flex', alignItems: 'center', gap: 10, width: '100%',
        background: isActive ? 'var(--s-brand-weak)' : 'transparent',
        color: isActive ? 'var(--s-link)' : isSoon ? 'var(--s-fg-disabled)' : 'var(--s-fg-2)',
        border: 0, cursor: isSoon ? 'not-allowed' : 'pointer',
        padding: '7px 8px', borderRadius: 'var(--s-r-md)',
        fontFamily: 'inherit', fontSize: 13,
        fontWeight: isActive ? 500 : 400,
        textAlign: 'left', marginBottom: 1,
        transition: 'background var(--s-dur-fast) var(--s-ease)',
      }}
      onMouseEnter={e => { if (!isActive && !isSoon) e.currentTarget.style.background = 'var(--s-bg-2)'; }}
      onMouseLeave={e => { if (!isActive && !isSoon) e.currentTarget.style.background = 'transparent'; }}>
      <Icon d={t.icon}/>
      <span style={{ flex: 1 }}>{t.label}</span>
      {status === 'running' && <span className="status-dot running"/>}
      {status === 'idle' && <span className="status-dot idle"/>}
      {isSoon && (
        <span style={{ fontSize: 9.5, padding: '1px 6px', borderRadius: 999, background: 'var(--s-bg-3)', color: 'var(--s-fg-3)', textTransform: 'uppercase', letterSpacing: '0.06em', fontWeight: 600 }}>Soon</span>
      )}
    </button>
  );
}

// ── Sidebar ────────────────────────────────────────────────────────────────────
function Sidebar({ active, onNavigate, toolStatuses = {} }) {
  const tools = [
    { id: 'home',       label: 'Home',           icon: I.home },
    { id: 'pipedrive',  label: 'Pipedrive Sync', icon: I.database },
    { id: 'knowledge',  label: 'Knowledge Base', icon: I.book },
    { id: 'monitoring', label: 'System Monitoring', icon: I.activity, soon: true },
  ];
  const system = [
    { id: 'settings', label: 'Settings', icon: I.settings },
  ];

  return (
    <aside style={{
      width: 232, background: 'var(--s-bg-0)',
      borderRight: '1px solid var(--s-border)',
      display: 'flex', flexDirection: 'column',
      flexShrink: 0, height: '100%',
    }}>
      {/* Brand */}
      <div style={{
        height: 56, padding: '0 14px',
        display: 'flex', alignItems: 'center', gap: 10,
        borderBottom: '1px solid var(--s-border)',
      }}>
        <svg width="28" height="28" viewBox="0 0 1000 1000" style={{ borderRadius: 6, display: 'block', flexShrink: 0 }} xmlns="http://www.w3.org/2000/svg">
          <rect width="1000" height="1000" fill="#0000aa"/>
          <g fill="#ffde40" transform="matrix(1.6251891,0,0,1.6251891,-265.22029,107.92313)">
            <path d="m 331.3,339.6 c 0,-43.1 -25.8,-73.1 -50.7,-102.2 -1.3,-1.6 -2.6,-3.1 -4,-4.6 -32.2,-37.9 -38.9,-45.7 -38.9,-69.9 0,-9.1 6.6,-18.9 17.3,-18.9 9,0 15.3,7.8 15.3,18.9 v 31.9 h 59.4 v -42.3 c 0,-15.6 -7.9,-31.4 -21.5,-43.2 C 293.9,96.9 275,90.1 255,90.1 c -24.4,0 -44.9,7.8 -59.2,22.5 -13.4,13.8 -20.7,33.3 -20.7,55 0,33.3 16.2,66.4 48.2,98.4 28.7,28.3 45.4,49.8 45.4,76 0,14.3 -5.9,21.3 -18.1,21.3 -12.2,0 -20.1,-8.4 -20.1,-21.3 v -29.5 h -59 v 42.3 c 0,13.4 6,26.8 16.6,37.6 L 321.2,379 c 6.6,-11.7 10.1,-25.2 10.1,-39.4 z"/>
            <polygon points="483.7,147.9 483.7,94.1 335.5,94.1 335.5,147.9 379.1,147.9 379.1,373.2 442.1,366.9 442.1,147.9 "/>
            <polygon points="555.3,340.7 555.3,269.5 617.3,269.5 617.3,213.7 555.3,213.7 555.3,147.9 617.3,147.9 617.3,94.1 492.3,94.1 492.3,361.9 617.3,349.4 617.3,340.7 "/>
            <path d="m 688.3,291.9 h 14.9 c 16.6,0 33,-5 45,-13.8 14.4,-10.5 22,-25.8 22,-44.1 v -82 c 0,-18.4 -7.6,-33.6 -22,-44.1 -12,-8.8 -28.5,-13.8 -45,-13.8 h -78.3 v 254.5 l 63.4,-6.4 z m 0,-142 h 9.3 c 7.9,0 11.7,4.4 11.7,13.3 v 59.6 c 0,8.9 -3.8,13.3 -11.7,13.3 h -9.3 z"/>
          </g>
        </svg>
        <div style={{ display: 'flex', flexDirection: 'column', lineHeight: 1.1 }}>
          <span style={{ fontSize: 14, fontWeight: 700, fontFamily: 'var(--s-font-display)', letterSpacing: '-0.01em' }}>Robylab</span>
          <span style={{ fontSize: 10.5, color: 'var(--s-fg-3)' }}>Internal tools</span>
        </div>
      </div>

      {/* Nav */}
      <div style={{ flex: 1, overflow: 'auto', padding: '14px 10px', display: 'flex', flexDirection: 'column', gap: 14 }}>
        <div>
          <div style={{ fontSize: 10.5, color: 'var(--s-fg-3)', fontWeight: 500, textTransform: 'uppercase', letterSpacing: '0.08em', padding: '0 8px 6px' }}>Tools</div>
          {tools.map(t => <NavItem key={t.id} t={t} active={active} onNavigate={onNavigate} status={toolStatuses[t.id]}/>)}
        </div>
        <div>
          <div style={{ fontSize: 10.5, color: 'var(--s-fg-3)', fontWeight: 500, textTransform: 'uppercase', letterSpacing: '0.08em', padding: '0 8px 6px' }}>System</div>
          {system.map(t => <NavItem key={t.id} t={t} active={active} onNavigate={onNavigate}/>)}
        </div>
      </div>

      {/* User */}
      <div style={{ padding: 10, borderTop: '1px solid var(--s-border)' }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 10, padding: 8, borderRadius: 'var(--s-r-md)', background: 'var(--s-bg-2)' }}>
          <span style={{ width: 26, height: 26, borderRadius: '50%', background: 'hsl(220 35% 30%)', color: 'hsl(220 70% 85%)', display: 'inline-flex', alignItems: 'center', justifyContent: 'center', fontSize: 11, fontWeight: 700 }}>RL</span>
          <div style={{ flex: 1, minWidth: 0 }}>
            <div style={{ fontSize: 12.5, fontWeight: 500, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>Admin</div>
            <div style={{ fontSize: 11, color: 'var(--s-fg-3)' }}>Robylab</div>
          </div>
        </div>
      </div>
    </aside>
  );
}

// ── TopBar ─────────────────────────────────────────────────────────────────────
function TopBar({ breadcrumbs = [], right, status }) {
  return (
    <header style={{
      height: 56, borderBottom: '1px solid var(--s-border)',
      padding: '0 24px', display: 'flex', alignItems: 'center', gap: 16,
      flexShrink: 0, background: 'var(--s-bg-1)',
    }}>
      <nav style={{ display: 'flex', alignItems: 'center', gap: 8, fontSize: 13, color: 'var(--s-fg-2)' }}>
        {breadcrumbs.map((b, i) => (
          <React.Fragment key={i}>
            <span style={{ color: i === breadcrumbs.length - 1 ? 'var(--s-fg-1)' : 'var(--s-fg-2)', fontWeight: i === breadcrumbs.length - 1 ? 600 : 400 }}>{b}</span>
            {i < breadcrumbs.length - 1 && <span style={{ color: 'var(--s-fg-3)' }}>/</span>}
          </React.Fragment>
        ))}
        {status && (<><span style={{ width: 1, height: 16, background: 'var(--s-border)', margin: '0 8px' }}/>{status}</>)}
      </nav>
      <div style={{ flex: 1 }}/>
      {right}
    </header>
  );
}

window.Sidebar = Sidebar;
window.TopBar = TopBar;
