// ===== WebAnchor — recreated, semi-interactive app demos =====
// These are original recreations (NOT the real apps, no real data) so visitors
// can play with the idea of each tool without the full thing.

function DemoWin({ theme = 'dark', name, label, children }) {
  return (
    <div className={'demo-win demo-' + theme}>
      <div className="demo-bar">
        <span className="d" /><span className="d" /><span className="d" />
        <span className="nm">{name}</span>
        {label && <span className="lbl">{label}</span>}
      </div>
      <div className="demo-body">{children}</div>
    </div>
  );
}

// ---------- Life OS — daily planner that syncs your calendar ----------
function LifeOSDemo() {
  const [tasks, setTasks] = useState([
    { t: 'Reply to new enquiries', tag: 'Work', time: '09:00', done: true },
    { t: 'Send invoice #0042', tag: 'Work', time: '11:00', done: false },
    { t: 'Prep Friday delivery', tag: 'Ops', time: '14:00', done: false },
    { t: 'Review this week’s leads', tag: 'Work', time: '16:00', done: false },
  ]);
  const [habits, setHabits] = useState([
    { t: 'Gym', on: true }, { t: 'Deep work', on: false }, { t: 'No phone AM', on: false },
  ]);
  const done = tasks.filter(t => t.done).length;
  const days = ['M', 'T', 'W', 'T', 'F', 'S', 'S'];

  return (
    <DemoWin theme="dark" name="Life OS" label="Today">
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', marginBottom: 16 }}>
        <div>
          <div style={{ fontSize: 21, fontWeight: 600, letterSpacing: '-0.02em' }}>Good morning.</div>
          <div style={{ color: 'var(--d-faint)', fontSize: 12, marginTop: 2 }}>Thursday, 12 June</div>
        </div>
        <div style={{ display: 'flex', gap: 5 }}>
          {days.map((d, i) => (
            <div key={i} style={{
              width: 26, height: 32, borderRadius: 7, display: 'flex', flexDirection: 'column',
              alignItems: 'center', justifyContent: 'center', fontSize: 10,
              border: '1px solid var(--d-line)',
              background: i === 3 ? 'var(--d-accent)' : 'transparent',
              color: i === 3 ? '#fff' : 'var(--d-faint)', fontWeight: i === 3 ? 600 : 400,
            }}>{d}</div>
          ))}
        </div>
      </div>

      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', margin: '0 2px 9px' }}>
        <span style={{ fontFamily: 'var(--mono)', fontSize: 10.5, letterSpacing: '0.08em', textTransform: 'uppercase', color: 'var(--d-faint)' }}>Today’s tasks</span>
        <span style={{ fontSize: 12, color: 'var(--d-dim)' }}>{done}/{tasks.length} done</span>
      </div>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
        {tasks.map((t, i) => (
          <div key={i} className="demo-row" onClick={() => setTasks(ts => ts.map((x, j) => j === i ? { ...x, done: !x.done } : x))}>
            <span className={'demo-chk ' + (t.done ? 'on' : '')}>{t.done && <Icon.Check style={{ width: 11, height: 11 }} />}</span>
            <span style={{ flex: 1, textDecoration: t.done ? 'line-through' : 'none', color: t.done ? 'var(--d-faint)' : 'var(--d-text)' }}>{t.t}</span>
            <span className="demo-tag">{t.tag}</span>
            <span style={{ fontFamily: 'var(--mono)', fontSize: 11, color: 'var(--d-faint)', minWidth: 38, textAlign: 'right' }}>{t.time}</span>
          </div>
        ))}
      </div>

      <div style={{ fontFamily: 'var(--mono)', fontSize: 10.5, letterSpacing: '0.08em', textTransform: 'uppercase', color: 'var(--d-faint)', margin: '16px 2px 9px' }}>Daily habits</div>
      <div style={{ display: 'flex', gap: 8 }}>
        {habits.map((h, i) => (
          <span key={i} className={'demo-pill ' + (h.on ? 'on' : '')} onClick={() => setHabits(hs => hs.map((x, j) => j === i ? { ...x, on: !x.on } : x))}>
            {h.on ? '✓ ' : ''}{h.t}
          </span>
        ))}
      </div>
    </DemoWin>
  );
}

// ---------- Stash — drop files in, it sorts the library ----------
function StashDemo() {
  const FILES = [
    { n: 'IMG_2381.png', f: 'Images' },
    { n: 'invoice-0042.pdf', f: 'Documents' },
    { n: 'logo_final.svg', f: 'Design' },
    { n: 'Q2-report.xlsx', f: 'Sheets' },
    { n: 'promo-clip.mp4', f: 'Video' },
    { n: 'meeting-notes.txt', f: 'Documents' },
  ];
  const FOLDERS = ['Images', 'Documents', 'Design', 'Sheets', 'Video'];
  const [sorted, setSorted] = useState(false);
  const count = (f) => sorted ? FILES.filter(x => x.f === f).length : 0;

  return (
    <DemoWin theme="dark" name="Stash" label={sorted ? 'Sorted' : '6 in inbox'}>
      <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 16 }}>
        <div>
          <div style={{ fontFamily: 'var(--mono)', fontSize: 10.5, letterSpacing: '0.08em', textTransform: 'uppercase', color: 'var(--d-faint)', marginBottom: 9 }}>Loose files</div>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 7 }}>
            {FILES.map((file, i) => (
              <div key={i} className={'stash-file ' + (sorted ? 'gone' : '')} style={{
                display: 'flex', alignItems: 'center', gap: 9, padding: '8px 10px', borderRadius: 8,
                border: '1px solid var(--d-line)', background: 'var(--d-panel)', transitionDelay: (sorted ? i * 70 : 0) + 'ms',
              }}>
                <span style={{ width: 8, height: 8, borderRadius: 2, background: 'var(--d-accent-2)', flexShrink: 0 }} />
                <span style={{ fontFamily: 'var(--mono)', fontSize: 11.5, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{file.n}</span>
              </div>
            ))}
          </div>
        </div>
        <div>
          <div style={{ fontFamily: 'var(--mono)', fontSize: 10.5, letterSpacing: '0.08em', textTransform: 'uppercase', color: 'var(--d-faint)', marginBottom: 9 }}>Library</div>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 7 }}>
            {FOLDERS.map((f) => (
              <div key={f} className={'stash-folder ' + (sorted && count(f) ? 'filled' : '')} style={{
                display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '8px 11px',
                borderRadius: 8, border: '1px solid var(--d-line)', background: 'var(--d-panel)',
              }}>
                <span style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
                  <span style={{ width: 9, height: 7, borderRadius: 2, background: 'var(--d-faint)', opacity: 0.6, flexShrink: 0 }} /> {f}
                </span>
                <span style={{ fontFamily: 'var(--mono)', fontSize: 11, color: count(f) ? 'var(--d-accent)' : 'var(--d-faint)', fontWeight: count(f) ? 600 : 400 }}>{count(f)}</span>
              </div>
            ))}
          </div>
        </div>
      </div>
      <div style={{ marginTop: 16, display: 'flex', alignItems: 'center', gap: 12 }}>
        {!sorted ? (
          <button className="btn-accept" style={{ background: 'var(--grad)' }} onClick={() => setSorted(true)}>✦ Sort with AI</button>
        ) : (
          <>
            <span className="badge-ok"><Icon.Check /> Sorted in 0.4s</span>
            <button className="demo-pill" onClick={() => setSorted(false)}>Reset</button>
          </>
        )}
        <span style={{ fontSize: 11.5, color: 'var(--d-faint)' }}>Drop a folder in — it files everything for you.</span>
      </div>
    </DemoWin>
  );
}

// ---------- Waypoint — leads from first hello to paid ----------
function WaypointDemo() {
  const COLS = ['New lead', 'Contacted', 'Quoted', 'Won'];
  const SRC = { Website: 'var(--cyan)', Facebook: 'var(--violet)', Referral: 'var(--pink)' };
  const [leads, setLeads] = useState([
    { n: 'Brewline Coffee', src: 'Website', col: 0, amt: 'R18k' },
    { n: 'Mara Photography', src: 'Referral', col: 0, amt: 'R12k' },
    { n: 'Cape Plumbing', src: 'Facebook', col: 1, amt: 'R32k' },
    { n: 'Harbour Café', src: 'Website', col: 2, amt: 'R26k' },
  ]);
  const advance = (idx) => setLeads(ls => ls.map((l, j) => j === idx ? { ...l, col: Math.min(l.col + 1, 3) } : l));

  return (
    <DemoWin theme="light" name="Waypoint" label="Pipeline">
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 12 }}>
        <span style={{ fontSize: 15, fontWeight: 600, letterSpacing: '-0.02em' }}>Leads</span>
        <span style={{ fontSize: 11.5, color: 'var(--d-faint)' }}>Click a card to move it →</span>
      </div>
      <div className="kanban">
        {COLS.map((c, ci) => (
          <div key={c} className="kanban-col">
            <h6><span>{c}</span><span>{leads.filter(l => l.col === ci).length}</span></h6>
            {leads.map((l, li) => l.col === ci && (
              <div key={li} className="lead-card" onClick={() => advance(li)}>
                <div style={{ fontWeight: 600, fontSize: 12.5, marginBottom: 5 }}>{l.n}</div>
                <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
                  <span style={{ display: 'inline-flex', alignItems: 'center', gap: 5, fontSize: 10.5, color: 'var(--d-faint)' }}>
                    <span style={{ width: 7, height: 7, borderRadius: 999, background: SRC[l.src] }} />{l.src}
                  </span>
                  {ci === 3
                    ? <span className="badge-ok" style={{ fontSize: 10.5 }}><Icon.Check /> {l.amt}</span>
                    : <span style={{ fontFamily: 'var(--mono)', fontSize: 11, color: 'var(--d-dim)' }}>{l.amt}</span>}
                </div>
              </div>
            ))}
          </div>
        ))}
      </div>
      <div style={{ marginTop: 12, fontSize: 11.5, color: 'var(--d-faint)' }}>
        A sign-up on your site or Facebook drops straight into “New lead”.
      </div>
    </DemoWin>
  );
}

// ---------- Trifusion — clients & installers, no middle-man ----------
function TrifusionDemo() {
  const [jobs, setJobs] = useState([
    { id: 'JOB-104', loc: 'Kempton Park', who: 'Korridor', ok: false },
    { id: 'JOB-105', loc: 'Sandton CBD', who: 'Brigade', ok: false },
    { id: 'JOB-106', loc: 'Pretoria East', who: 'Vortex', ok: true },
  ]);
  const pending = jobs.filter(j => !j.ok).length;
  const active = 4 + jobs.filter(j => j.ok).length;
  const accept = (i) => setJobs(js => js.map((j, k) => k === i ? { ...j, ok: true } : j));
  const stats = [['24', 'Total jobs'], [String(active), 'Active'], [String(pending), 'Pending'], ['15', 'Completed']];

  return (
    <DemoWin theme="dark" name="Trifusion Portal" label="Admin">
      <div className="tri-stats">
        {stats.map(([v, l], i) => (
          <div key={i} className="tri-stat">
            <div className="v" style={{ color: i === 2 && pending ? 'var(--warn)' : 'var(--d-text)' }}>{v}</div>
            <div className="l">{l}</div>
          </div>
        ))}
      </div>
      <div style={{ display: 'flex', alignItems: 'center', gap: 8, margin: '16px 2px 9px' }}>
        <span style={{ width: 8, height: 8, borderRadius: 999, background: 'var(--bad)' }} />
        <span style={{ fontFamily: 'var(--mono)', fontSize: 10.5, letterSpacing: '0.08em', textTransform: 'uppercase', color: 'var(--d-faint)' }}>Needs attention</span>
      </div>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
        {jobs.map((j, i) => (
          <div key={j.id} style={{
            display: 'flex', alignItems: 'center', gap: 12, padding: '10px 13px', borderRadius: 10,
            border: '1px solid var(--d-line)', background: 'var(--d-panel)',
          }}>
            <span style={{ fontFamily: 'var(--mono)', fontSize: 11, color: 'var(--d-faint)' }}>{j.id}</span>
            <span style={{ flex: 1 }}><b style={{ fontWeight: 600 }}>{j.loc}</b> <span style={{ color: 'var(--d-faint)' }}>· {j.who}</span></span>
            {j.ok
              ? <span className="badge-ok"><Icon.Check /> Accepted</span>
              : <button className="btn-accept" onClick={() => accept(i)}>Accept job</button>}
          </div>
        ))}
      </div>
      <div style={{ marginTop: 16, textAlign: 'center', fontFamily: 'var(--mono)', fontSize: 9.5, letterSpacing: '0.18em', textTransform: 'uppercase', color: 'var(--d-faint)' }}>
        Powered by WebAnchor
      </div>
    </DemoWin>
  );
}

Object.assign(window, { DemoWin, LifeOSDemo, StashDemo, WaypointDemo, TrifusionDemo });
