// admin-shell.jsx — Layout + shared primitives
const { useState } = React;

// ── Icon system — pure SVG, no external deps ────────────────
const ICON_PATHS = {
  LayoutDashboard: '<rect x="3" y="3" width="7" height="9" rx="1"/><rect x="14" y="3" width="7" height="5" rx="1"/><rect x="14" y="12" width="7" height="9" rx="1"/><rect x="3" y="16" width="7" height="5" rx="1"/>',
  Users:           '<path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M22 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/>',
  ToggleLeft:      '<rect x="2" y="6" width="20" height="12" rx="6"/><circle cx="8" cy="12" r="2"/>',
  BarChart3:       '<path d="M3 3v18h18"/><path d="M18 17V9"/><path d="M13 17V5"/><path d="M8 17v-3"/>',
  CreditCard:      '<rect x="2" y="5" width="20" height="14" rx="2"/><line x1="2" y1="10" x2="22" y2="10"/>',
  Headphones:      '<path d="M3 14h3a2 2 0 0 1 2 2v3a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-7a9 9 0 0 1 18 0v7a2 2 0 0 1-2 2h-1a2 2 0 0 1-2-2v-3a2 2 0 0 1 2-2h3"/>',
  LogOut:          '<path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/><polyline points="16 17 21 12 16 7"/><line x1="21" y1="12" x2="9" y2="12"/>',
  Zap:             '<polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"/>',
  Bell:            '<path d="M6 8a6 6 0 0 1 12 0c0 7 3 9 3 9H3s3-2 3-9"/><path d="M10.3 21a1.94 1.94 0 0 0 3.4 0"/>',
  ChevronRight:    '<path d="m9 18 6-6-6-6"/>',
  ArrowUpRight:    '<path d="M7 7h10v10"/><path d="M7 17 17 7"/>',
  ArrowDownRight:  '<path d="M17 17H7V7"/><path d="m7 17 10-10"/>',
  Search:          '<circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/>',
  X:               '<path d="M18 6 6 18"/><path d="m6 6 12 12"/>',
  Save:            '<path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"/><polyline points="17 21 17 13 7 13 7 21"/><polyline points="7 3 7 8 15 8"/>',
  TrendingDown:    '<polyline points="22 17 13.5 8.5 8.5 13.5 2 7"/><polyline points="16 17 22 17 22 11"/>',
  Settings:        '<circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/>',
  Moon:            '<path d="M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z"/>',
  Sun:             '<circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M6.34 17.66l-1.41 1.41M19.07 4.93l-1.41 1.41"/>',
  Mail:            '<rect width="20" height="16" x="2" y="4" rx="2"/><path d="m22 7-8.97 5.7a1.94 1.94 0 0 1-2.06 0L2 7"/>',
  Plus:            '<path d="M5 12h14"/><path d="M12 5v14"/>',
  Send:            '<line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/>',
  Eye:             '<path d="M2 12s3-7 10-7 10 7 10 7-3 7-10 7-10-7-10-7Z"/><circle cx="12" cy="12" r="3"/>',
  Trash2:          '<path d="M3 6h18"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/><line x1="10" y1="11" x2="10" y2="17"/><line x1="14" y1="11" x2="14" y2="17"/>',
  Inbox:           '<polyline points="22 12 16 12 14 15 10 15 8 12 2 12"/><path d="M5.45 5.11 2 12v6a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-6l-3.45-6.89A2 2 0 0 0 16.76 4H7.24a2 2 0 0 0-1.79 1.11z"/>',
  TrendingUp:      '<polyline points="22 7 13.5 15.5 8.5 10.5 2 17"/><polyline points="16 7 22 7 22 13"/>',
  CheckCircle:     '<path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/>',
  Clock:           '<circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/>',
  Calendar:        '<rect x="3" y="4" width="18" height="18" rx="2" ry="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/>',
  Loader:          '<line x1="12" y1="2" x2="12" y2="6"/><line x1="12" y1="18" x2="12" y2="22"/><line x1="4.93" y1="4.93" x2="7.76" y2="7.76"/><line x1="16.24" y1="16.24" x2="19.07" y2="19.07"/><line x1="2" y1="12" x2="6" y2="12"/><line x1="18" y1="12" x2="22" y2="12"/><line x1="4.93" y1="19.07" x2="7.76" y2="16.24"/><line x1="16.24" y1="7.76" x2="19.07" y2="4.93"/>',
  Image:           '<rect x="3" y="3" width="18" height="18" rx="2" ry="2"/><circle cx="9" cy="9" r="2"/><path d="m21 15-3.086-3.086a2 2 0 0 0-2.828 0L6 21"/>',
  ShieldAlert:     '<path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"/><path d="M12 8v4"/><path d="M12 16h.01"/>',
  GitBranch:       '<line x1="6" y1="3" x2="6" y2="15"/><circle cx="18" cy="6" r="3"/><circle cx="6" cy="18" r="3"/><path d="M18 9a9 9 0 0 1-9 9"/>',
  GripVertical:    '<circle cx="9" cy="6" r="1.2"/><circle cx="9" cy="12" r="1.2"/><circle cx="9" cy="18" r="1.2"/><circle cx="15" cy="6" r="1.2"/><circle cx="15" cy="12" r="1.2"/><circle cx="15" cy="18" r="1.2"/>',
  Workflow:        '<rect width="8" height="8" x="3" y="3" rx="2"/><path d="M7 11v4a2 2 0 0 0 2 2h4"/><rect width="8" height="8" x="13" y="13" rx="2"/>',
  Hourglass:       '<path d="M5 22h14"/><path d="M5 2h14"/><path d="M17 22v-4.172a2 2 0 0 0-.586-1.414L12 12l-4.414 4.414A2 2 0 0 0 7 17.828V22"/><path d="M7 2v4.172a2 2 0 0 0 .586 1.414L12 12l4.414-4.414A2 2 0 0 0 17 6.172V2"/>',
  EyeOff:          '<path d="M9.88 9.88a3 3 0 1 0 4.24 4.24"/><path d="M10.73 5.08A10.43 10.43 0 0 1 12 5c7 0 10 7 10 7a13.16 13.16 0 0 1-1.67 2.68"/><path d="M6.61 6.61A13.526 13.526 0 0 0 2 12s3 7 10 7a9.74 9.74 0 0 0 5.39-1.61"/><line x1="2" y1="2" x2="22" y2="22"/>',
  ChevronDown:     '<path d="m6 9 6 6 6-6"/>',
  Power:           '<path d="M12 2v10"/><path d="M18.4 6.6a9 9 0 1 1-12.77.04"/>',
  Sparkles:        '<path d="m12 3-1.9 5.8a2 2 0 0 1-1.3 1.3L3 12l5.8 1.9a2 2 0 0 1 1.3 1.3L12 21l1.9-5.8a2 2 0 0 1 1.3-1.3L21 12l-5.8-1.9a2 2 0 0 1-1.3-1.3z"/>',
  Rocket:          '<path d="M4.5 16.5c-1.5 1.26-2 5-2 5s3.74-.5 5-2c.71-.84.7-2.13-.09-2.91a2.18 2.18 0 0 0-2.91-.09z"/><path d="m12 15-3-3a22 22 0 0 1 2-3.95A12.88 12.88 0 0 1 22 2c0 2.72-.78 7.5-6 11a22.35 22.35 0 0 1-4 2z"/><path d="M9 12H4s.55-3.03 2-4c1.62-1.08 5 0 5 0"/><path d="M12 15v5s3.03-.55 4-2c1.08-1.62 0-5 0-5"/>',
  FileText:        '<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/><polyline points="10 9 9 9 8 9"/>',
};

function Icon({ name, size = 16, color = 'currentColor', fill = 'none' }) {
  const paths = ICON_PATHS[name] || '';
  return React.createElement('svg', {
    width: size, height: size, viewBox: '0 0 24 24',
    fill, stroke: color, strokeWidth: 2,
    strokeLinecap: 'round', strokeLinejoin: 'round',
    style: { flexShrink: 0, display: 'block' },
    dangerouslySetInnerHTML: { __html: paths },
  });
}

// ── Navigation ───────────────────────────────────────────────
const NAV = [
  { id: 'dashboard', label: 'Overview',    icon: 'LayoutDashboard' },
  { id: 'users',     label: 'Users',       icon: 'Users'           },
  { id: 'features',  label: 'Features',    icon: 'ToggleLeft'      },
  { id: 'costs',     label: 'Costs & P&L', icon: 'BarChart3'       },
  { id: 'billing',   label: 'Billing',     icon: 'CreditCard'      },
  { id: 'support',   label: 'Support',     icon: 'Headphones'      },
  { id: 'newsletter', label: 'Newsletter', icon: 'Mail'            },
  { id: 'blog-images', label: 'Blog Image Generator', icon: 'Image' },
  { id: 'mission-control', label: 'Mission Control', icon: 'Rocket'   },
  { id: 'settings',   label: 'Slack Notifications',  icon: 'Settings' },
];

function Sidebar({ page, setPage }) {
  const { admin } = window.MOCK;
  return (
    <aside style={{ width:212, flexShrink:0, background:'#fff', borderRight:'1px solid #E5E7EB', display:'flex', flexDirection:'column', height:'100%' }}>
      <div style={{ padding:'20px 16px 16px', borderBottom:'1px solid #F3F4F6', display:'flex', alignItems:'center', gap:10 }}>
        <div style={{ width:30, height:30, borderRadius:8, background:'#F97316', display:'flex', alignItems:'center', justifyContent:'center', flexShrink:0 }}>
          <Icon name="Zap" size={14} color="#fff" fill="#fff" />
        </div>
        <div>
          <div style={{ fontSize:13, fontWeight:800, color:'#1A2B3B', lineHeight:1 }}>WiseFunnel</div>
          <div style={{ fontSize:9, fontWeight:700, color:'#9CA3AF', textTransform:'uppercase', letterSpacing:'0.1em', marginTop:2 }}>Admin</div>
        </div>
      </div>
      <nav style={{ flex:1, padding:'8px', display:'flex', flexDirection:'column', gap:1 }}>
        {NAV.map(({ id, label, icon }) => (
          <button key={id} onClick={() => setPage(id)} style={{
            display:'flex', alignItems:'center', gap:9,
            padding:'8px 10px', borderRadius:8, border:'none',
            fontFamily:'inherit', fontSize:13, fontWeight:600,
            cursor:'pointer', transition:'all 0.12s', textAlign:'left',
            background: page===id ? '#FFF7ED' : 'transparent',
            color: page===id ? '#F97316' : '#6B7280',
          }}
          onMouseEnter={e => { if(page!==id){ e.currentTarget.style.background='#F9FAFB'; e.currentTarget.style.color='#1A2B3B'; }}}
          onMouseLeave={e => { if(page!==id){ e.currentTarget.style.background='transparent'; e.currentTarget.style.color='#6B7280'; }}}
          >
            <Icon name={icon} size={15} color={page===id ? '#F97316' : '#6B7280'} />
            {label}
          </button>
        ))}
      </nav>
      <div style={{ padding:'10px 8px', borderTop:'1px solid #F3F4F6' }}>
        <div style={{ display:'flex', alignItems:'center', gap:8, padding:'8px 10px', marginBottom:2 }}>
          <div style={{ width:26, height:26, borderRadius:'50%', background:'#FFF7ED', display:'flex', alignItems:'center', justifyContent:'center', fontSize:11, fontWeight:800, color:'#F97316', flexShrink:0 }}>
            {admin.name[0]}
          </div>
          <div style={{ flex:1, minWidth:0 }}>
            <div style={{ fontSize:12, fontWeight:700, color:'#1A2B3B', overflow:'hidden', textOverflow:'ellipsis', whiteSpace:'nowrap' }}>{admin.name}</div>
            <div style={{ fontSize:10, color:'#9CA3AF', fontWeight:600, textTransform:'capitalize' }}>{admin.role}</div>
          </div>
        </div>
        <button onClick={() => { try { window.AdminAuth && window.AdminAuth.clear(); } catch (e) {} location.reload(); }}
          style={{ display:'flex', alignItems:'center', gap:9, padding:'7px 10px', borderRadius:8, border:'none', fontFamily:'inherit', fontSize:12, fontWeight:600, cursor:'pointer', background:'transparent', color:'#9CA3AF', width:'100%', transition:'all 0.12s' }}
          onMouseEnter={e=>{ e.currentTarget.style.color='#F43F5E'; e.currentTarget.style.background='#FFF1F2'; }}
          onMouseLeave={e=>{ e.currentTarget.style.color='#9CA3AF'; e.currentTarget.style.background='transparent'; }}
        >
          <Icon name="LogOut" size={14} color="#9CA3AF" />Sign out
        </button>
      </div>
    </aside>
  );
}

function MobileHeader({ page }) {
  const cur = NAV.find(n=>n.id===page)||NAV[0];
  return (
    <div className="mobile-header" style={{ padding:'12px 16px', background:'#fff', borderBottom:'1px solid #E5E7EB', alignItems:'center', justifyContent:'space-between', flexShrink:0 }}>
      <div style={{ display:'flex', alignItems:'center', gap:8 }}>
        <div style={{ width:26, height:26, borderRadius:6, background:'#F97316', display:'flex', alignItems:'center', justifyContent:'center' }}>
          <Icon name="Zap" size={12} color="#fff" fill="#fff"/>
        </div>
        <span style={{ fontSize:13, fontWeight:800, color:'#1A2B3B' }}>WiseFunnel</span>
      </div>
      <span style={{ fontSize:13, fontWeight:700, color:'#6B7280' }}>{cur.label}</span>
      <div style={{ width:28, height:28, borderRadius:'50%', background:'#FFF7ED', display:'flex', alignItems:'center', justifyContent:'center', fontSize:11, fontWeight:800, color:'#F97316' }}>P</div>
    </div>
  );
}

function MobileNav({ page, setPage }) {
  return (
    <div className="mobile-nav" style={{ position:'fixed', bottom:0, left:0, right:0, background:'#fff', borderTop:'1px solid #E5E7EB', padding:'6px 0 8px', zIndex:40 }}>
      {NAV.slice(0,5).map(({ id, label, icon }) => (
        <button key={id} onClick={() => setPage(id)} style={{ flex:1, display:'flex', flexDirection:'column', alignItems:'center', gap:3, border:'none', background:'none', cursor:'pointer', padding:'4px 6px', fontFamily:'inherit' }}>
          <Icon name={icon} size={20} color={page===id ? '#F97316' : '#9CA3AF'} />
          <span style={{ fontSize:9, fontWeight:700, color:page===id ? '#F97316' : '#9CA3AF', textTransform:'capitalize' }}>{id==='costs'?'P&L':label}</span>
        </button>
      ))}
    </div>
  );
}

function AdminShell({ page, setPage, children }) {
  return (
    <div style={{ display:'flex', height:'100vh', overflow:'hidden' }} className="admin-root">
      <Sidebar page={page} setPage={setPage} />
      <div style={{ flex:1, display:'flex', flexDirection:'column', minWidth:0, overflow:'hidden', background:'#F9FAFB' }}>
        <MobileHeader page={page} />
        <div className="topbar" style={{ padding:'14px 28px', borderBottom:'1px solid #E5E7EB', background:'#fff', display:'flex', alignItems:'center', justifyContent:'space-between', flexShrink:0 }}>
          <h1 style={{ fontSize:15, fontWeight:800, color:'#1A2B3B' }}>{NAV.find(n=>n.id===page)?.label}</h1>
          <button style={{ width:30, height:30, borderRadius:8, border:'1px solid #E5E7EB', background:'#fff', display:'flex', alignItems:'center', justifyContent:'center', cursor:'pointer' }}>
            <Icon name="Bell" size={14} color="#9CA3AF" />
          </button>
        </div>
        <main style={{ flex:1, overflowY:'auto', padding:24 }} className="page-content">
          {children}
        </main>
      </div>
      <MobileNav page={page} setPage={setPage} />
    </div>
  );
}

// ── Shared primitives ────────────────────────────────────────
function PlanBadge({ plan }) {
  const cfg = { scale:{c:'#10B981',l:'Scale'}, growth:{c:'#3B82F6',l:'Growth'}, lite:{c:'#F59E0B',l:'Lite'}, trial:{c:'#9CA3AF',l:'Trial'} };
  const { c, l } = cfg[plan] || cfg.trial;
  return <span style={{ display:'inline-flex', alignItems:'center', padding:'2px 8px', borderRadius:999, fontSize:10, fontWeight:700, background:c+'18', color:c, whiteSpace:'nowrap' }}>{l}</span>;
}

function StatusDot({ status }) {
  const cfg = { active:{c:'#10B981',l:'Active'}, trialing:{c:'#F59E0B',l:'Trial'}, canceled:{c:'#F43F5E',l:'Canceled'}, past_due:{c:'#F97316',l:'Past Due'} };
  const { c, l } = cfg[status] || { c:'#9CA3AF', l:status };
  return <span style={{ display:'inline-flex', alignItems:'center', gap:5, fontSize:12, fontWeight:600, color:c }}><span style={{ width:6, height:6, borderRadius:'50%', background:c, display:'inline-block' }}/>{l}</span>;
}

function Btn({ children, variant='secondary', onClick, style, disabled }) {
  const base = { display:'inline-flex', alignItems:'center', gap:6, padding:'7px 14px', borderRadius:8, fontFamily:'inherit', fontSize:12, fontWeight:700, cursor:disabled?'not-allowed':'pointer', border:'none', transition:'all 0.12s', opacity:disabled?0.5:1, ...style };
  const v = { primary:{background:'#F97316',color:'#fff'}, secondary:{background:'#F3F4F6',color:'#1A2B3B',border:'1px solid #E5E7EB'}, ghost:{background:'transparent',color:'#6B7280'}, danger:{background:'#FFF1F2',color:'#F43F5E',border:'1px solid #FECDD3'} };
  return <button onClick={onClick} disabled={disabled} style={{ ...base, ...v[variant] }}>{children}</button>;
}

function Tabs({ options, value, onChange }) {
  return (
    <div style={{ display:'flex', gap:2, padding:3, background:'#F3F4F6', borderRadius:9, width:'fit-content' }}>
      {options.map(o => {
        const v = o.value||o; const l = o.label||o;
        return <button key={v} onClick={()=>onChange(v)} style={{ padding:'5px 12px', borderRadius:7, fontFamily:'inherit', fontSize:12, fontWeight:700, border:'none', cursor:'pointer', transition:'all 0.12s', background:value===v?'#fff':'transparent', color:value===v?'#1A2B3B':'#6B7280', boxShadow:value===v?'0 1px 3px rgba(0,0,0,0.08)':'none' }}>{l}</button>;
      })}
    </div>
  );
}

function Toggle({ on, onClick }) {
  return (
    <button onClick={onClick} style={{ width:34, height:19, borderRadius:999, border:'none', cursor:'pointer', background:on?'#F97316':'#D1D5DB', position:'relative', transition:'background 0.2s', flexShrink:0 }}>
      <span style={{ position:'absolute', top:2, left:on?17:2, width:15, height:15, borderRadius:'50%', background:'#fff', transition:'left 0.2s', boxShadow:'0 1px 3px rgba(0,0,0,0.2)' }}/>
    </button>
  );
}

function BarChart({ data, height=110 }) {
  const maxMrr = Math.max(...data.map(d=>d.mrr));
  const n = data.length; const W=300, H=height;
  const barW = (W/n)*0.55; const gap = W/n;
  const bars = data.map((d,i) => {
    const mH=((d.mrr/maxMrr)*(H-20)); const cH=((d.costs/maxMrr)*(H-20));
    const x=i*gap+(gap-barW)/2;
    return `<rect x="${x}" y="${H-16-mH}" width="${barW}" height="${mH}" rx="2" fill="#FFF7ED"/><rect x="${x}" y="${H-16-cH}" width="${barW}" height="${cH}" rx="2" fill="#F97316" opacity="0.75"/><text x="${x+barW/2}" y="${H-2}" text-anchor="middle" font-size="8" fill="#9CA3AF" font-family="Inter,sans-serif">${d.month}</text>`;
  }).join('');
  return React.createElement('svg', { width:'100%', viewBox:`0 0 ${W} ${H}`, preserveAspectRatio:'none', dangerouslySetInnerHTML:{ __html: bars }});
}

Object.assign(window, { AdminShell, PlanBadge, StatusDot, Btn, Tabs, Toggle, BarChart, Icon, NAV });
