File size: 24,031 Bytes
94193b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/**
 * Auto-Sync Utility
 *
 * Handles automatic background synchronization of projects to server.
 * Provides sync status calculation and conflict detection.
 */

import { Project } from './types';
import { vfs } from './index';
import { saveManager } from './save-manager';
import { logger } from '@/lib/utils';
import { toast } from 'sonner';
import { apiFetch } from '@/lib/api/backend-status';
import { track } from '@/lib/telemetry';

// ── Workspace-scoped URL helpers ─────────────────────────────────────────────

let _autoSyncWorkspaceId: string | undefined;

/**
 * Set the workspace ID used by auto-sync functions for URL scoping.
 * When set, all `/api/sync/…` calls become `/api/w/{workspaceId}/sync/…`.
 * Call with `undefined` to revert to unscoped paths (browser/no-workspace mode).
 */
export function setAutoSyncWorkspaceId(workspaceId: string | undefined): void {
  _autoSyncWorkspaceId = workspaceId;
}

/**
 * Build an API URL scoped to the current workspace when one is configured.
 * @param path - must start with '/' (e.g. '/sync/status')
 */
export function getAutoSyncApiUrl(path: string): string {
  if (_autoSyncWorkspaceId) {
    return `/api/w/${_autoSyncWorkspaceId}${path}`;
  }
  return `/api${path}`;
}

// ── Sync status request dedup ───────────────────────────────────────────────
// Prevents duplicate /sync/status fetches when multiple callers (PageLayout
// quota check, autoPullAllProjects) request it within the same tick.

let _pendingSyncStatus: Promise<any | null> | null = null;
let _cachedSyncData: { data: any; ts: number } | null = null;
const SYNC_STATUS_CACHE_TTL = 5_000;

export async function fetchSyncStatus(): Promise<any | null> {
  if (_cachedSyncData && Date.now() - _cachedSyncData.ts < SYNC_STATUS_CACHE_TTL) {
    return _cachedSyncData.data;
  }
  if (!_pendingSyncStatus) {
    _pendingSyncStatus = (async () => {
      try {
        const res = await apiFetch(getAutoSyncApiUrl('/sync/status'));
        if (!res.ok) return null;
        const data = await res.json();
        _cachedSyncData = { data, ts: Date.now() };
        return data;
      } catch {
        return null;
      } finally {
        _pendingSyncStatus = null;
      }
    })();
  }
  return _pendingSyncStatus;
}

// ─────────────────────────────────────────────────────────────────────────────

export type SyncStatus = 'synced' | 'local-newer' | 'server-newer' | 'conflict' | 'never-synced' | 'local-only' | 'server-only';

interface SyncStatusResult {
  status: SyncStatus;
  message: string;
}


/**
 * Calculate sync status using three-way timestamp comparison
 */
export function calculateSyncStatus(
  localProject: Project,
  serverUpdatedAt?: Date
): SyncStatusResult {
  const { updatedAt, lastSyncedAt } = localProject;

  // If no server timestamp available, it's local-only
  if (!serverUpdatedAt) {
    return {
      status: 'local-only',
      message: 'Project exists only locally'
    };
  }

  // If never synced before
  if (!lastSyncedAt) {
    // Compare local and server times
    if (updatedAt > serverUpdatedAt) {
      return {
        status: 'local-newer',
        message: 'Local changes not yet synced'
      };
    } else if (serverUpdatedAt > updatedAt) {
      return {
        status: 'server-newer',
        message: 'Server has updates'
      };
    } else {
      return {
        status: 'synced',
        message: 'In sync with server'
      };
    }
  }

  // Three-way comparison
  const localChanged = updatedAt > lastSyncedAt;
  const serverChanged = serverUpdatedAt > lastSyncedAt;

  if (localChanged && serverChanged) {
    return {
      status: 'conflict',
      message: 'Both local and server have changes'
    };
  }

  if (localChanged) {
    return {
      status: 'local-newer',
      message: 'Local changes not yet synced'
    };
  }

  if (serverChanged) {
    return {
      status: 'server-newer',
      message: 'Server has updates'
    };
  }

  return {
    status: 'synced',
    message: 'In sync with server'
  };
}

const syncRetries = new Map<string, number>();
const MAX_RETRIES = 3;

/**
 * Auto-sync a project to the server (non-blocking, silent by default)
 */
export async function autoSyncProject(projectId: string, silent = true): Promise<void> {
  // Only sync in Server Mode
  if (process.env.NEXT_PUBLIC_SERVER_MODE !== 'true') {
    return;
  }

  try {
    const project = await vfs.getProject(projectId);
    if (!project) {
      logger.error(`[AutoSync] Project ${projectId} not found`);
      return;
    }

    // Don't sync if already syncing
    if (project.syncStatus === 'syncing') {
      return;
    }

    // Get all files
    const files = await vfs.listFiles(projectId);

    // Push to server
    const response = await apiFetch(getAutoSyncApiUrl(`/sync/projects/${projectId}`), {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ project, files }),
    });

    if (response.status === 401) {
      syncRetries.delete(projectId);
      logger.warn(`[AutoSync] Skipping ${projectId}: session expired`);
      return;
    }

    if (response.status === 409) {
      logger.warn(`[AutoSync] Conflict for ${projectId}: server has newer changes`);
      project.syncStatus = 'error';
      await vfs.updateProject(project, { preserveUpdatedAt: true });
      toast.warning(
        `"${project.name}" was edited on another device. Your local changes are preserved — open Server Sync to compare.`,
        { duration: Infinity }
      );
      syncRetries.delete(projectId);
      return;
    }

    if (!response.ok) {
      throw new Error(`Sync failed: ${response.status}`);
    }

    const data = await response.json();
    const syncedProject = data.project;

    // Update local project with sync metadata (preserve updatedAt)
    project.lastSyncedAt = new Date(syncedProject.lastSyncedAt);
    project.serverUpdatedAt = new Date(syncedProject.serverUpdatedAt);
    project.syncStatus = 'synced';
    await vfs.updateProject(project, { preserveUpdatedAt: true });

    syncRetries.delete(projectId);
    logger.debug(`[AutoSync] Project ${projectId} synced successfully`);

    if (!silent) {
      toast.success('Project synced', {
        duration: 2000,
        position: 'bottom-right'
      });
    }
  } catch (error) {
    logger.error(`[AutoSync] Failed to sync project ${projectId}:`, error);
    track('sync_fail', { item_type: 'project', direction: 'push' });

    const retries = syncRetries.get(projectId) ?? 0;
    if (retries < MAX_RETRIES) {
      syncRetries.set(projectId, retries + 1);
      logger.warn(`[AutoSync] Will retry ${projectId} (${retries + 1}/${MAX_RETRIES})`);
      setTimeout(() => autoSyncProject(projectId), (retries + 1) * 5000);
    } else {
      syncRetries.delete(projectId);
      try {
        const project = await vfs.getProject(projectId);
        if (project) {
          project.syncStatus = 'error';
          await vfs.updateProject(project, { preserveUpdatedAt: true });
        }
      } catch (updateError) {
        logger.error(`[AutoSync] Failed to update project status:`, updateError);
      }

      if (!silent) {
        toast.error('Sync failed', {
          duration: 4000,
          position: 'bottom-right'
        });
      }
    }
  }
}

/**
 * Check if server has updates for a project
 */
export async function checkServerUpdates(projectId: string): Promise<boolean> {
  if (process.env.NEXT_PUBLIC_SERVER_MODE !== 'true') {
    return false;
  }

  try {
    const localProject = await vfs.getProject(projectId);
    if (!localProject) {
      return false;
    }

    // Use lightweight status endpoint instead of fetching full project + files
    const response = await apiFetch(getAutoSyncApiUrl('/sync/status'));
    if (!response.ok) {
      return false;
    }

    const data = await response.json();
    const serverStatus = (data.projects || []).find((p: { id: string }) => p.id === projectId);
    if (!serverStatus) {
      return false;
    }

    const serverUpdatedAt = new Date(serverStatus.updatedAt);
    const status = calculateSyncStatus(localProject, serverUpdatedAt);
    return status.status === 'server-newer' || status.status === 'conflict';
  } catch (error) {
    logger.error(`[AutoSync] Failed to check server updates for ${projectId}:`, error);
    return false;
  }
}

/**
 * Pull updates from server for a project
 */
export async function pullServerUpdates(projectId: string, showToast = true): Promise<boolean> {
  if (process.env.NEXT_PUBLIC_SERVER_MODE !== 'true') {
    return false;
  }

  try {
    const response = await apiFetch(getAutoSyncApiUrl(`/sync/projects/${projectId}`));
    if (!response.ok) {
      throw new Error(`Failed to pull updates: ${response.status}`);
    }

    const data = await response.json();
    const serverProject: Project = data.project;
    const serverFiles = data.files;

    // Save local state as checkpoint before overwriting (safety net)
    const localFiles = await vfs.listFiles(projectId);
    if (localFiles.length > 0) {
      try {
        const { checkpointManager } = await import('./checkpoint');
        await checkpointManager.createCheckpoint(projectId, 'Pre-sync backup (before pull)', { kind: 'auto' });
      } catch (cpErr) {
        logger.warn(`[AutoSync] Failed to create pre-pull checkpoint for ${projectId}:`, cpErr);
      }
    }

    // Suppress dirty marking during pull — these are server state, not user edits
    await saveManager.runWithSuppressedDirty(projectId, async () => {
      // Update project
      await vfs.updateProject(serverProject);

      // Sync files: update existing, create new, delete removed
      const existingFiles = await vfs.listFiles(projectId);
      const existingPaths = new Set(existingFiles.map(f => f.path));
      const serverPaths = new Set(serverFiles.map((f: any) => f.path));

      for (const file of serverFiles) {
        if (existingPaths.has(file.path)) {
          await vfs.updateFile(projectId, file.path, file.content || '');
        } else {
          await vfs.createFile(projectId, file.path, file.content || '');
        }
      }

      for (const existing of existingFiles) {
        if (!serverPaths.has(existing.path)) {
          await vfs.deleteFile(projectId, existing.path);
        }
      }

      // Update sync metadata
      const localProject = await vfs.getProject(projectId);
      if (localProject) {
        localProject.lastSyncedAt = new Date();
        localProject.serverUpdatedAt = new Date(serverProject.updatedAt);
        localProject.syncStatus = 'synced';
        await vfs.updateProject(localProject);
      }
    });

    logger.debug(`[AutoSync] Pulled updates for project ${projectId}`);
    if (showToast) {
      toast.success('Project updated from server');
    }

    return true;
  } catch (error) {
    logger.error(`[AutoSync] Failed to pull updates for ${projectId}:`, error);
    track('sync_fail', { item_type: 'project', direction: 'pull' });
    if (showToast) {
      toast.error('Failed to pull server updates');
    }
    return false;
  }
}


const AUTO_PULL_SESSION_KEY = 'osw_auto_pull_done';

/**
 * Auto-pull projects from server on first load of a browser tab.
 * Compares local `serverUpdatedAt` against server timestamps — only pulls
 * projects that actually diverged or don't exist locally.
 * Runs once per tab (tracked via sessionStorage). Pass force=true to bypass.
 */
export async function autoPullAllProjects(onProgress?: (current: number, total: number) => void, options?: { force?: boolean }): Promise<{
  pulled: number;
  skipped: number;
  conflicts: string[];
  errors: number;
}> {
  if (process.env.NEXT_PUBLIC_SERVER_MODE !== 'true') {
    return { pulled: 0, skipped: 0, conflicts: [], errors: 0 };
  }

  if (!options?.force) {
    try {
      if (sessionStorage.getItem(AUTO_PULL_SESSION_KEY)) {
        return { pulled: 0, skipped: 0, conflicts: [], errors: 0 };
      }
    } catch { /* sessionStorage unavailable — proceed */ }
  }

  let pulled = 0;
  let skipped = 0;
  const conflicts: string[] = [];
  let errors = 0;

  try {
    // Lightweight check — just project IDs + timestamps from server (deduped)
    const data = await fetchSyncStatus();
    if (!data) {
      logger.debug('[AutoSync] Server not available for pull');
      return { pulled: 0, skipped: 0, conflicts: [], errors: 0 };
    }

    const serverStatuses: { id: string; updatedAt: string }[] = data.projects || [];

    // Build local lookup: projectId → serverUpdatedAt (already cached from last sync)
    await vfs.init();
    const localProjects = await vfs.listProjects();
    const localMap = new Map(localProjects.map(p => [p.id, p]));

    // Filter to only projects that need attention
    const needsPull: { id: string; serverUpdatedAt: Date; isNew: boolean }[] = [];
    for (const serverStatus of serverStatuses) {
      const serverUpdatedAt = new Date(serverStatus.updatedAt);
      const local = localMap.get(serverStatus.id);

      if (!local) {
        needsPull.push({ id: serverStatus.id, serverUpdatedAt, isNew: true });
        continue;
      }

      const syncStatus = calculateSyncStatus(local, serverUpdatedAt);
      if (syncStatus.status === 'server-newer') {
        needsPull.push({ id: serverStatus.id, serverUpdatedAt, isNew: false });
      } else if (syncStatus.status === 'conflict') {
        conflicts.push(serverStatus.id);
      } else {
        skipped++;
      }
    }

    if (needsPull.length === 0 && conflicts.length === 0) {
      logger.debug(`[AutoSync] All ${skipped} projects up to date`);
      try { sessionStorage.setItem(AUTO_PULL_SESSION_KEY, '1'); } catch {}
      return { pulled: 0, skipped, conflicts, errors: 0 };
    }

    const total = needsPull.length;
    let processed = 0;
    const CONCURRENCY = 4;

    async function pullOne(item: typeof needsPull[number]) {
      try {
        if (item.isNew) {
          const pullResponse = await apiFetch(getAutoSyncApiUrl(`/sync/projects/${item.id}`));
          if (!pullResponse.ok) { errors++; return; }

          const pullData = await pullResponse.json();
          const serverProject: Project = pullData.project;
          const serverFiles = pullData.files;

          await vfs.createProject(serverProject.name, serverProject.description || '', serverProject.id);

          await saveManager.runWithSuppressedDirty(serverProject.id, async () => {
            const newProject = await vfs.getProject(serverProject.id);
            if (newProject) {
              newProject.settings = serverProject.settings || {};
              newProject.lastSyncedAt = new Date();
              newProject.serverUpdatedAt = item.serverUpdatedAt;
              newProject.syncStatus = 'synced';
              await vfs.updateProject(newProject, { preserveUpdatedAt: true });
            }

            for (const file of serverFiles) {
              await vfs.createFile(serverProject.id, file.path, file.content || '');
            }
          });

          pulled++;
          logger.debug(`[AutoSync] Pulled new project: ${serverProject.name}`);
        } else {
          await pullServerUpdates(item.id, false);
          pulled++;
        }
      } catch (error) {
        logger.error(`[AutoSync] Failed to process project ${item.id}:`, error);
        track('sync_fail', { item_type: 'project', direction: 'pull' });
        errors++;
      } finally {
        processed++;
        onProgress?.(processed, total);
      }
    }

    // Pull up to CONCURRENCY projects in parallel
    for (let i = 0; i < needsPull.length; i += CONCURRENCY) {
      const batch = needsPull.slice(i, i + CONCURRENCY);
      await Promise.all(batch.map(pullOne));
    }

    if (pulled > 0) {
      logger.debug(`[AutoSync] Auto-pull complete: ${pulled} updated, ${skipped} skipped, ${errors} errors`);
    }

    try { sessionStorage.setItem(AUTO_PULL_SESSION_KEY, '1'); } catch {}
    return { pulled, skipped, conflicts, errors };
  } catch (error) {
    logger.error('[AutoSync] Failed to auto-pull projects:', error);
    return { pulled, skipped, conflicts, errors };
  }
}

/**
 * Auto-delete a project from the server (non-blocking)
 */
export async function autoDeleteProject(projectId: string): Promise<void> {
  if (process.env.NEXT_PUBLIC_SERVER_MODE !== 'true') return;
  try {
    await apiFetch(getAutoSyncApiUrl(`/sync/projects/${projectId}`), { method: 'DELETE' });
    logger.debug(`[AutoSync] Project ${projectId} deleted from server`);
  } catch (error) {
    logger.error(`[AutoSync] Failed to delete project ${projectId} from server:`, error);
  }
}

/**
 * Auto-sync a skill to the server (non-blocking)
 */
export async function autoSyncSkill(skill: import('./skills/types').Skill): Promise<void> {
  if (process.env.NEXT_PUBLIC_SERVER_MODE !== 'true') return;
  if (skill.isBuiltIn) return;
  try {
    const { getSyncManager } = await import('./sync-manager');
    const syncManager = getSyncManager();
    await syncManager.pushSkill(skill);
    logger.debug(`[AutoSync] Skill ${skill.id} synced`);
  } catch (error) {
    logger.error(`[AutoSync] Failed to sync skill ${skill.id}:`, error);
    track('sync_fail', { item_type: 'skill', direction: 'push' });
  }
}

/**
 * Auto-delete a skill from the server (non-blocking)
 */
export async function autoDeleteSkill(skillId: string): Promise<void> {
  if (process.env.NEXT_PUBLIC_SERVER_MODE !== 'true') return;
  try {
    await apiFetch(getAutoSyncApiUrl(`/sync/skills/${skillId}`), { method: 'DELETE' });
    logger.debug(`[AutoSync] Skill ${skillId} deleted from server`);
  } catch (error) {
    logger.error(`[AutoSync] Failed to delete skill ${skillId} from server:`, error);
  }
}

/**
 * Auto-sync a template to the server (non-blocking)
 */
export async function autoSyncTemplate(template: import('./types').CustomTemplate): Promise<void> {
  if (process.env.NEXT_PUBLIC_SERVER_MODE !== 'true') return;
  try {
    const { getSyncManager } = await import('./sync-manager');
    const syncManager = getSyncManager();
    await syncManager.pushTemplate(template);
    logger.debug(`[AutoSync] Template ${template.id} synced`);
  } catch (error) {
    logger.error(`[AutoSync] Failed to sync template ${template.id}:`, error);
    track('sync_fail', { item_type: 'template', direction: 'push' });
  }
}

/**
 * Auto-delete a template from the server (non-blocking)
 */
export async function autoDeleteTemplate(templateId: string): Promise<void> {
  if (process.env.NEXT_PUBLIC_SERVER_MODE !== 'true') return;
  try {
    await apiFetch(getAutoSyncApiUrl(`/sync/templates/${templateId}`), { method: 'DELETE' });
    logger.debug(`[AutoSync] Template ${templateId} deleted from server`);
  } catch (error) {
    logger.error(`[AutoSync] Failed to delete template ${templateId} from server:`, error);
  }
}

/**
 * Auto-sync a model template to the server (non-blocking)
 */
export async function autoSyncModelTemplate(template: import('@/lib/llm/models/assignment').ModelTemplate): Promise<void> {
  if (process.env.NEXT_PUBLIC_SERVER_MODE !== 'true') return;
  if (template.builtin) return;
  try {
    const { getSyncManager } = await import('./sync-manager');
    const syncManager = getSyncManager();
    await syncManager.pushModelTemplate(template);
    logger.debug(`[AutoSync] Model template ${template.id} synced`);
  } catch (error) {
    logger.error(`[AutoSync] Failed to sync model template ${template.id}:`, error);
    track('sync_fail', { item_type: 'modelTemplate', direction: 'push' });
  }
}

/**
 * Auto-delete a model template from the server (non-blocking)
 */
export async function autoDeleteModelTemplate(templateId: string): Promise<void> {
  if (process.env.NEXT_PUBLIC_SERVER_MODE !== 'true') return;
  try {
    await apiFetch(getAutoSyncApiUrl(`/sync/model-templates/${templateId}`), { method: 'DELETE' });
    logger.debug(`[AutoSync] Model template ${templateId} deleted from server`);
  } catch (error) {
    logger.error(`[AutoSync] Failed to delete model template ${templateId} from server:`, error);
  }
}

/**
 * Auto-sync a custom provider connection to the server (non-blocking, key-less)
 */
export async function autoSyncConnection(cfg: import('@/lib/llm/providers/types').ProviderConfig): Promise<void> {
  if (process.env.NEXT_PUBLIC_SERVER_MODE !== 'true') return;
  try {
    const { toConnectionRecord } = await import('@/lib/llm/providers/connection-record');
    const { getSyncManager } = await import('./sync-manager');
    await getSyncManager().pushConnection(toConnectionRecord(cfg));
    logger.debug(`[AutoSync] Connection ${cfg.id} synced`);
  } catch (error) {
    logger.error(`[AutoSync] Failed to sync connection ${cfg.id}:`, error);
  }
}

/**
 * Pull custom provider connections from the server into the local cache (server mode only).
 * Keys are never pulled — only definitions. Uses the low-level cache writer so it does NOT
 * re-trigger auto-sync (which would cause a pull->push loop).
 */
export async function pullConnectionsIntoCache(): Promise<void> {
  if (process.env.NEXT_PUBLIC_SERVER_MODE !== 'true') return;
  try {
    const { getSyncManager } = await import('./sync-manager');
    const result = await getSyncManager().pullConnections();
    if (!result.success || !result.connections?.length) return;
    const { getCustomProviders, setCustomProviders } = await import('@/lib/llm/providers/custom-providers');
    const { fromConnectionRecord } = await import('@/lib/llm/providers/connection-record');
    const merged = { ...getCustomProviders() };
    for (const rec of result.connections) {
      merged[rec.id] = fromConnectionRecord(rec);
    }
    setCustomProviders(merged);
    logger.debug(`[AutoSync] Pulled ${result.connections.length} connection(s) into cache`);
  } catch (error) {
    logger.error('[AutoSync] Failed to pull connections into cache:', error);
  }
}

/**
 * Auto-sync a custom interview template to the server (non-blocking)
 */
export async function autoSyncInterviewTemplate(template: import('@/lib/interview/types').InterviewTemplate): Promise<void> {
  if (process.env.NEXT_PUBLIC_SERVER_MODE !== 'true') return;
  if (template.isBuiltIn) return;
  try {
    const { getSyncManager } = await import('./sync-manager');
    await getSyncManager().pushInterviewTemplate(template);
    logger.debug(`[AutoSync] Interview template ${template.id} synced`);
  } catch (error) {
    logger.error(`[AutoSync] Failed to sync interview template ${template.id}:`, error);
    track('sync_fail', { item_type: 'interviewTemplate', direction: 'push' });
  }
}

/**
 * Auto-delete an interview template from the server (non-blocking)
 */
export async function autoDeleteInterviewTemplate(templateId: string): Promise<void> {
  if (process.env.NEXT_PUBLIC_SERVER_MODE !== 'true') return;
  try {
    await apiFetch(getAutoSyncApiUrl(`/sync/interview-templates/${templateId}`), { method: 'DELETE' });
    logger.debug(`[AutoSync] Interview template ${templateId} deleted from server`);
  } catch (error) {
    logger.error(`[AutoSync] Failed to delete interview template ${templateId} from server:`, error);
  }
}

/**
 * Auto-delete a custom provider connection from the server (non-blocking)
 */
export async function autoDeleteConnection(id: string): Promise<void> {
  if (process.env.NEXT_PUBLIC_SERVER_MODE !== 'true') return;
  try {
    await apiFetch(getAutoSyncApiUrl(`/sync/connections/${id}`), { method: 'DELETE' });
    logger.debug(`[AutoSync] Connection ${id} deleted from server`);
  } catch (error) {
    logger.error(`[AutoSync] Failed to delete connection ${id} from server:`, error);
  }
}