// src/db/mappings.js
//
// Fallback SQLite handle used when a caller doesn't inject its own connection.
//
// SAFETY NOTE (Production Constraint - 2026-05-28):
// The live production database file must never be moved or renamed. The
// canonical path is resolved by `getDbPath()` in ./path.js; both this module
// and the main entrypoint call that helper so they always agree.
//
// The primary bot process (index.js) opens the DB once and injects it into
// every service that needs it. This module exists so utilities that can be
// invoked outside the main process (or in isolation, e.g. via a script) still
// have a working handle.
//
// The mappings table itself is created by the main entrypoint at startup —
// we intentionally do NOT run CREATE TABLE here anymore, to avoid schema
// drift (an earlier version of this file declared a `storeNumber` column that
// the production DB never had, which caused inserts to fail silently).

import sqlite3 from 'sqlite3';
import { logger } from '../utils/logger.js';
import { getDbPath } from './path.js';

const resolvedDbPath = getDbPath();

const db = new sqlite3.Database(resolvedDbPath, (err) => {
  if (err) {
    logger('db:mappings', `Failed to open SQLite DB at ${resolvedDbPath}: ${err.message}`, 'error');
  } else {
    logger('db:mappings', `SQLite DB connected/opened at ${resolvedDbPath}`);
  }
});

export default db;
