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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export type ProjectRuntime = 'static' | 'handlebars' | 'react' | 'preact' | 'svelte' | 'vue' | 'python' | 'lua';

export interface Project {
  id: string;
  name: string;
  description?: string;
  createdAt: Date;
  updatedAt: Date;
  settings: {
    runtime?: ProjectRuntime;  // defaults to 'handlebars' when absent (legacy compat)
    defaultTemplate?: string;
    globalStyles?: string;
    previewEntryPoint?: string;  // defaults to '/index.html' when absent
    githubRepo?: {
      owner: string;
      name: string;
      html_url: string;
      full_name: string;
    };
  };
  lastSavedCheckpointId?: string | null;
  lastSavedAt?: Date | null;
  previewImage?: string; // base64 data URL of project preview
  previewUpdatedAt?: Date; // when the preview was last captured
  lastSyncedAt?: Date | null; // Server mode: when project was last synced with server
  serverUpdatedAt?: Date | null; // Server mode: cached server's updatedAt timestamp
  syncStatus?: 'synced' | 'syncing' | 'error' | 'never-synced'; // Server mode: current sync state
  costTracking?: {
    totalCost: number;
    providerBreakdown: Record<string, {
      totalCost: number;
      tokenUsage: {
        input: number;
        output: number;
        reasoning?: number;
        cached?: number;
      };
      requestCount: number;
      lastUpdated: Date;
    }>;
    sessionHistory?: Array<{
      sessionId: string;
      cost: number;
      provider: string;
      timestamp: Date;
      tokenUsage?: {
        input: number;
        output: number;
      };
      correction?: boolean;
    }>;
  };
}

// Deployment - Published version of a project
export interface Deployment {
  // Identity
  id: string;
  projectId: string;
  name: string;
  slug?: string;
  enabled: boolean;

  // Publishing configuration
  underConstruction: boolean;
  customDomain?: string;
  headScripts: ScriptConfig[];
  bodyScripts: ScriptConfig[];
  cdnLinks: CdnConfig[];
  analytics: AnalyticsConfig;
  seo: SeoConfig;
  compliance: ComplianceConfig;

  // State tracking
  settingsVersion: number;
  lastPublishedVersion?: number;

  // Preview
  previewImage?: string; // base64 data URL of deployment screenshot
  previewUpdatedAt?: Date;

  // Database feature (for edge functions - future feature)
  databaseEnabled?: boolean;

  // Metadata
  createdAt: Date;
  updatedAt: Date;
  publishedAt?: Date | null;
}

// Legacy: Publish Settings (kept for backward compatibility)
export interface PublishSettings {
  // Status
  enabled: boolean;
  underConstruction: boolean;
  customDomain?: string;

  // Scripts & Resources
  headScripts: ScriptConfig[];
  bodyScripts: ScriptConfig[];
  cdnLinks: CdnConfig[];

  // Analytics
  analytics: AnalyticsConfig;

  // SEO
  seo: SeoConfig;

  // Compliance
  compliance: ComplianceConfig;

  // State tracking
  settingsVersion: number;
  lastPublishedVersion?: number;
}

export interface ScriptConfig {
  id: string;
  name: string;
  content: string;
  type: 'inline' | 'external';
  src?: string; // URL for external scripts
  async?: boolean;
  defer?: boolean;
  enabled: boolean;
}

export interface CdnConfig {
  id: string;
  name: string;
  url: string;
  type: 'css' | 'js';
  integrity?: string;
  crossorigin?: 'anonymous' | 'use-credentials';
  enabled: boolean;
}

export interface AnalyticsConfig {
  enabled: boolean;
  provider: 'builtin' | 'gtm' | 'ga4' | 'plausible' | 'custom';
  trackingId?: string;
  customScript?: string;
  privacyMode: boolean;

  // Enhanced analytics features (toggleable)
  features?: {
    basicTracking?: boolean;       // Pageviews, referrers (always enabled if analytics on)
    heatmaps?: boolean;             // Click/scroll heatmaps (heavy data)
    sessionRecording?: boolean;     // Journey tracking
    performanceMetrics?: boolean;   // Core Web Vitals, load times
    engagementTracking?: boolean;   // Time on page, scroll depth
    customEvents?: boolean;         // Goal/conversion tracking
  };

  // Data retention settings (in days)
  retention?: {
    pageviews?: number;    // Default: 90 days
    interactions?: number; // Default: 30 days (heatmap data)
    sessions?: number;     // Default: 60 days
  };

  // Analytics token (for secure tracking)
  token?: string;
  tokenGeneratedAt?: string;
}

export interface SeoConfig {
  title?: string;
  description?: string;
  keywords?: string[];
  ogImage?: string;
  ogTitle?: string;
  ogDescription?: string;
  twitterCard?: 'summary' | 'summary_large_image';
  canonical?: string;
  noIndex?: boolean;
  noFollow?: boolean;
}

export interface ComplianceConfig {
  enabled: boolean;
  bannerPosition: 'top' | 'bottom';
  bannerStyle: 'bar' | 'modal' | 'corner';
  message: string;
  acceptButtonText: string;
  declineButtonText: string;
  privacyPolicyUrl?: string;
  cookiePolicyUrl?: string;
  mode: 'opt-in' | 'opt-out';
  blockAnalytics: boolean;
}

export interface VirtualFile {
  id: string;
  projectId: string;
  path: string;
  name: string;
  type: 'html' | 'css' | 'js' | 'json' | 'text' | 'template' | 'image' | 'video' | 'binary';
  content: string | ArrayBuffer;
  mimeType: string;
  size: number;
  createdAt: Date;
  updatedAt: Date;
  metadata: {
    isEntry?: boolean;
    dependencies?: string[];
    isTransient?: boolean;
    isGenerated?: boolean;
    isBuiltIn?: boolean;
    isServerContext?: boolean;
    isReadOnly?: boolean;
  };
}

export interface FileTreeNode {
  id: string;
  projectId: string;
  path: string;
  name: string;
  type: 'directory' | 'file';
  parentPath: string | null;
  isExpanded?: boolean;
  children?: string[];
  metadata?: Record<string, unknown>;
}

export interface FileOperation {
  projectId: string;
  path: string;
  content?: string | ArrayBuffer;
  newPath?: string;
}

export type FileType = VirtualFile['type'];

export const MIME_TYPES: Record<FileType, string> = {
  html: 'text/html',
  css: 'text/css',
  js: 'application/javascript',
  json: 'application/json',
  text: 'text/plain',
  template: 'text/x-handlebars-template',
  image: 'image/*',
  video: 'video/*',
  binary: 'application/octet-stream'
};

export const SUPPORTED_EXTENSIONS = {
  html: ['html', 'htm'],
  css: ['css'],
  js: ['js', 'mjs', 'jsx', 'ts', 'tsx', 'svelte', 'vue', 'py', 'lua'],
  json: ['json'],
  text: ['txt', 'md', 'xml', 'svg'],
  template: ['hbs', 'handlebars'],
  image: ['png', 'jpg', 'jpeg', 'gif', 'webp', 'ico', 'bmp'],
  video: ['mp4', 'webm', 'ogg']
};

export const FILE_SIZE_LIMITS = {
  text: 5 * 1024 * 1024,
  html: 5 * 1024 * 1024,
  css: 5 * 1024 * 1024,
  js: 5 * 1024 * 1024,
  json: 5 * 1024 * 1024,
  template: 5 * 1024 * 1024,
  image: 10 * 1024 * 1024,
  video: 50 * 1024 * 1024,
  binary: 10 * 1024 * 1024
};

export function getFileTypeFromPath(path: string): FileType {
  const ext = path.split('.').pop()?.toLowerCase();
  
  for (const [type, extensions] of Object.entries(SUPPORTED_EXTENSIONS)) {
    if (extensions.includes(ext || '')) {
      return type as FileType;
    }
  }
  
  return 'text';
}

export function getSpecificMimeType(path: string): string {
  const ext = path.split('.').pop()?.toLowerCase();
  
  const mimeMap: Record<string, string> = {
    'html': 'text/html',
    'htm': 'text/html',
    
    'css': 'text/css',
    
    'js': 'application/javascript',
    'mjs': 'application/javascript',
    'jsx': 'application/javascript',
    'ts': 'application/typescript',
    'tsx': 'application/typescript',
    'svelte': 'text/x-svelte',
    'vue': 'text/x-vue',
    'py': 'text/x-python',
    'lua': 'text/x-lua',

    'json': 'application/json',
    
    'txt': 'text/plain',
    'md': 'text/markdown',
    'xml': 'application/xml',
    'svg': 'image/svg+xml',
    
    'hbs': 'text/x-handlebars-template',
    'handlebars': 'text/x-handlebars-template',
    
    'png': 'image/png',
    'jpg': 'image/jpeg',
    'jpeg': 'image/jpeg',
    'gif': 'image/gif',
    'webp': 'image/webp',
    'ico': 'image/x-icon',
    'bmp': 'image/bmp',
    
    'mp4': 'video/mp4',
    'webm': 'video/webm',
    'ogg': 'video/ogg'
  };
  
  return mimeMap[ext || ''] || 'application/octet-stream';
}

export function isFileSupported(fileName: string): boolean {
  const ext = fileName.split('.').pop()?.toLowerCase();
  for (const extensions of Object.values(SUPPORTED_EXTENSIONS)) {
    if (extensions.includes(ext || '')) {
      return true;
    }
  }
  return false;
}

export function getMimeType(type: FileType): string {
  return MIME_TYPES[type];
}

// Template System Types

export interface CustomTemplate {
  id: string;
  name: string;
  description: string;
  version: string;
  files: Array<{path: string; content: string | ArrayBuffer}>;
  directories: string[];
  assets?: Array<{
    filename: string;
    path: string;
  }>;
  metadata: {
    author?: string;
    authorUrl?: string;
    license: string;              // Required, defaults to "personal"
    licenseLabel?: string;        // For custom licenses
    licenseDescription?: string;  // For custom licenses
    tags?: string[];
    thumbnail?: string;           // Base64 data URL
    previewImages?: string[];     // Array of base64 images
    downloadUrl?: string;
  };
  runtime?: ProjectRuntime;
  importedAt: Date;
  updatedAt?: Date;
  backendFeatures?: BackendFeatures;
}

export interface LicenseOption {
  value: string;
  label: string;
  description: string;
}

// Edge Functions Types
export interface EdgeFunction {
  id: string;
  projectId: string;
  name: string;          // URL-safe name (e.g., "products", "get-user")
  description?: string;
  code: string;          // JavaScript code
  method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'ANY';
  enabled: boolean;
  timeoutMs: number;     // Default 5000, max 30000
  createdAt: Date;
  updatedAt: Date;
}

export interface FunctionLog {
  id: number;
  functionId: string;
  method: string;
  path: string;
  statusCode: number;
  durationMs: number;
  error?: string;
  timestamp: Date;
}

// Database Schema Types (for SQL schema viewer)
export interface TableInfo {
  name: string;
  columns: ColumnInfo[];
  rowCount: number;
  isSystemTable: boolean;
}

export interface ColumnInfo {
  name: string;
  type: string;
  nullable: boolean;
  primaryKey: boolean;
  defaultValue?: string;
}

// Server Functions (callable from edge functions)
export interface ServerFunction {
  id: string;
  projectId: string;
  name: string;          // Function name (e.g., 'validateAuth', 'formatPrice')
  description?: string;
  code: string;          // JavaScript function body (receives args, has db/fetch access)
  enabled: boolean;
  createdAt: Date;
  updatedAt: Date;
}

// Secrets (encrypted key-value storage for edge functions)
export interface Secret {
  id: string;
  projectId: string;
  name: string;          // e.g., 'STRIPE_API_KEY', 'SENDGRID_KEY'
  description?: string;
  hasValue: boolean;     // true if user has set a value, false if placeholder
  value?: string;        // Cleartext value for project-level storage (never sent to client in API responses)
  createdAt: Date;
  updatedAt: Date;
}

// Scheduled Functions (cron-triggered edge function execution)
export interface ScheduledFunction {
  id: string;
  projectId: string;
  name: string;                          // URL-safe: lowercase, numbers, hyphens
  description?: string;
  functionId: string;                     // FK → edge_functions.id
  cronExpression: string;                 // e.g. '0 8 * * *'
  timezone: string;                       // e.g. 'UTC', 'America/New_York'
  config: Record<string, unknown>;        // Custom body passed to edge function
  enabled: boolean;
  lastRunAt?: Date;
  nextRunAt?: Date;
  lastStatus?: 'success' | 'error';
  lastError?: string;
  lastDurationMs?: number;
  createdAt: Date;
  updatedAt: Date;
}

// Backend Features (for templates that include backend infrastructure)
export interface BackendFeatures {
  edgeFunctions?: Array<{
    name: string;
    method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'ANY';
    code: string;
    description?: string;
    enabled?: boolean;
    timeoutMs?: number;
  }>;
  serverFunctions?: Array<{
    name: string;
    code: string;
    description?: string;
    enabled?: boolean;
  }>;
  secrets?: Array<{
    name: string;
    description?: string;
  }>;
  scheduledFunctions?: Array<{
    name: string;
    functionName: string;   // name of the edge function to link
    cronExpression: string;
    timezone?: string;
    description?: string;
    config?: Record<string, unknown>;
    enabled?: boolean;
  }>;
  databaseSchema?: string;
  deploymentSettings?: Record<string, unknown>;
}

export const LICENSE_OPTIONS: LicenseOption[] = [
  {
    value: 'personal',
    label: 'Personal Use Only',
    description: 'Cannot be resold or used commercially'
  },
  {
    value: 'commercial',
    label: 'Commercial Use',
    description: 'Can be used in commercial projects, cannot resell template'
  },
  {
    value: 'mit',
    label: 'MIT License',
    description: 'Use freely, must include copyright notice'
  },
  {
    value: 'apache-2.0',
    label: 'Apache 2.0',
    description: 'Similar to MIT, with patent protection'
  },
  {
    value: 'gpl-3.0',
    label: 'GPL 3.0',
    description: 'Open source, derivatives must also be GPL'
  },
  {
    value: 'bsd-3-clause',
    label: 'BSD 3-Clause',
    description: 'Permissive, cannot use author name for promotion'
  },
  {
    value: 'cc-by-4.0',
    label: 'CC BY 4.0',
    description: 'Free use with attribution'
  },
  {
    value: 'cc-by-sa-4.0',
    label: 'CC BY-SA 4.0',
    description: 'Free use with attribution, share-alike'
  },
  {
    value: 'cc-by-nc-4.0',
    label: 'CC BY-NC 4.0',
    description: 'Free for non-commercial use with attribution'
  },
  {
    value: 'unlicense',
    label: 'Unlicense (Public Domain)',
    description: 'No restrictions, completely free to use'
  },
  {
    value: 'all-rights-reserved',
    label: 'All Rights Reserved',
    description: 'Most restrictive, requires explicit permission'
  },
  {
    value: 'custom',
    label: 'Custom License',
    description: 'Specify your own terms'
  }
];