const sqlite3 = require('sqlite3').verbose(); const path = require('path'); const Location = require('../models/Location'); const ProfanityWord = require('../models/ProfanityWord'); class DatabaseService { constructor() { this.mainDb = null; this.profanityDb = null; this.locationModel = null; this.profanityWordModel = null; } async initialize() { await this.initializeMainDatabase(); await this.initializeProfanityDatabase(); } async initializeMainDatabase() { return new Promise((resolve, reject) => { const dbPath = path.join(__dirname, '..', 'icewatch.db'); this.mainDb = new sqlite3.Database(dbPath, async (err) => { if (err) { console.error('Could not connect to main database', err); return reject(err); } console.log('Connected to main SQLite database.'); this.locationModel = new Location(this.mainDb); try { await this.locationModel.initializeTable(); resolve(); } catch (error) { reject(error); } }); }); } async initializeProfanityDatabase() { return new Promise((resolve, reject) => { const dbPath = path.join(__dirname, '..', 'profanity.db'); this.profanityDb = new sqlite3.Database(dbPath, async (err) => { if (err) { console.error('Could not connect to profanity database', err); return reject(err); } console.log('Connected to profanity SQLite database.'); this.profanityWordModel = new ProfanityWord(this.profanityDb); try { await this.profanityWordModel.initializeTable(); resolve(); } catch (error) { reject(error); } }); }); } getLocationModel() { if (!this.locationModel) { throw new Error('Database not initialized. Call initialize() first.'); } return this.locationModel; } getProfanityWordModel() { if (!this.profanityWordModel) { throw new Error('Database not initialized. Call initialize() first.'); } return this.profanityWordModel; } getMainDb() { return this.mainDb; } getProfanityDb() { return this.profanityDb; } close() { if (this.mainDb) { this.mainDb.close(); } if (this.profanityDb) { this.profanityDb.close(); } } } module.exports = DatabaseService;