const { DatabaseSync } = require('node:sqlite');
const path = require('path');

const fs = require('fs');
// Resolve DB_PATH: if absolute — use as-is; if relative — resolve from backend/ dir;
// fallback to __dirname-relative default so CWD never matters.
const _rawPath = process.env.DB_PATH;
const _backendDir = path.join(__dirname, '../../');
const dbPath = _rawPath
  ? (path.isAbsolute(_rawPath) ? _rawPath : path.resolve(_backendDir, _rawPath))
  : path.join(__dirname, '../../data/learnspace.db');
const dbDir = path.dirname(dbPath);
if (!fs.existsSync(dbDir)) fs.mkdirSync(dbDir, { recursive: true });

// Auto-migrate from old location (backend/learnspace.db → backend/data/learnspace.db)
const oldPath = path.join(__dirname, '../../learnspace.db');
if (!fs.existsSync(dbPath) && fs.existsSync(oldPath)) {
  fs.copyFileSync(oldPath, dbPath);
  // Also copy WAL/SHM if present
  if (fs.existsSync(oldPath + '-wal')) fs.copyFileSync(oldPath + '-wal', dbPath + '-wal');
  if (fs.existsSync(oldPath + '-shm')) fs.copyFileSync(oldPath + '-shm', dbPath + '-shm');
  console.log('[db] Migrated database from', oldPath, '→', dbPath);
}

const db = new DatabaseSync(dbPath);

db.exec('PRAGMA journal_mode = WAL');
db.exec('PRAGMA synchronous = NORMAL');     // safe with WAL, ~5x faster than FULL
db.exec('PRAGMA foreign_keys = ON');
db.exec('PRAGMA cache_size = -16000');       // 16 MB page cache (was default ~2 MB)
db.exec('PRAGMA temp_store = MEMORY');       // temp tables in RAM, not disk

/**
 * Run a synchronous function inside a BEGIN/COMMIT/ROLLBACK transaction.
 * Returns the value returned by fn(), or rethrows on error.
 */
db.transaction = function transaction(fn) {
  return (...args) => {
    db.exec('BEGIN');
    try {
      const result = fn(...args);
      db.exec('COMMIT');
      return result;
    } catch (err) {
      db.exec('ROLLBACK');
      throw err;
    }
  };
};

module.exports = db;
