File size: 17,380 Bytes
529090e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Database initialization
 *
 * Primary: Prisma/PostgreSQL when DATABASE_URL is set
 * Fallback: sql.js (SQLite, in-memory) for legacy synchronous consumers
 */

import fs from 'fs';
import path from 'path';
import initSqlJs from 'sql.js';
import { prisma, checkPrismaConnection } from './prisma.js';

type SqliteDatabase = import('sql.js').Database;

// Legacy interface for backward compatibility (synchronous API)
export interface DatabaseStatement<P = any[], R = any> {
  all: (...params: P extends any[] ? P : any[]) => R[];
  get: (...params: P extends any[] ? P : any[]) => R | undefined;
  run: (...params: P extends any[] ? P : any[]) => { changes: number; lastInsertRowid: number | bigint };
  free: () => void;
}

export interface Database {
  prepare: <P = any[], R = any>(sql: string) => DatabaseStatement<P, R>;
  run: (sql: string, params?: any[]) => { changes: number; lastInsertRowid: number | bigint };
  close: () => void;
}

let isInitialized = false;
let sqliteDb: SqliteDatabase | null = null;
let sqliteReady = false;
let prismaReady = false;

const legacyTableBootstrap = `
CREATE TABLE IF NOT EXISTS security_search_templates (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL,
  description TEXT NOT NULL,
  query TEXT NOT NULL,
  severity TEXT NOT NULL,
  timeframe TEXT NOT NULL,
  sources TEXT NOT NULL,
  created_at TEXT DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS security_search_history (
  id TEXT PRIMARY KEY,
  query TEXT NOT NULL,
  severity TEXT NOT NULL,
  timeframe TEXT NOT NULL,
  sources TEXT NOT NULL,
  results_count INTEGER NOT NULL,
  latency_ms INTEGER NOT NULL,
  created_at TEXT DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS security_activity_events (
  id TEXT PRIMARY KEY,
  title TEXT NOT NULL,
  description TEXT NOT NULL,
  category TEXT NOT NULL,
  severity TEXT NOT NULL,
  source TEXT NOT NULL,
  rule TEXT,
  channel TEXT NOT NULL,
  payload TEXT,
  created_at TEXT DEFAULT CURRENT_TIMESTAMP,
  acknowledged INTEGER DEFAULT 0
);

CREATE TABLE IF NOT EXISTS widget_permissions (
  widget_id TEXT,
  resource_type TEXT NOT NULL,
  access_level TEXT NOT NULL,
  override INTEGER DEFAULT 0,
  PRIMARY KEY (widget_id, resource_type)
);

CREATE TABLE IF NOT EXISTS vector_documents (
  id TEXT PRIMARY KEY,
  content TEXT NOT NULL,
  embedding TEXT,
  metadata TEXT,
  namespace TEXT DEFAULT 'default',
  userId TEXT DEFAULT 'system',
  orgId TEXT DEFAULT 'default',
  createdAt TEXT DEFAULT CURRENT_TIMESTAMP,
  updatedAt TEXT DEFAULT CURRENT_TIMESTAMP
);`;

/**
 * Initialize database connection(s)
 * - Connect Prisma when DATABASE_URL is present
 * - Always prepare a lightweight SQLite (sql.js) fallback for legacy sync consumers
 */
export async function initializeDatabase(): Promise<void> {
  if (isInitialized) return;

  // 1) Try Prisma/Postgres first
  try {
    const prismaOk = await checkPrismaConnection();
    if (prismaOk) {
      prismaReady = true;
      // Ensure required legacy tables exist for raw queries
      await prisma.$executeRawUnsafe(`
        CREATE TABLE IF NOT EXISTS "security_search_templates" (
          "id" TEXT PRIMARY KEY,
          "name" TEXT NOT NULL,
          "description" TEXT NOT NULL,
          "query" TEXT NOT NULL,
          "severity" TEXT NOT NULL,
          "timeframe" TEXT NOT NULL,
          "sources" JSONB NOT NULL DEFAULT '[]'::jsonb,
          "created_at" TIMESTAMPTZ DEFAULT NOW()
        );
      `);
      await prisma.$executeRawUnsafe(`
        CREATE TABLE IF NOT EXISTS "security_search_history" (
          "id" TEXT PRIMARY KEY,
          "query" TEXT NOT NULL,
          "severity" TEXT NOT NULL,
          "timeframe" TEXT NOT NULL,
          "sources" JSONB NOT NULL DEFAULT '[]'::jsonb,
          "results_count" INTEGER NOT NULL,
          "latency_ms" INTEGER NOT NULL,
          "created_at" TIMESTAMPTZ DEFAULT NOW()
        );
      `);
      await prisma.$executeRawUnsafe(`
        CREATE TABLE IF NOT EXISTS "security_activity_events" (
          "id" TEXT PRIMARY KEY,
          "title" TEXT NOT NULL,
          "description" TEXT NOT NULL,
          "category" TEXT NOT NULL,
          "severity" TEXT NOT NULL,
          "source" TEXT NOT NULL,
          "rule" TEXT,
          "channel" TEXT NOT NULL,
          "payload" JSONB,
          "created_at" TIMESTAMPTZ DEFAULT NOW(),
          "acknowledged" BOOLEAN DEFAULT FALSE
        );
      `);
      await prisma.$executeRawUnsafe(`
        CREATE TABLE IF NOT EXISTS "widget_permissions" (
          "widget_id" TEXT,
          "resource_type" TEXT NOT NULL,
          "access_level" TEXT NOT NULL,
          "override" BOOLEAN DEFAULT FALSE,
          CONSTRAINT widget_permissions_pk PRIMARY KEY ("widget_id", "resource_type")
        );
      `);
      await prisma.$executeRawUnsafe(`
        CREATE TABLE IF NOT EXISTS "vector_documents" (
          "id" TEXT PRIMARY KEY,
          "content" TEXT NOT NULL,
          "embedding" JSONB,
          "metadata" JSONB,
          "namespace" TEXT DEFAULT 'default',
          "userId" TEXT DEFAULT 'system',
          "orgId" TEXT DEFAULT 'default',
          "createdAt" TIMESTAMPTZ DEFAULT NOW(),
          "updatedAt" TIMESTAMPTZ DEFAULT NOW()
        );
      `);
      console.log('✅ Prisma database connected');
    }
  } catch (error) {
    console.warn('⚠️ Prisma connection failed, continuing with SQLite fallback', error);
  }

  // 2) Always prepare SQLite (sql.js) fallback for synchronous consumers
  try {
    const SQL = await initSqlJs();
    sqliteDb = new SQL.Database();

    // Initialize SQLite with inlined schema (avoids fs/path issues in Docker/Bundled envs)
    const fallbackSchema = `
-- Memory (CMA) tables
CREATE TABLE IF NOT EXISTS memory_entities (
  id              INTEGER PRIMARY KEY AUTOINCREMENT,
  org_id          TEXT NOT NULL,
  user_id         TEXT,
  entity_type     TEXT NOT NULL,
  content         TEXT NOT NULL,
  importance      INTEGER NOT NULL DEFAULT 3,
  created_at      DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS memory_relations (
  id              INTEGER PRIMARY KEY AUTOINCREMENT,
  org_id          TEXT NOT NULL,
  source_id       INTEGER NOT NULL REFERENCES memory_entities(id),
  target_id       INTEGER NOT NULL REFERENCES memory_entities(id),
  relation_type   TEXT NOT NULL,
  created_at      DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS memory_tags (
  id              INTEGER PRIMARY KEY AUTOINCREMENT,
  entity_id       INTEGER NOT NULL REFERENCES memory_entities(id),
  tag             TEXT NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_memory_entities_org ON memory_entities(org_id);
CREATE INDEX IF NOT EXISTS idx_memory_entities_user ON memory_entities(user_id);
CREATE INDEX IF NOT EXISTS idx_memory_tags_entity ON memory_tags(entity_id);
CREATE INDEX IF NOT EXISTS idx_memory_tags_tag ON memory_tags(tag);

-- SRAG tables
CREATE TABLE IF NOT EXISTS raw_documents (
  id            INTEGER PRIMARY KEY AUTOINCREMENT,
  org_id        TEXT NOT NULL,
  source_type   TEXT NOT NULL,
  source_path   TEXT NOT NULL,
  content       TEXT NOT NULL,
  created_at    DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS structured_facts (
  id            INTEGER PRIMARY KEY AUTOINCREMENT,
  org_id        TEXT NOT NULL,
  doc_id        INTEGER REFERENCES raw_documents(id),
  fact_type     TEXT NOT NULL,
  json_payload  TEXT NOT NULL,
  occurred_at   DATETIME,
  created_at    DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_raw_documents_org ON raw_documents(org_id);
CREATE INDEX IF NOT EXISTS idx_structured_facts_org ON structured_facts(org_id);
CREATE INDEX IF NOT EXISTS idx_structured_facts_type ON structured_facts(fact_type);

-- Evolution Agent tables
CREATE TABLE IF NOT EXISTS agent_prompts (
  id            INTEGER PRIMARY KEY AUTOINCREMENT,
  agent_id      TEXT NOT NULL,
  version       INTEGER NOT NULL,
  prompt_text   TEXT NOT NULL,
  created_at    DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  created_by    TEXT NOT NULL DEFAULT 'evolution-agent'
);

CREATE TABLE IF NOT EXISTS agent_runs (
  id              INTEGER PRIMARY KEY AUTOINCREMENT,
  agent_id        TEXT NOT NULL,
  prompt_version  INTEGER NOT NULL,
  input_summary   TEXT,
  output_summary  TEXT,
  kpi_name        TEXT,
  kpi_delta       REAL,
  run_context     TEXT,
  created_at      DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_agent_prompts_agent ON agent_prompts(agent_id, version);
CREATE INDEX IF NOT EXISTS idx_agent_runs_agent ON agent_runs(agent_id);

-- PAL tables
CREATE TABLE IF NOT EXISTS pal_user_profiles (
  id            INTEGER PRIMARY KEY AUTOINCREMENT,
  user_id       TEXT NOT NULL,
  org_id        TEXT NOT NULL,
  preference_tone TEXT NOT NULL DEFAULT 'neutral',
  created_at    DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at    DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS pal_focus_windows (
  id            INTEGER PRIMARY KEY AUTOINCREMENT,
  user_id       TEXT NOT NULL,
  org_id        TEXT NOT NULL,
  weekday       INTEGER NOT NULL,
  start_hour    INTEGER NOT NULL,
  end_hour      INTEGER NOT NULL
);

CREATE TABLE IF NOT EXISTS pal_events (
  id            INTEGER PRIMARY KEY AUTOINCREMENT,
  user_id       TEXT NOT NULL,
  org_id        TEXT NOT NULL,
  event_type    TEXT NOT NULL,
  payload       TEXT NOT NULL,
  detected_stress_level TEXT,
  created_at    DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_pal_profiles_user ON pal_user_profiles(user_id, org_id);
CREATE INDEX IF NOT EXISTS idx_pal_focus_windows_user ON pal_focus_windows(user_id);
CREATE INDEX IF NOT EXISTS idx_pal_events_user ON pal_events(user_id, org_id);

-- Security Intelligence tables
CREATE TABLE IF NOT EXISTS security_search_templates (
  id            TEXT PRIMARY KEY,
  name          TEXT NOT NULL,
  description   TEXT NOT NULL,
  query         TEXT NOT NULL,
  severity      TEXT NOT NULL DEFAULT 'all',
  timeframe     TEXT NOT NULL DEFAULT '24h',
  sources       TEXT NOT NULL DEFAULT '[]',
  created_at    DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS security_search_history (
  id             TEXT PRIMARY KEY,
  query          TEXT NOT NULL,
  severity       TEXT NOT NULL,
  timeframe      TEXT NOT NULL,
  sources        TEXT NOT NULL,
  results_count  INTEGER NOT NULL DEFAULT 0,
  latency_ms     INTEGER NOT NULL DEFAULT 0,
  created_at     DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_security_search_history_created ON security_search_history(created_at DESC);

CREATE TABLE IF NOT EXISTS security_activity_events (
  id             TEXT PRIMARY KEY,
  title          TEXT NOT NULL,
  description    TEXT NOT NULL,
  category       TEXT NOT NULL,
  severity       TEXT NOT NULL,
  source         TEXT NOT NULL,
  rule           TEXT,
  channel        TEXT NOT NULL DEFAULT 'SSE',
  payload        TEXT,
  created_at     DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  acknowledged   INTEGER NOT NULL DEFAULT 0
);
CREATE INDEX IF NOT EXISTS idx_security_activity_events_created ON security_activity_events(created_at DESC);

CREATE TABLE IF NOT EXISTS widget_permissions (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  widget_id TEXT NOT NULL,
  resource_type TEXT NOT NULL,
  access_level TEXT NOT NULL CHECK (access_level IN ('none', 'read', 'write')),
  override BOOLEAN DEFAULT 0,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  UNIQUE(widget_id, resource_type)
);

-- COGNITIVE MEMORY LAYER
CREATE TABLE IF NOT EXISTS mcp_query_patterns (
  id TEXT PRIMARY KEY,
  widget_id TEXT NOT NULL,
  query_type TEXT NOT NULL,
  query_signature TEXT NOT NULL,
  source_used TEXT NOT NULL,
  latency_ms INTEGER NOT NULL,
  result_size INTEGER,
  success BOOLEAN NOT NULL,
  user_context TEXT,
  timestamp DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_query_patterns_widget 
  ON mcp_query_patterns(widget_id, timestamp DESC);
CREATE INDEX IF NOT EXISTS idx_query_patterns_signature 
  ON mcp_query_patterns(query_signature);

CREATE TABLE IF NOT EXISTS mcp_failure_memory (
  id TEXT PRIMARY KEY,
  source_name TEXT NOT NULL,
  error_type TEXT NOT NULL,
  error_message TEXT,
  error_context TEXT,
  recovery_action TEXT,
  recovery_success BOOLEAN,
  recovery_time_ms INTEGER,
  occurred_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_failure_memory_source 
  ON mcp_failure_memory(source_name, occurred_at DESC);

CREATE TABLE IF NOT EXISTS mcp_source_health (
  id TEXT PRIMARY KEY,
  source_name TEXT NOT NULL,
  health_score REAL NOT NULL,
  latency_p50 REAL,
  latency_p95 REAL,
  latency_p99 REAL,
  success_rate REAL NOT NULL,
  request_count INTEGER NOT NULL,
  error_count INTEGER NOT NULL,
  timestamp DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_source_health_source 
  ON mcp_source_health(source_name, timestamp DESC);

CREATE TABLE IF NOT EXISTS mcp_decision_log (
  id TEXT PRIMARY KEY,
  query_intent TEXT NOT NULL,
  selected_source TEXT NOT NULL,
  decision_confidence REAL NOT NULL,
  actual_latency_ms INTEGER,
  was_optimal BOOLEAN,
  timestamp DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS mcp_widget_patterns (
  id TEXT PRIMARY KEY,
  widget_id TEXT NOT NULL,
  pattern_type TEXT NOT NULL,
  pattern_data TEXT NOT NULL,
  occurrence_count INTEGER NOT NULL DEFAULT 1,
  confidence REAL NOT NULL,
  last_seen DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_widget_patterns_widget 
  ON mcp_widget_patterns(widget_id, confidence DESC);

-- PROJECT MEMORY LAYER
CREATE TABLE IF NOT EXISTS project_lifecycle_events (
  id            INTEGER PRIMARY KEY AUTOINCREMENT,
  event_type    TEXT NOT NULL,
  status        TEXT NOT NULL,
  details       TEXT,
  created_at    DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS project_features (
  id            INTEGER PRIMARY KEY AUTOINCREMENT,
  name          TEXT NOT NULL,
  description   TEXT,
  status        TEXT NOT NULL,
  created_at    DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at    DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS vector_documents (
  id            TEXT PRIMARY KEY,
  content       TEXT NOT NULL,
  embedding     TEXT,
  metadata      TEXT,
  namespace     TEXT DEFAULT 'default',
  "userId"      TEXT,
  "orgId"       TEXT,
  "createdAt"   DATETIME DEFAULT CURRENT_TIMESTAMP,
  "updatedAt"   DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_vector_documents_namespace ON vector_documents(namespace);
`;

    sqliteDb.run(fallbackSchema);
    sqliteDb.run(legacyTableBootstrap);
    sqliteReady = true;
  } catch (error) {
    console.error('❌ Failed to initialize SQLite fallback', error);
  }

  isInitialized = prismaReady || sqliteReady;
}

/**
 * Get synchronous legacy database (sql.js)
 * Falls back to an in-memory stub if initialization failed.
 */
export function getDatabase(): Database {
  if (!sqliteReady || !sqliteDb) {
    // Provide a harmless stub to avoid runtime crashes
    return {
      prepare: () => ({
        all: () => [],
        get: () => undefined,
        run: () => ({ changes: 0, lastInsertRowid: 0 }),
        free: () => undefined,
      }),
      run: () => ({ changes: 0, lastInsertRowid: 0 }),
      close: () => undefined,
    };
  }

  return {
    prepare: <P = any[], R = any>(sql: string): DatabaseStatement<P, R> => {
      const stmt = sqliteDb!.prepare(sql);
      return {
        all: (...params: any[]) => {
          stmt.bind(params);
          const rows: any[] = [];
          while (stmt.step()) {
            rows.push(stmt.getAsObject());
          }
          stmt.reset();
          return rows as R[];
        },
        get: (...params: any[]) => {
          stmt.bind(params);
          const hasRow = stmt.step();
          const row = hasRow ? stmt.getAsObject() : undefined;
          stmt.reset();
          return row as R | undefined;
        },
        run: (...params: any[]) => {
          stmt.bind(params);
          stmt.step();
          const info = { changes: sqliteDb!.getRowsModified(), lastInsertRowid: sqliteDb!.getRowsModified() };
          stmt.reset();
          return info;
        },
        free: () => stmt.free(),
      };
    },
    run: (sql: string, params?: any[]) => {
      sqliteDb!.run(sql, params);
      return { changes: sqliteDb!.getRowsModified(), lastInsertRowid: sqliteDb!.getRowsModified() };
    },
    close: () => sqliteDb!.close(),
  };
}

/**
 * Get raw sql.js Database for memory systems that need direct exec() access
 * This is needed for CognitiveMemory, PatternMemory, FailureMemory
 */
export function getSqlJsDatabase(): SqliteDatabase | null {
  return sqliteDb;
}

export async function closeDatabase(): Promise<void> {
  if (sqliteDb) {
    sqliteDb.close();
  }
  if (prismaReady) {
    await prisma.$disconnect();
  }
  isInitialized = false;
  sqliteReady = false;
  prismaReady = false;
}

export function isPrismaReady(): boolean {
  return prismaReady;
}

export function isSqliteReady(): boolean {
  return sqliteReady;
}

// Re-export prisma for convenience
export { prisma };