@nodestackapp/server 1.3.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +10 -10
  3. package/dist/NodeStack.d.ts +5 -0
  4. package/dist/NodeStack.d.ts.map +1 -1
  5. package/dist/NodeStack.js +19 -1
  6. package/dist/NodeStack.js.map +1 -1
  7. package/dist/admin/AdminUIBundler.d.ts.map +1 -1
  8. package/dist/admin/AdminUIBundler.js +1 -0
  9. package/dist/admin/AdminUIBundler.js.map +1 -1
  10. package/dist/admin/ui/css/analytics.css +68 -11
  11. package/dist/admin/ui/css/main.css +2189 -2015
  12. package/dist/admin/ui/css/rules.css +292 -264
  13. package/dist/admin/ui/css/webhooks.css +34 -10
  14. package/dist/admin/ui/index.html +10434 -9850
  15. package/dist/admin/ui/js/analytics.js +673 -624
  16. package/dist/admin/ui/js/api.js +16 -0
  17. package/dist/admin/ui/js/app.js +9 -18
  18. package/dist/admin/ui/js/apps.js +24 -6
  19. package/dist/admin/ui/js/collections.js +3 -4
  20. package/dist/admin/ui/js/command-palette.js +49 -18
  21. package/dist/admin/ui/js/demo.js +373 -271
  22. package/dist/admin/ui/js/modals.js +14 -14
  23. package/dist/admin/ui/js/plugin-tester.js +6 -6
  24. package/dist/admin/ui/js/plugins.js +31 -21
  25. package/dist/admin/ui/js/records.js +9 -9
  26. package/dist/admin/ui/js/rules.js +1 -1
  27. package/dist/admin/ui/js/theme.js +120 -0
  28. package/dist/admin/ui/js/views.js +632 -714
  29. package/dist/admin/ui/js/webhooks.js +27 -9
  30. package/dist/apps/AppScope.d.ts +2 -0
  31. package/dist/apps/AppScope.d.ts.map +1 -1
  32. package/dist/apps/AppScope.js +7 -3
  33. package/dist/apps/AppScope.js.map +1 -1
  34. package/dist/auth/AuthService.d.ts.map +1 -1
  35. package/dist/auth/AuthService.js +36 -9
  36. package/dist/auth/AuthService.js.map +1 -1
  37. package/dist/cli/cli.d.ts.map +1 -1
  38. package/dist/cli/cli.js +65 -1
  39. package/dist/cli/cli.js.map +1 -1
  40. package/dist/core/config/ConfigService.d.ts +1 -0
  41. package/dist/core/config/ConfigService.d.ts.map +1 -1
  42. package/dist/core/config/ConfigService.js +4 -1
  43. package/dist/core/config/ConfigService.js.map +1 -1
  44. package/dist/core/container/Tokens.d.ts +2 -0
  45. package/dist/core/container/Tokens.d.ts.map +1 -1
  46. package/dist/core/container/Tokens.js +2 -0
  47. package/dist/core/container/Tokens.js.map +1 -1
  48. package/dist/database/migrations/SystemMigrations.js +1 -1
  49. package/dist/database/migrations/SystemMigrations.js.map +1 -1
  50. package/dist/demo/DemoService.d.ts +15 -103
  51. package/dist/demo/DemoService.d.ts.map +1 -1
  52. package/dist/demo/DemoService.js +14 -419
  53. package/dist/demo/DemoService.js.map +1 -1
  54. package/dist/http/HttpServer.d.ts +3 -1
  55. package/dist/http/HttpServer.d.ts.map +1 -1
  56. package/dist/http/HttpServer.js +43 -8
  57. package/dist/http/HttpServer.js.map +1 -1
  58. package/dist/http/controllers/DemoController.d.ts.map +1 -1
  59. package/dist/http/controllers/DemoController.js +1 -0
  60. package/dist/http/controllers/DemoController.js.map +1 -1
  61. package/dist/http/controllers/HealthController.js +2 -2
  62. package/dist/http/controllers/SnapshotController.d.ts +58 -0
  63. package/dist/http/controllers/SnapshotController.d.ts.map +1 -0
  64. package/dist/http/controllers/SnapshotController.js +79 -0
  65. package/dist/http/controllers/SnapshotController.js.map +1 -0
  66. package/dist/index.d.ts +4 -1
  67. package/dist/index.d.ts.map +1 -1
  68. package/dist/index.js +7 -1
  69. package/dist/index.js.map +1 -1
  70. package/dist/records/QueryFilterParser.d.ts.map +1 -1
  71. package/dist/records/QueryFilterParser.js +2 -1
  72. package/dist/records/QueryFilterParser.js.map +1 -1
  73. package/dist/records/RecordService.d.ts.map +1 -1
  74. package/dist/records/RecordService.js +32 -14
  75. package/dist/records/RecordService.js.map +1 -1
  76. package/dist/rules/RuleEngine.d.ts +14 -0
  77. package/dist/rules/RuleEngine.d.ts.map +1 -1
  78. package/dist/rules/RuleEngine.js +144 -1
  79. package/dist/rules/RuleEngine.js.map +1 -1
  80. package/dist/schema/OpenApiGenerator.js +3 -3
  81. package/dist/snapshots/SnapshotService.d.ts +75 -0
  82. package/dist/snapshots/SnapshotService.d.ts.map +1 -0
  83. package/dist/snapshots/SnapshotService.js +775 -0
  84. package/dist/snapshots/SnapshotService.js.map +1 -0
  85. package/dist/snapshots/types.d.ts +63 -0
  86. package/dist/snapshots/types.d.ts.map +1 -0
  87. package/dist/snapshots/types.js +5 -0
  88. package/dist/snapshots/types.js.map +1 -0
  89. package/dist/templates/definitions/ecommerce.js +13 -13
  90. package/dist/templates/definitions/ecommerce.js.map +1 -1
  91. package/dist/webhooks/WebhookService.js +1 -1
  92. package/package.json +88 -88
@@ -1,11 +1,16 @@
1
- // Demo State Management ("Snapshot Demo State" & "Reset Demo Data")
1
+ // Application Snapshots & State Management ("Take Snapshot", "Restore Checkpoint", "10-Snapshot FIFO Retention")
2
2
 
3
3
  async function fetchDemoStatus() {
4
4
  try {
5
- const res = await api('/api/demo/status');
6
- state.demo = res || { hasSnapshot: false, snapshot: null };
5
+ const res = await api('/api/snapshots');
6
+ state.demo = res || { hasSnapshot: false, snapshots: [], total: 0, maxLimit: 10, snapshot: null, activeSnapshot: null };
7
7
  } catch {
8
- state.demo = { hasSnapshot: false, snapshot: null };
8
+ try {
9
+ const res = await api('/api/demo/status');
10
+ state.demo = res || { hasSnapshot: false, snapshots: [], total: 0, maxLimit: 10, snapshot: null, activeSnapshot: null };
11
+ } catch {
12
+ state.demo = { hasSnapshot: false, snapshots: [], total: 0, maxLimit: 10, snapshot: null, activeSnapshot: null };
13
+ }
9
14
  }
10
15
 
11
16
  updateDemoSidebarDot();
@@ -26,9 +31,10 @@ function updateDemoSidebarDot() {
26
31
  const isModified = Boolean(state.demo.liveStats?.drift?.isModified);
27
32
  dot.style.display = 'inline-block';
28
33
  dot.style.background = isModified ? '#f59e0b' : '#10b981';
34
+ const active = state.demo.activeSnapshot || state.demo.snapshot;
29
35
  dot.title = isModified
30
- ? `Demo Snapshot: changes detected (${state.demo.liveStats?.drift?.recordsDelta >= 0 ? '+' : ''}${state.demo.liveStats?.drift?.recordsDelta} records)`
31
- : `Demo Snapshot: clean baseline (${state.demo.snapshot?.name})`;
36
+ ? `Snapshots (${state.demo.total || 1}): live data modified (${state.demo.liveStats?.drift?.recordsDelta >= 0 ? '+' : ''}${state.demo.liveStats?.drift?.recordsDelta} records)`
37
+ : `Snapshots (${state.demo.total || 1}): clean baseline (${active?.name || 'Active'})`;
32
38
  } else {
33
39
  dot.style.display = 'none';
34
40
  }
@@ -42,212 +48,193 @@ function renderSnapshotsView() {
42
48
  const main = document.getElementById('page-view-snapshots') || document.getElementById('main-body');
43
49
  if (!main) return;
44
50
 
45
- const hasSnap = Boolean(state.demo && state.demo.hasSnapshot && state.demo.snapshot);
46
- const snap = hasSnap ? state.demo.snapshot : null;
47
- const drift = state.demo?.liveStats?.drift;
51
+ const demoState = state.demo || {};
52
+ const snapshots = Array.isArray(demoState.snapshots) ? demoState.snapshots : (demoState.snapshot ? [demoState.snapshot] : []);
53
+ const total = demoState.total || snapshots.length;
54
+ const maxLimit = demoState.maxLimit || 10;
55
+ const activeSnap = demoState.activeSnapshot || demoState.snapshot || (snapshots.length > 0 ? snapshots[snapshots.length - 1] : null);
56
+ const drift = demoState.liveStats?.drift;
48
57
  const isModified = Boolean(drift?.isModified);
49
58
 
50
- const snapTime = snap
51
- ? new Date(snap.createdAt).toLocaleString([], {
52
- dateStyle: 'medium',
53
- timeStyle: 'short',
54
- })
55
- : '';
59
+ // 1. Top Header Bar
60
+ const headerHtml = `
61
+ <div style="display:flex; align-items:center; justify-content:space-between; flex-wrap:wrap; gap:1rem; padding-bottom:0.75rem; border-bottom:1px solid var(--border-subtle);">
62
+ <div>
63
+ <div style="display:flex; align-items:center; gap:10px;">
64
+ <h2 style="font-size:18px; font-weight:800; color: var(--text-heading); margin:0;">Application Snapshots</h2>
65
+ <span class="badge" class="badge badge-primary" style="font-size:11.5px; font-weight:600; padding:2px 8px;">
66
+ ${total} / ${maxLimit} Used
67
+ </span>
68
+ </div>
69
+ <p style="font-size:12.5px; color:var(--text-muted); margin:4px 0 0 0;">
70
+ Point-in-time full application checkpoints (SQLite database & storage files). Jump between any checkpoints without losing history.
71
+ </p>
72
+ </div>
56
73
 
57
- let heroHtml = '';
58
- let statsHtml = '';
59
- let collectionsTableHtml = '';
74
+ <div style="display:flex; align-items:center; gap:8px;">
75
+ <button class="btn btn-primary" onclick="openCreateSnapshotModal()" style="font-size:12.5px; padding:6px 14px; font-weight:600;">
76
+ + Create Snapshot
77
+ </button>
78
+ ${snapshots.length > 0 ? `
79
+ <button class="btn btn-secondary btn-sm" onclick="openClearAllSnapshotsModal()" style="font-size:11.5px; padding:6px 10px; color:#f87171;">
80
+ Clear All
81
+ </button>
82
+ ` : ''}
83
+ </div>
84
+ </div>
85
+ `;
60
86
 
61
- if (hasSnap && snap) {
87
+ // 2. Active Baseline Drift Card (clean left accent, no tacky icon box)
88
+ let activeCardHtml = '';
89
+ if (activeSnap) {
90
+ const snapTime = new Date(activeSnap.createdAt).toLocaleString([], { dateStyle: 'medium', timeStyle: 'short' });
62
91
  const recDelta = drift?.recordsDelta !== undefined
63
92
  ? (drift.recordsDelta >= 0 ? `+${drift.recordsDelta}` : `${drift.recordsDelta}`)
64
93
  : '0';
65
-
66
94
  const driftLabel = (drift?.recordsDelta !== 0 && drift?.recordsDelta !== undefined)
67
95
  ? `${recDelta} records`
68
96
  : 'Content modified';
69
97
 
70
98
  const statusBadge = isModified
71
- ? `<span class="badge badge-post" style="font-size:11.5px; background:rgba(245, 158, 11, 0.15); border-color:rgba(245, 158, 11, 0.4); color:#fbbf24; padding:3px 9px;">⚠️ Live Data Modified (${driftLabel})</span>`
72
- : `<span class="badge badge-get" style="font-size:11.5px; background:rgba(16, 185, 129, 0.15); border-color:rgba(16, 185, 129, 0.4); color:#34d399; padding:3px 9px;">● Pristine Clean Baseline</span>`;
73
-
74
- heroHtml = `
75
- <div style="background: linear-gradient(135deg, rgba(59, 130, 246, 0.08) 0%, rgba(16, 185, 129, 0.07) 100%); border: 1px solid rgba(59, 130, 246, 0.3); border-radius: 12px; padding: 1.5rem; display: flex; flex-direction: column; gap: 1rem;">
76
- <div style="display: flex; align-items: flex-start; justify-content: space-between; flex-wrap: wrap; gap: 1rem;">
77
- <div style="display: flex; align-items: flex-start; gap: 1rem;">
78
- <div style="width: 46px; height: 46px; border-radius: 12px; background: #0c0e14; border: 1px solid rgba(59, 130, 246, 0.4); display: flex; align-items: center; justify-content: center; flex-shrink: 0; box-shadow: 0 4px 12px rgba(59, 130, 246, 0.2);">
79
- <span style="font-size: 24px;">📸</span>
80
- </div>
81
- <div>
82
- <div style="display: flex; align-items: center; gap: 8px; flex-wrap: wrap;">
83
- <h2 style="font-size: 18px; font-weight: 800; color: #fff; margin: 0;">${snap.name}</h2>
84
- ${statusBadge}
85
- </div>
86
- <div style="font-size: 12.5px; color: var(--text-muted); margin-top: 4px;">
87
- Frozen on <strong style="color: #cbd5e1;">${snapTime}</strong> • ${snap.description || 'Clean showcase baseline'}
88
- </div>
89
- </div>
90
- </div>
99
+ ? `<span class="badge badge-warning" style="font-size:11px; padding:2px 8px;">Live Data Modified (${driftLabel})</span>`
100
+ : `<span class="badge badge-success" style="font-size:11px; padding:2px 8px;">Live Matches Baseline</span>`;
91
101
 
102
+ activeCardHtml = `
103
+ <div style="background: var(--bg-card); border: 1px solid rgba(59, 130, 246, 0.35); border-left: 3px solid #3b82f6; border-radius: 8px; padding: 1.1rem 1.25rem; display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 1rem;">
104
+ <div>
92
105
  <div style="display: flex; align-items: center; gap: 8px; flex-wrap: wrap;">
93
- <button class="btn btn-warning" onclick="openResetDemoModal()" style="font-size: 12.5px; padding: 6px 14px; font-weight: 700; box-shadow: 0 4px 14px rgba(245, 158, 11, 0.2);">
94
- ↺ Reset Demo Data
95
- </button>
96
- <button class="btn btn-secondary" onclick="openSnapshotDemoModal()" style="font-size: 12.5px; padding: 6px 12px;">
97
- 📸 Update Snapshot
98
- </button>
99
- <button class="btn btn-danger btn-sm" onclick="openClearDemoModal()" style="font-size: 11px; padding: 6px 10px;">
100
- Clear
101
- </button>
106
+ <span style="font-size:11px; font-weight:700; text-transform:uppercase; color:var(--text-muted); letter-spacing:0.06em;">Active Restore Baseline:</span>
107
+ <strong style="font-size: 15px; color: var(--text-heading);">${activeSnap.name}</strong>
108
+ ${statusBadge}
109
+ </div>
110
+ <div style="font-size: 12px; color: var(--text-muted); margin-top: 4px;">
111
+ Captured on <strong style="color: #cbd5e1;">${snapTime}</strong> • ${activeSnap.totalRecords} records across ${activeSnap.totalCollections} collections
102
112
  </div>
103
- </div>
104
- </div>
105
- `;
106
-
107
- statsHtml = `
108
- <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 1rem;">
109
- <!-- KPI 1: Preserved Records -->
110
- <div style="background: var(--bg-card); border: 1px solid var(--border-subtle); border-radius: 8px; padding: 1.1rem 1.25rem;">
111
- <div style="font-size: 11px; font-weight: 700; text-transform: uppercase; color: var(--text-muted); letter-spacing: 0.05em;">Preserved Baseline</div>
112
- <div style="font-size: 22px; font-weight: 800; color: #60a5fa; margin-top: 4px;">${snap.totalRecords.toLocaleString()}</div>
113
- <div style="font-size: 11px; color: var(--text-muted); margin-top: 2px;">Records across ${snap.totalCollections} collections</div>
114
- </div>
115
-
116
- <!-- KPI 2: Uploaded Files -->
117
- <div style="background: var(--bg-card); border: 1px solid var(--border-subtle); border-radius: 8px; padding: 1.1rem 1.25rem;">
118
- <div style="font-size: 11px; font-weight: 700; text-transform: uppercase; color: var(--text-muted); letter-spacing: 0.05em;">Storage & Media</div>
119
- <div style="font-size: 22px; font-weight: 800; color: #c084fc; margin-top: 4px;">${snap.totalFiles} Files</div>
120
- <div style="font-size: 11px; color: var(--text-muted); margin-top: 2px;">${(snap.storageSizeBytes / 1024).toFixed(1)} KB preserved assets</div>
121
113
  </div>
122
114
 
123
- <!-- KPI 3: Live Drift Status -->
124
- <div style="background: var(--bg-card); border: 1px solid var(--border-subtle); border-radius: 8px; padding: 1.1rem 1.25rem;">
125
- <div style="font-size: 11px; font-weight: 700; text-transform: uppercase; color: var(--text-muted); letter-spacing: 0.05em;">Live Drift Status</div>
126
- <div style="font-size: 22px; font-weight: 800; color: ${isModified ? '#fbbf24' : '#34d399'}; margin-top: 4px;">
127
- ${isModified ? (drift?.recordsDelta !== 0 ? `${recDelta} Changes` : 'Modified') : '0 Changes'}
128
- </div>
129
- <div style="font-size: 11px; color: var(--text-muted); margin-top: 2px;">
130
- ${isModified ? 'Data modified during evaluations' : 'Exact match with baseline'}
131
- </div>
115
+ <div>
116
+ <button class="btn btn-warning btn-sm" onclick="openRestoreSnapshotModal('${activeSnap.id}')" style="font-weight:700; padding:6px 12px; font-size:12px;">
117
+ Restore Baseline
118
+ </button>
132
119
  </div>
133
120
  </div>
134
121
  `;
122
+ }
135
123
 
136
- // Collections Breakdown Table
137
- collectionsTableHtml = `
138
- <div style="background: var(--bg-card); border: 1px solid var(--border-subtle); border-radius: 10px; overflow: hidden;">
139
- <div style="padding: 1rem 1.25rem; border-bottom: 1px solid var(--border-subtle); display: flex; align-items: center; justify-content: space-between;">
140
- <div style="display: flex; align-items: center; gap: 8px;">
141
- <span style="font-size: 16px;">📂</span>
142
- <h3 style="font-size: 14px; font-weight: 700; color: #fff; margin: 0;">Preserved Collections Scope</h3>
143
- <span class="badge badge-post" style="font-size: 11px;">${snap.collections.length} Tables</span>
144
- </div>
124
+ // 3. Snapshots Timeline / History List (clean minimalist rows, no tacky icons)
125
+ let listHtml = '';
126
+ if (snapshots.length > 0) {
127
+ // Reverse chronologically so newest is at the top
128
+ const displayList = [...snapshots].reverse();
129
+
130
+ listHtml = `
131
+ <div style="display:flex; flex-direction:column; gap:0.85rem;">
132
+ <div style="display:flex; align-items:center; justify-content:space-between; flex-wrap:wrap; gap:0.5rem;">
133
+ <h3 style="font-size:13.5px; font-weight:700; color: var(--text-heading); margin:0;">
134
+ Saved Checkpoints (${snapshots.length} of ${maxLimit})
135
+ </h3>
136
+ <span style="font-size:11.5px; color:var(--text-muted);">
137
+ Oldest snapshot is automatically pruned when taking snapshot #11 after confirmation.
138
+ </span>
145
139
  </div>
146
- <div class="table-container" style="border: none; border-radius: 0;">
147
- <table>
148
- <thead>
149
- <tr>
150
- <th>Collection</th>
151
- <th>Type</th>
152
- <th>Fields</th>
153
- <th>Baseline Records</th>
154
- <th>Current Live</th>
155
- <th>Status</th>
156
- <th>Action</th>
157
- </tr>
158
- </thead>
159
- <tbody>
160
- ${snap.collections.map((c) => {
161
- const liveCol = state.collections.find((sc) => sc.name === c.name);
162
- const liveCountRow = document.getElementById(`col-count-${c.name}`);
163
- const currentCount = liveCol ? (liveCountRow ? parseInt(liveCountRow.textContent || '0', 10) : c.recordsCount) : 0;
164
- const isTableModified = currentCount !== c.recordsCount;
165
-
166
- return `
167
- <tr>
168
- <td><strong style="color: #fff; font-family: var(--font-mono);">${c.name}</strong></td>
169
- <td><span class="collection-badge">${c.type}</span></td>
170
- <td style="color: var(--text-muted); font-size: 12px;">${c.schemaFieldsCount} fields</td>
171
- <td><span style="font-weight: 700; color: #60a5fa;">${c.recordsCount}</span></td>
172
- <td><span style="font-weight: 700; color: #fff;">${currentCount}</span></td>
173
- <td>
174
- ${isTableModified
175
- ? `<span class="badge badge-post" style="font-size: 10.5px;">Modified</span>`
176
- : `<span class="badge badge-get" style="font-size: 10.5px;">Matches</span>`}
177
- </td>
178
- <td>
179
- <button class="btn btn-secondary btn-sm" onclick="selectCollection('${c.name}')" style="padding: 2px 7px; font-size: 11px;">
180
- Browse ➔
181
- </button>
182
- </td>
183
- </tr>
184
- `;
185
- }).join('')}
186
- </tbody>
187
- </table>
140
+
141
+ <div style="display:grid; grid-template-columns:1fr; gap:0.65rem;">
142
+ ${displayList.map((snap) => {
143
+ const isSnapActive = activeSnap && snap.id === activeSnap.id;
144
+ const snapTime = new Date(snap.createdAt).toLocaleString([], { dateStyle: 'medium', timeStyle: 'short' });
145
+ const storageKb = (snap.storageSizeBytes / 1024).toFixed(1);
146
+ const dbKb = (snap.dbSizeBytes / 1024).toFixed(1);
147
+
148
+ return `
149
+ <div style="background:var(--bg-card); border:1px solid ${isSnapActive ? 'rgba(59, 130, 246, 0.45)' : 'var(--border-subtle)'}; border-left:${isSnapActive ? '3px solid #3b82f6' : '1px solid var(--border-subtle)'}; border-radius:8px; padding:1rem 1.25rem; display:flex; align-items:center; justify-content:space-between; flex-wrap:wrap; gap:1rem; transition:border-color 0.15s ease;">
150
+ <div style="flex:1; min-width:240px;">
151
+ <div style="display:flex; align-items:center; gap:8px; flex-wrap:wrap;">
152
+ <h4 style="font-size:14px; font-weight:700; color: var(--text-heading); margin:0;">${snap.name}</h4>
153
+ ${isSnapActive ? `<span class="badge badge-get" style="font-size:10px; padding:2px 6px;">Active Baseline</span>` : ''}
154
+ <code style="font-size:10.5px; color:var(--text-muted); background:var(--bg-input); padding:2px 6px; border-radius:4px; font-family:var(--font-mono);">${snap.id}</code>
155
+ </div>
156
+ <div style="font-size:12px; color:var(--text-muted); margin-top:3px;">
157
+ ${snap.description ? `${snap.description} • ` : ''}Created on <strong style="color:#cbd5e1;">${snapTime}</strong>
158
+ </div>
159
+
160
+ <div style="display:flex; align-items:center; gap:8px; flex-wrap:wrap; margin-top:8px; font-size:11.5px; color:var(--text-muted);">
161
+ <span><strong style="color:#60a5fa;">${snap.totalRecords}</strong> records</span>
162
+ <span style="opacity:0.35;">•</span>
163
+ <span><strong style="color: var(--text-heading);">${snap.totalCollections}</strong> collections</span>
164
+ <span style="opacity:0.35;">•</span>
165
+ <span><strong style="color:#cbd5e1;">${snap.totalFiles}</strong> files (${storageKb} KB)</span>
166
+ <span style="opacity:0.35;">•</span>
167
+ <span><strong style="color:#cbd5e1;">${dbKb} KB</strong> SQLite</span>
168
+ </div>
169
+ </div>
170
+
171
+ <div style="display:flex; align-items:center; gap:8px;">
172
+ <button class="btn btn-secondary btn-sm" onclick="openRestoreSnapshotModal('${snap.id}')" style="font-weight:600; font-size:11.5px; padding:5px 12px;">
173
+ Restore
174
+ </button>
175
+ <button class="btn btn-secondary btn-sm" onclick="deleteSnapshot('${snap.id}')" title="Delete snapshot" style="font-size:11px; padding:5px 10px; color:#f87171;">
176
+ Delete
177
+ </button>
178
+ </div>
179
+ </div>
180
+ `;
181
+ }).join('')}
188
182
  </div>
189
183
  </div>
190
184
  `;
191
185
  } else {
192
- // Empty state
193
- heroHtml = `
194
- <div style="background: var(--bg-card); border: 1px dashed rgba(59, 130, 246, 0.35); border-radius: 12px; padding: 2.5rem 1.5rem; text-align: center; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1rem;">
195
- <div style="width: 56px; height: 56px; border-radius: 16px; background: rgba(59, 130, 246, 0.1); border: 1px solid rgba(59, 130, 246, 0.3); display: flex; align-items: center; justify-content: center;">
196
- <span style="font-size: 28px;">📸</span>
197
- </div>
198
- <div style="max-width: 520px;">
199
- <h2 style="font-size: 18px; font-weight: 800; color: #fff; margin-bottom: 6px;">No Demo Baseline Saved Yet</h2>
200
- <p style="font-size: 13px; color: var(--text-muted); line-height: 1.5; margin: 0;">
201
- Freeze your current database tables and file assets as the clean baseline.
202
- Let prospects, evaluators, or investors test and create dummy records freely, then click <strong>"Reset Demo Data"</strong> anytime to restore your exact clean state before the next presentation.
203
- </p>
204
- </div>
205
- <button class="btn btn-primary" onclick="openSnapshotDemoModal()" style="font-size: 13px; padding: 7px 18px; margin-top: 0.5rem; display: inline-flex; align-items: center; gap: 6px;">
206
- <span>📸 Freeze Clean Demo State Now</span>
186
+ // Empty State (no tacky icons)
187
+ listHtml = `
188
+ <div style="background: var(--bg-card); border: 1px dashed var(--border-subtle); border-radius: 8px; padding: 2.5rem 1.5rem; text-align: center; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0.75rem;">
189
+ <h2 style="font-size: 16px; font-weight: 700; color: var(--text-heading); margin: 0;">No Snapshots Saved Yet</h2>
190
+ <p style="font-size: 13px; color: var(--text-muted); line-height: 1.5; margin: 0; max-width: 480px;">
191
+ Capture full application state snapshots of your database and storage files. Restore from any point in time without losing newer checkpoints.
192
+ </p>
193
+ <button class="btn btn-primary" onclick="openCreateSnapshotModal()" style="font-size: 12.5px; padding: 6px 16px; margin-top: 0.5rem;">
194
+ + Create First Snapshot
207
195
  </button>
208
196
  </div>
209
197
  `;
210
198
  }
211
199
 
212
- // Presentation Guide Card
200
+ // 4. Feature Guide Card (clean and structured)
213
201
  const guideHtml = `
214
- <div style="background: var(--bg-card); border: 1px solid var(--border-subtle); border-radius: 10px; padding: 1.25rem;">
215
- <h3 style="font-size: 14px; font-weight: 700; color: #fff; margin-bottom: 0.75rem; display: flex; align-items: center; gap: 6px;">
216
- <span>🎯</span>
217
- <span>How Live Demo State Works</span>
202
+ <div style="background: var(--bg-card); border: 1px solid var(--border-subtle); border-radius: 8px; padding: 1.25rem;">
203
+ <h3 style="font-size: 13.5px; font-weight: 700; color: var(--text-heading); margin-bottom: 0.75rem;">
204
+ How Application Snapshots Work
218
205
  </h3>
219
- <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 1rem;">
220
- <div style="background: var(--bg-input); border: 1px solid var(--border-subtle); border-radius: 8px; padding: 1rem;">
221
- <div style="font-size: 12px; font-weight: 700; color: #60a5fa; margin-bottom: 4px;">1. Freeze Clean Baseline</div>
206
+ <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 1rem;">
207
+ <div style="background: var(--bg-input); border: 1px solid var(--border-subtle); border-radius: 6px; padding: 0.85rem 1rem;">
208
+ <div style="font-size: 12px; font-weight: 700; color: #60a5fa; margin-bottom: 4px;">1. Full State Checkpoints</div>
222
209
  <div style="font-size: 12px; color: var(--text-muted); line-height: 1.4;">
223
- Pre-seed sample products, deals, or articles. Click <strong>Snapshot Demo State</strong> to freeze transactional SQLite state and uploaded storage assets.
210
+ Captures atomic SQLite WAL database state and uploaded file assets into an independent, self-contained backup.
224
211
  </div>
225
212
  </div>
226
- <div style="background: var(--bg-input); border: 1px solid var(--border-subtle); border-radius: 8px; padding: 1rem;">
227
- <div style="font-size: 12px; font-weight: 700; color: #fbbf24; margin-bottom: 4px;">2. Present & Test Freely</div>
213
+ <div style="background: var(--bg-input); border: 1px solid var(--border-subtle); border-radius: 6px; padding: 0.85rem 1rem;">
214
+ <div style="font-size: 12px; font-weight: 700; color: #34d399; margin-bottom: 4px;">2. Non-Destructive Restore</div>
228
215
  <div style="font-size: 12px; color: var(--text-muted); line-height: 1.4;">
229
- Let evaluators create test rows, delete records, or modify fields during live pitches. NodeStack tracks modifications in real time.
216
+ Restoring an earlier snapshot never destroys newer checkpoints. Jump backward and forward freely across your history.
230
217
  </div>
231
218
  </div>
232
- <div style="background: var(--bg-input); border: 1px solid var(--border-subtle); border-radius: 8px; padding: 1rem;">
233
- <div style="font-size: 12px; font-weight: 700; color: #34d399; margin-bottom: 4px;">3. 1-Click Instant Restore</div>
219
+ <div style="background: var(--bg-input); border: 1px solid var(--border-subtle); border-radius: 6px; padding: 0.85rem 1rem;">
220
+ <div style="font-size: 12px; font-weight: 700; color: #fbbf24; margin-bottom: 4px;">3. 10-Snapshot Rolling Cap</div>
234
221
  <div style="font-size: 12px; color: var(--text-muted); line-height: 1.4;">
235
- Click <strong>Reset Demo Data</strong> before your next meeting. In &lt;50ms, all test changes are wiped and the pristine baseline is restored.
222
+ Stores up to 10 snapshots. If the limit is reached, you are prompted with a confirmation before the oldest snapshot is purged.
236
223
  </div>
237
224
  </div>
238
225
  </div>
239
226
  <div style="margin-top: 1rem; padding-top: 0.75rem; border-top: 1px solid var(--border-subtle); display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 0.5rem; font-size: 11.5px; color: var(--text-muted);">
240
- <span>CLI Access: <code style="color: #60a5fa;">npx nodestack demo reset</code></span>
241
- <span>Client SDK: <code style="color: #60a5fa;">await client.demo.reset()</code></span>
227
+ <span>CLI: <code style="color: #60a5fa;">npx nodestack snapshots list</code></span>
228
+ <span>SDK: <code style="color: #60a5fa;">await client.snapshots.create({ name: 'Before migration' })</code></span>
242
229
  </div>
243
230
  </div>
244
231
  `;
245
232
 
246
233
  main.innerHTML = `
247
234
  <div style="max-width: 1040px; margin: 0 auto; display: flex; flex-direction: column; gap: 1.25rem; padding: 1rem 0;">
248
- ${heroHtml}
249
- ${statsHtml}
250
- ${collectionsTableHtml}
235
+ ${headerHtml}
236
+ ${activeCardHtml}
237
+ ${listHtml}
251
238
  ${guideHtml}
252
239
  </div>
253
240
  `;
@@ -257,36 +244,33 @@ function updateDemoHomeCardUI() {
257
244
  const card = document.getElementById('demo-state-home-card');
258
245
  if (!card) return;
259
246
 
260
- if (state.demo && state.demo.hasSnapshot && state.demo.snapshot) {
261
- const snap = state.demo.snapshot;
247
+ const total = state.demo?.total || (state.demo?.snapshots?.length) || (state.demo?.hasSnapshot ? 1 : 0);
248
+ const active = state.demo?.activeSnapshot || state.demo?.snapshot;
249
+
250
+ if (state.demo && state.demo.hasSnapshot && active) {
262
251
  const drift = state.demo.liveStats?.drift;
263
252
  const isModified = Boolean(drift?.isModified);
264
-
265
253
  const recDelta = drift?.recordsDelta !== undefined
266
254
  ? (drift.recordsDelta >= 0 ? `+${drift.recordsDelta}` : `${drift.recordsDelta}`)
267
255
  : '0';
268
256
 
269
- const driftLabel = (drift?.recordsDelta !== 0 && drift?.recordsDelta !== undefined)
270
- ? `${recDelta} records`
271
- : 'Content modified';
272
-
273
257
  const statusPill = isModified
274
- ? `<span class="badge badge-post" style="font-size:10.5px;">⚠️ Modified (${driftLabel})</span>`
275
- : `<span class="badge badge-get" style="font-size:10.5px;">● Clean Baseline</span>`;
258
+ ? `<span class="badge badge-post" style="font-size:10.5px;">Live Data Modified (${recDelta} recs)</span>`
259
+ : `<span class="badge badge-get" style="font-size:10.5px;">Clean Baseline</span>`;
276
260
 
277
261
  card.innerHTML = `
278
262
  <div style="display:flex; align-items:center; justify-content:space-between; flex-wrap:wrap; gap:0.5rem;">
279
263
  <div style="display:flex; align-items:center; gap:8px;">
280
- <span style="font-size:16px;">📸</span>
281
- <strong style="color:#fff; font-size:13px;">Demo Snapshot: ${snap.name}</strong>
264
+ <strong style="color: var(--text-heading); font-size:13px;">Snapshot: ${active.name}</strong>
265
+ <span class="badge badge-primary" style="font-size:10px;">${total}/10 saved</span>
282
266
  ${statusPill}
283
267
  </div>
284
268
  <div style="display:flex; align-items:center; gap:6px;">
285
- <button class="btn btn-warning btn-sm" onclick="openResetDemoModal()" style="font-weight:700; padding:3px 9px; font-size:11px;">
286
- ↺ Reset Demo
269
+ <button class="btn btn-warning btn-sm" onclick="openRestoreSnapshotModal('${active.id}')" style="font-weight:700; padding:3px 9px; font-size:11px;">
270
+ Restore
287
271
  </button>
288
272
  <button class="btn btn-secondary btn-sm" onclick="selectNav('snapshots')" style="padding:3px 9px; font-size:11px;">
289
- Manage Snapshots ➔
273
+ Manage Snapshots →
290
274
  </button>
291
275
  </div>
292
276
  </div>
@@ -294,13 +278,12 @@ function updateDemoHomeCardUI() {
294
278
  } else {
295
279
  card.innerHTML = `
296
280
  <div style="display:flex; align-items:center; justify-content:space-between; flex-wrap:wrap; gap:0.5rem;">
297
- <div style="display:flex; align-items:center; gap:8px;">
298
- <span style="font-size:16px;">📸</span>
299
- <span style="color:#fff; font-size:13px; font-weight:600;">Live Demo & Presentation Mode</span>
300
- <span class="badge" style="font-size:10.5px; opacity:0.7;">No Snapshot Saved</span>
281
+ <div>
282
+ <span style="color: var(--text-heading); font-size:13px; font-weight:600;">Application Snapshots & Checkpoints</span>
283
+ <span class="badge" style="font-size:10.5px; opacity:0.7; margin-left:6px;">No Snapshots Saved</span>
301
284
  </div>
302
285
  <button class="btn btn-secondary btn-sm" onclick="selectNav('snapshots')" style="padding:3px 9px; font-size:11px;">
303
- Open Snapshots ➔
286
+ Open Snapshots →
304
287
  </button>
305
288
  </div>
306
289
  `;
@@ -311,90 +294,111 @@ function updateDemoSettingsUI() {
311
294
  const container = document.getElementById('demo-settings-panel');
312
295
  if (!container) return;
313
296
 
314
- if (state.demo && state.demo.hasSnapshot && state.demo.snapshot) {
315
- const snap = state.demo.snapshot;
297
+ const total = state.demo?.total || (state.demo?.snapshots?.length) || (state.demo?.hasSnapshot ? 1 : 0);
298
+ const active = state.demo?.activeSnapshot || state.demo?.snapshot;
299
+
300
+ if (state.demo && state.demo.hasSnapshot && active) {
316
301
  container.innerHTML = `
317
302
  <div style="display:flex; flex-direction:column; gap:0.85rem; font-size:13px;">
318
303
  <div style="display:flex; justify-content:space-between; border-bottom:1px solid var(--border-subtle); padding-bottom:0.5rem;">
319
- <span style="color:var(--text-muted);">Baseline Snapshot Name</span>
320
- <strong style="color:#fff;">${snap.name}</strong>
304
+ <span style="color:var(--text-muted);">Snapshots Saved</span>
305
+ <strong style="color:#60a5fa;">${total} of 10 max</strong>
321
306
  </div>
322
307
  <div style="display:flex; justify-content:space-between; border-bottom:1px solid var(--border-subtle); padding-bottom:0.5rem;">
323
- <span style="color:var(--text-muted);">Frozen At</span>
324
- <code>${new Date(snap.createdAt).toLocaleString()}</code>
308
+ <span style="color:var(--text-muted);">Active Baseline</span>
309
+ <strong style="color: var(--text-heading);">${active.name}</strong>
325
310
  </div>
326
311
  <div style="display:flex; justify-content:space-between; border-bottom:1px solid var(--border-subtle); padding-bottom:0.5rem;">
327
- <span style="color:var(--text-muted);">Preserved Data</span>
328
- <span>${snap.totalCollections} collections, ${snap.totalRecords} records, ${snap.totalFiles} files</span>
312
+ <span style="color:var(--text-muted);">Created At</span>
313
+ <code>${new Date(active.createdAt).toLocaleString()}</code>
329
314
  </div>
330
315
  <div style="display:flex; gap:8px; margin-top:0.5rem;">
331
- <button class="btn btn-warning btn-sm" onclick="openResetDemoModal()">↺ Reset Demo Data</button>
332
- <button class="btn btn-secondary btn-sm" onclick="selectNav('snapshots')">Open Snapshots View ➔</button>
333
- <button class="btn btn-danger btn-sm" onclick="openClearDemoModal()">Clear Snapshot</button>
316
+ <button class="btn btn-warning btn-sm" onclick="openRestoreSnapshotModal('${active.id}')">Restore Active</button>
317
+ <button class="btn btn-secondary btn-sm" onclick="selectNav('snapshots')">Manage Snapshots (${total}) →</button>
318
+ <button class="btn btn-danger btn-sm" onclick="openClearAllSnapshotsModal()">Clear All Snapshots</button>
334
319
  </div>
335
320
  </div>
336
321
  `;
337
322
  } else {
338
323
  container.innerHTML = `
339
324
  <p style="font-size:13px; color:var(--text-muted); margin-bottom:1rem;">
340
- No demo snapshot has been created yet. Freeze your clean database state to enable 1-click restore during live presentations.
325
+ No application snapshots created yet. Freeze your database and file storage state to enable 1-click restore.
341
326
  </p>
342
- <button class="btn btn-primary btn-sm" onclick="openSnapshotDemoModal()">📸 Snapshot Demo State</button>
327
+ <button class="btn btn-primary btn-sm" onclick="openCreateSnapshotModal()">+ Take Application Snapshot</button>
343
328
  `;
344
329
  }
345
330
  }
346
331
 
347
332
  // ==========================================
348
- // Snapshot Demo State Modal
333
+ // Create Snapshot Modal (with 10-limit confirmation)
349
334
  // ==========================================
350
335
 
351
- function openSnapshotDemoModal() {
336
+ function openCreateSnapshotModal() {
352
337
  const modal = document.getElementById('modal-root');
353
338
  const collectionsCount = state.collections ? state.collections.length : 0;
354
- const isUpdate = Boolean(state.demo && state.demo.hasSnapshot);
339
+ const snapshots = state.demo?.snapshots || [];
340
+ const total = state.demo?.total || snapshots.length;
341
+ const maxLimit = state.demo?.maxLimit || 10;
342
+ const isAtLimit = total >= maxLimit;
343
+ const oldestSnapshot = snapshots.length > 0 ? snapshots[0] : null;
344
+
345
+ let limitWarningHtml = '';
346
+ if (isAtLimit && oldestSnapshot) {
347
+ const oldestDate = new Date(oldestSnapshot.createdAt).toLocaleString();
348
+ limitWarningHtml = `
349
+ <div style="background:rgba(239, 68, 68, 0.08); border:1px solid rgba(239, 68, 68, 0.3); border-radius:8px; padding:0.85rem 1rem;">
350
+ <div style="font-size:13px; font-weight:700; color:var(--accent-danger); margin-bottom:4px;">
351
+ Snapshot Capacity Limit Reached (${total} / ${maxLimit})
352
+ </div>
353
+ <div style="font-size:12px; color:#cbd5e1; line-height:1.4;">
354
+ Taking this new snapshot will <strong>permanently delete the oldest snapshot</strong>:
355
+ </div>
356
+ <div style="margin-top:6px; background:rgba(0,0,0,0.25); border:1px solid rgba(239, 68, 68, 0.2); border-radius:6px; padding:6px 10px; font-size:11.5px;">
357
+ <strong style="color: var(--text-heading);">${oldestSnapshot.name}</strong> • ${oldestDate} (${oldestSnapshot.totalRecords} records)
358
+ </div>
359
+ </div>
360
+ `;
361
+ }
355
362
 
356
363
  modal.innerHTML = `
357
364
  <div class="modal-backdrop">
358
365
  <div class="modal" style="max-width:480px;">
359
366
  <div class="modal-header">
360
- <div style="display:flex; align-items:center; gap:0.6rem;">
361
- <span style="font-size:20px;">📸</span>
362
- <div>
363
- <h2 class="modal-title" style="font-size:16px;">${isUpdate ? 'Update Demo Snapshot' : 'Snapshot Demo State'}</h2>
364
- <span style="font-size:11px; color:var(--text-muted);">Freeze clean baseline for presentations & pitches</span>
365
- </div>
367
+ <div>
368
+ <h2 class="modal-title" style="font-size:16px;">Create Application Snapshot</h2>
369
+ <span style="font-size:11px; color:var(--text-muted);">
370
+ ${isAtLimit ? 'Confirm replacement of oldest snapshot' : `Checkpoint ${total + 1} of ${maxLimit}`}
371
+ </span>
366
372
  </div>
367
373
  <button class="btn btn-secondary btn-sm" onclick="closeModal()">✕</button>
368
374
  </div>
369
375
 
370
376
  <div class="modal-body" style="display:flex; flex-direction:column; gap:1rem;">
371
- <p style="font-size:13px; color:var(--text-muted); margin:0; line-height:1.5;">
372
- Freeze your current database state as the clean baseline. During meetings, let clients or investors create, update, and delete records without worrying about ruining your data.
373
- </p>
377
+ ${limitWarningHtml}
374
378
 
375
379
  <div style="background:var(--bg-input); border:1px solid var(--border-subtle); border-radius:8px; padding:0.85rem 1rem; display:flex; align-items:center; justify-content:space-between;">
376
380
  <div>
377
- <div style="font-size:11px; color:var(--text-muted); font-weight:600; text-transform:uppercase;">Active Schema Scope</div>
378
- <div style="font-size:14px; font-weight:700; color:#fff; margin-top:2px;">${collectionsCount} Collections Active</div>
381
+ <div style="font-size:11px; color:var(--text-muted); font-weight:600; text-transform:uppercase;">Active Application State</div>
382
+ <div style="font-size:14px; font-weight:700; color: var(--text-heading); margin-top:2px;">${collectionsCount} Collections Active</div>
379
383
  </div>
380
- <span class="badge badge-get" style="font-size:11px;">SQLite WAL Snapshot</span>
384
+ <span class="badge badge-get" style="font-size:11px;">SQLite WAL & Storage</span>
381
385
  </div>
382
386
 
383
387
  <div class="form-group" style="margin:0;">
384
- <label class="form-label">Snapshot Label / Name</label>
385
- <input type="text" class="form-input" id="snapshot-demo-name" value="${state.demo?.snapshot?.name || 'Initial Clean State'}" placeholder="e.g. Investor Pitch Demo" autofocus>
388
+ <label class="form-label">Snapshot Name / Label</label>
389
+ <input type="text" class="form-input" id="snapshot-modal-name" value="Snapshot #${total + 1}" placeholder="e.g. Before schema migration" autofocus>
386
390
  </div>
387
391
 
388
392
  <div class="form-group" style="margin:0;">
389
393
  <label class="form-label">Description (Optional)</label>
390
- <input type="text" class="form-input" id="snapshot-demo-desc" value="${state.demo?.snapshot?.description || 'Clean showcase baseline data'}" placeholder="e.g. Baseline with 20 sample products and 4 reviews">
394
+ <input type="text" class="form-input" id="snapshot-modal-desc" placeholder="e.g. State before bulk import or demo pitch">
391
395
  </div>
392
396
  </div>
393
397
 
394
398
  <div class="modal-footer" style="padding:0.75rem 1.25rem;">
395
399
  <button class="btn btn-secondary" onclick="closeModal()">Cancel</button>
396
- <button class="btn btn-primary" id="btn-submit-snapshot" onclick="submitSnapshotDemo()">
397
- <span>📸 Freeze Demo State</span>
400
+ <button class="btn ${isAtLimit ? 'btn-warning' : 'btn-primary'}" id="btn-submit-snapshot" onclick="submitCreateSnapshot(${isAtLimit ? 'true' : 'false'})">
401
+ <span>${isAtLimit ? 'Confirm & Evict Oldest' : 'Create Snapshot'}</span>
398
402
  </button>
399
403
  </div>
400
404
  </div>
@@ -402,103 +406,165 @@ function openSnapshotDemoModal() {
402
406
  `;
403
407
  }
404
408
 
405
- async function submitSnapshotDemo() {
409
+ async function submitCreateSnapshot(evictOldest = false) {
406
410
  const btn = document.getElementById('btn-submit-snapshot');
407
- const name = document.getElementById('snapshot-demo-name')?.value || 'Initial Clean State';
408
- const description = document.getElementById('snapshot-demo-desc')?.value || '';
411
+ const name = document.getElementById('snapshot-modal-name')?.value || 'Application Snapshot';
412
+ const description = document.getElementById('snapshot-modal-desc')?.value || '';
409
413
 
410
414
  if (btn) {
411
415
  btn.disabled = true;
412
416
  btn.innerHTML = `
413
417
  <div style="width:13px; height:13px; border:2px solid #ffffff; border-top-color:transparent; border-radius:50%; animation:spin 0.6s linear infinite; display:inline-block; margin-right:4px;"></div>
414
- <span>Freezing State...</span>
418
+ <span>Capturing State...</span>
415
419
  `;
416
420
  }
417
421
 
418
422
  try {
419
- const res = await api('/api/demo/snapshot', {
423
+ const res = await api('/api/snapshots', {
420
424
  method: 'POST',
421
- body: JSON.stringify({ name, description }),
425
+ body: JSON.stringify({ name, description, evictOldest }),
422
426
  });
423
427
 
424
- toast(`📸 Demo baseline '${res.name}' frozen! (${res.totalRecords} records preserved)`, 'success');
428
+ toast(`Snapshot '${res.name}' created! (${res.totalRecords} records preserved)`, 'success');
425
429
  closeModal();
426
430
  await fetchDemoStatus();
427
431
  } catch (err) {
428
- toast(`Failed to take snapshot: ${err.message || String(err)}`, 'error');
432
+ if (err?.data?.code === 'SNAPSHOT_LIMIT_REACHED' || err?.message?.includes('Snapshot limit')) {
433
+ toast(err.message, 'warning');
434
+ closeModal();
435
+ openEvictionConfirmationModal(name, description, err?.data?.oldestSnapshot);
436
+ return;
437
+ }
438
+
439
+ toast(`Failed to create snapshot: ${err.message || String(err)}`, 'error');
429
440
  if (btn) {
430
441
  btn.disabled = false;
431
- btn.innerHTML = '<span>📸 Freeze Demo State</span>';
442
+ btn.innerHTML = `<span>Create Snapshot</span>`;
432
443
  }
433
444
  }
434
445
  }
435
446
 
447
+ function openEvictionConfirmationModal(name, description, oldest) {
448
+ const modal = document.getElementById('modal-root');
449
+ modal.innerHTML = `
450
+ <div class="modal-backdrop">
451
+ <div class="modal" style="max-width:460px;">
452
+ <div class="modal-header" style="border-bottom-color:rgba(239, 68, 68, 0.3);">
453
+ <div>
454
+ <h2 class="modal-title" style="font-size:16px; color:#f87171;">Snapshot Limit Reached</h2>
455
+ <span style="font-size:11px; color:var(--text-muted);">Maximum 10 snapshots allowed</span>
456
+ </div>
457
+ <button class="btn btn-secondary btn-sm" onclick="closeModal()">✕</button>
458
+ </div>
459
+
460
+ <div class="modal-body" style="display:flex; flex-direction:column; gap:1rem;">
461
+ <p style="font-size:13px; color:#cbd5e1; margin:0; line-height:1.5;">
462
+ You have already reached the maximum storage capacity of <strong>10 snapshots</strong>.
463
+ Creating <strong>'${name}'</strong> will permanently evict and delete your oldest snapshot:
464
+ </p>
465
+
466
+ ${oldest ? `
467
+ <div style="background:rgba(239, 68, 68, 0.08); border:1px solid rgba(239, 68, 68, 0.3); border-radius:8px; padding:0.85rem 1rem;">
468
+ <strong style="color: var(--text-heading); font-size:13px;">${oldest.name}</strong>
469
+ <div style="font-size:11.5px; color:var(--text-muted); margin-top:2px;">
470
+ Created: ${new Date(oldest.createdAt).toLocaleString()}
471
+ </div>
472
+ </div>
473
+ ` : ''}
474
+
475
+ <p style="font-size:12px; color:var(--text-muted); margin:0;">
476
+ Do you want to proceed and replace the oldest snapshot?
477
+ </p>
478
+ </div>
479
+
480
+ <div class="modal-footer" style="padding:0.75rem 1.25rem;">
481
+ <button class="btn btn-secondary" onclick="closeModal()">Cancel</button>
482
+ <button class="btn btn-warning" id="btn-confirm-evict" onclick="executeEvictionAndCreate('${encodeURIComponent(name)}', '${encodeURIComponent(description)}')">
483
+ <span>Proceed & Delete Oldest</span>
484
+ </button>
485
+ </div>
486
+ </div>
487
+ </div>
488
+ `;
489
+ }
490
+
491
+ async function executeEvictionAndCreate(encodedName, encodedDesc) {
492
+ const name = decodeURIComponent(encodedName);
493
+ const description = decodeURIComponent(encodedDesc);
494
+ const btn = document.getElementById('btn-confirm-evict');
495
+ if (btn) {
496
+ btn.disabled = true;
497
+ btn.innerHTML = `<span>Saving...</span>`;
498
+ }
499
+
500
+ try {
501
+ const res = await api('/api/snapshots', {
502
+ method: 'POST',
503
+ body: JSON.stringify({ name, description, evictOldest: true }),
504
+ });
505
+
506
+ toast(`Snapshot '${res.name}' created! (Oldest snapshot evicted)`, 'success');
507
+ closeModal();
508
+ await fetchDemoStatus();
509
+ } catch (err) {
510
+ toast(`Failed to take snapshot: ${err.message || String(err)}`, 'error');
511
+ if (btn) btn.disabled = false;
512
+ }
513
+ }
514
+
436
515
  // ==========================================
437
- // Reset Demo Data Modal
516
+ // Restore Snapshot Modal
438
517
  // ==========================================
439
518
 
440
- function openResetDemoModal() {
441
- if (!state.demo || !state.demo.hasSnapshot) {
442
- openSnapshotDemoModal();
443
- return;
444
- }
519
+ function openRestoreSnapshotModal(snapshotId) {
520
+ const snapshots = state.demo?.snapshots || [];
521
+ const snap = snapshots.find((s) => s.id === snapshotId) || state.demo?.activeSnapshot || state.demo?.snapshot;
522
+ if (!snap) return;
445
523
 
446
- const snap = state.demo.snapshot;
447
- const drift = state.demo.liveStats?.drift;
448
- const isModified = Boolean(drift?.isModified);
449
524
  const snapTime = new Date(snap.createdAt).toLocaleString();
450
-
451
525
  const modal = document.getElementById('modal-root');
526
+
452
527
  modal.innerHTML = `
453
528
  <div class="modal-backdrop">
454
529
  <div class="modal" style="max-width:480px;">
455
530
  <div class="modal-header" style="border-bottom-color:rgba(245, 158, 11, 0.25);">
456
- <div style="display:flex; align-items:center; gap:0.6rem;">
457
- <span style="font-size:20px;">↺</span>
458
- <div>
459
- <h2 class="modal-title" style="font-size:16px; color:#fbbf24;">Reset Demo Data</h2>
460
- <span style="font-size:11px; color:var(--text-muted);">Restore pristine state before your next presentation</span>
461
- </div>
531
+ <div>
532
+ <h2 class="modal-title" style="font-size:16px; color:#fbbf24;">Restore Application Snapshot</h2>
533
+ <span style="font-size:11px; color:var(--text-muted);">Safe non-destructive restoration</span>
462
534
  </div>
463
535
  <button class="btn btn-secondary btn-sm" onclick="closeModal()">✕</button>
464
536
  </div>
465
537
 
466
538
  <div class="modal-body" style="display:flex; flex-direction:column; gap:1rem;">
467
539
  <div style="background:rgba(245, 158, 11, 0.08); border:1px solid rgba(245, 158, 11, 0.3); border-radius:8px; padding:0.85rem 1rem;">
468
- <div style="font-size:13px; font-weight:700; color:#fbbf24; margin-bottom:4px;">
469
- Ready to restore '${snap.name}'?
540
+ <div style="font-size:13px; font-weight:700; color:var(--accent-warning); margin-bottom:4px;">
541
+ Restore checkpoint '${snap.name}'?
470
542
  </div>
471
543
  <div style="font-size:12px; color:#e2e8f0; line-height:1.4;">
472
- All records created, edited, or deleted during test evaluations will be instantly reverted back to the exact clean snapshot taken on <strong style="color:#fff;">${snapTime}</strong>.
544
+ Your live database and uploaded files will be reverted to this checkpoint taken on <strong style="color: var(--text-heading);">${snapTime}</strong>.
473
545
  </div>
474
546
  </div>
475
547
 
476
548
  <div style="background:var(--bg-input); border:1px solid var(--border-subtle); border-radius:8px; padding:0.85rem 1rem; display:grid; grid-template-columns:1fr 1fr; gap:0.75rem;">
477
549
  <div>
478
- <div style="font-size:11px; color:var(--text-muted); font-weight:600; text-transform:uppercase;">Collections Restored</div>
479
- <div style="font-size:15px; font-weight:700; color:#fff; margin-top:2px;">${snap.totalCollections} Collections</div>
550
+ <div style="font-size:11px; color:var(--text-muted); font-weight:600; text-transform:uppercase;">Collections</div>
551
+ <div style="font-size:15px; font-weight:700; color: var(--text-heading); margin-top:2px;">${snap.totalCollections} Collections</div>
480
552
  </div>
481
553
  <div>
482
- <div style="font-size:11px; color:var(--text-muted); font-weight:600; text-transform:uppercase;">Records Restored</div>
554
+ <div style="font-size:11px; color:var(--text-muted); font-weight:600; text-transform:uppercase;">Records</div>
483
555
  <div style="font-size:15px; font-weight:700; color:#60a5fa; margin-top:2px;">${snap.totalRecords} Records</div>
484
556
  </div>
485
557
  </div>
486
558
 
487
- ${isModified ? `
488
- <div style="font-size:12px; color:#f87171; background:rgba(239, 68, 68, 0.08); border:1px solid rgba(239, 68, 68, 0.25); border-radius:6px; padding:0.6rem 0.85rem;">
489
- ⚠️ Changes detected: ${drift.recordsDelta >= 0 ? `+${drift.recordsDelta}` : drift.recordsDelta} records will be wiped to restore your clean baseline.
490
- </div>
491
- ` : `
492
- <div style="font-size:12px; color:#34d399; background:rgba(16, 185, 129, 0.08); border:1px solid rgba(16, 185, 129, 0.25); border-radius:6px; padding:0.6rem 0.85rem;">
493
- ✓ Database will be cleanly refreshed to the pristine snapshot.
494
- </div>
495
- `}
559
+ <div style="font-size:12px; color:var(--accent-success); background:var(--accent-success-subtle); border:1px solid var(--border-subtle); border-radius:6px; padding:0.6rem 0.85rem;">
560
+ Safe restoration: Older and newer snapshots in your history will remain safe and unaffected.
561
+ </div>
496
562
  </div>
497
563
 
498
564
  <div class="modal-footer" style="padding:0.75rem 1.25rem;">
499
565
  <button class="btn btn-secondary" onclick="closeModal()">Cancel</button>
500
- <button class="btn btn-warning" id="btn-submit-reset-demo" onclick="submitResetDemo()" style="font-weight:700;">
501
- <span>↺ Reset Demo Data Now</span>
566
+ <button class="btn btn-warning" id="btn-submit-restore-snap" onclick="submitRestoreSnapshot('${snap.id}')" style="font-weight:700;">
567
+ <span>Restore Checkpoint Now</span>
502
568
  </button>
503
569
  </div>
504
570
  </div>
@@ -506,22 +572,22 @@ function openResetDemoModal() {
506
572
  `;
507
573
  }
508
574
 
509
- async function submitResetDemo() {
510
- const btn = document.getElementById('btn-submit-reset-demo');
575
+ async function submitRestoreSnapshot(snapshotId) {
576
+ const btn = document.getElementById('btn-submit-restore-snap');
511
577
  if (btn) {
512
578
  btn.disabled = true;
513
579
  btn.innerHTML = `
514
580
  <div style="width:13px; height:13px; border:2px solid #ffffff; border-top-color:transparent; border-radius:50%; animation:spin 0.6s linear infinite; display:inline-block; margin-right:4px;"></div>
515
- <span>Restoring Clean State...</span>
581
+ <span>Restoring State...</span>
516
582
  `;
517
583
  }
518
584
 
519
585
  try {
520
- const res = await api('/api/demo/reset', {
586
+ const res = await api(`/api/snapshots/${encodeURIComponent(snapshotId)}/restore`, {
521
587
  method: 'POST',
522
588
  });
523
589
 
524
- toast(`✨ Clean demo state restored in ${res.durationMs}ms! (${res.snapshot.totalRecords} records restored)`, 'success');
590
+ toast(`Restored '${res.snapshot.name}' in ${res.durationMs}ms (${res.snapshot.totalRecords} records restored)`, 'success');
525
591
  closeModal();
526
592
 
527
593
  await fetchDemoStatus();
@@ -539,21 +605,57 @@ async function submitResetDemo() {
539
605
  renderHomeView();
540
606
  }
541
607
  } catch (err) {
542
- toast(`Failed to reset demo: ${err.message || String(err)}`, 'error');
608
+ toast(`Failed to restore: ${err.message || String(err)}`, 'error');
543
609
  if (btn) {
544
610
  btn.disabled = false;
545
- btn.innerHTML = '<span>↺ Reset Demo Data Now</span>';
611
+ btn.innerHTML = '<span>Restore Checkpoint Now</span>';
546
612
  }
547
613
  }
548
614
  }
549
615
 
550
- // Clear snapshot confirmation modal
616
+ // Delete single snapshot
617
+ async function deleteSnapshot(snapshotId) {
618
+ const snapshots = state.demo?.snapshots || [];
619
+ const target = snapshots.find((s) => s.id === snapshotId);
620
+ const label = target ? target.name : snapshotId;
621
+
622
+ if (!confirm(`Are you sure you want to delete snapshot '${label}'?`)) return;
623
+
624
+ try {
625
+ await api(`/api/snapshots/${encodeURIComponent(snapshotId)}`, { method: 'DELETE' });
626
+ toast(`Snapshot '${label}' deleted`, 'info');
627
+ await fetchDemoStatus();
628
+ } catch (err) {
629
+ toast(`Failed to delete snapshot: ${err.message || String(err)}`, 'error');
630
+ }
631
+ }
632
+
633
+ // Clear all snapshots
634
+ async function openClearAllSnapshotsModal() {
635
+ if (!confirm('Are you sure you want to delete ALL application snapshots? This cannot be undone.')) return;
636
+ try {
637
+ await api('/api/snapshots', { method: 'DELETE' });
638
+ toast('All snapshots deleted', 'info');
639
+ await fetchDemoStatus();
640
+ } catch (err) {
641
+ toast(`Failed to clear snapshots: ${err.message || String(err)}`, 'error');
642
+ }
643
+ }
644
+
645
+ // Backwards compatibility aliases for existing buttons / triggers
646
+ function openSnapshotDemoModal() {
647
+ openCreateSnapshotModal();
648
+ }
649
+
650
+ function openResetDemoModal() {
651
+ const activeId = state.demo?.activeSnapshot?.id || state.demo?.snapshot?.id;
652
+ if (activeId) {
653
+ openRestoreSnapshotModal(activeId);
654
+ } else {
655
+ openCreateSnapshotModal();
656
+ }
657
+ }
658
+
551
659
  function openClearDemoModal() {
552
- if (!confirm('Are you sure you want to delete the saved demo snapshot?')) return;
553
- api('/api/demo/snapshot', { method: 'DELETE' })
554
- .then(() => {
555
- toast('Demo snapshot deleted', 'info');
556
- fetchDemoStatus();
557
- })
558
- .catch((err) => toast(err.message, 'error'));
660
+ openClearAllSnapshotsModal();
559
661
  }