const db = require('./analytics-db');

/**
 * Games Database Access Layer
 * Handles all CRUD operations for game configuration
 */
class GamesDB {
    constructor() {
        // Prepare statements for better performance
        this.statements = {
            getAll: db.prepare('SELECT * FROM games ORDER BY name'),
            getEnabled: db.prepare('SELECT * FROM games WHERE enabled = 1 ORDER BY name'),
            getById: db.prepare('SELECT * FROM games WHERE id = ?'),
            insert: db.prepare(`
                INSERT INTO games (
                    id, name, description, icon, players, estimated_time,
                    enabled, featured, category, path, ai_powered, is_coming_soon, created_at, updated_at
                ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            `),
            update: db.prepare(`
                UPDATE games 
                SET name = ?, description = ?, icon = ?, players = ?, 
                    estimated_time = ?, category = ?, featured = ?, is_coming_soon = ?, updated_at = ?
                WHERE id = ?
            `),
            toggleEnabled: db.prepare(`
                UPDATE games 
                SET enabled = ?, updated_at = ?
                WHERE id = ?
            `),
            updateMaintenance: db.prepare(`
                UPDATE games 
                SET maintenance_mode = ?, maintenance_message = ?, maintenance_eta = ?, updated_at = ?
                WHERE id = ?
            `),
            delete: db.prepare('DELETE FROM games WHERE id = ?')
        };
    }

    /**
     * Get all games
     */
    getAllGames() {
        return this.statements.getAll.all();
    }

    /**
     * Get only enabled games
     */
    getEnabledGames() {
        return this.statements.getEnabled.all();
    }

    /**
     * Get game by ID
     */
    getGameById(id) {
        return this.statements.getById.get(id);
    }

    /**
     * Add new game
     */
    addGame(game) {
        const now = new Date().toISOString();
        return this.statements.insert.run(
            game.id,
            game.name,
            game.description,
            game.icon,
            game.players,
            game.estimatedTime,
            game.enabled ? 1 : 0,
            game.featured ? 1 : 0,
            game.category || 'strategy',
            game.path,
            game.aiPowered ? 1 : 0,
            game.isComingSoon ? 1 : 0,
            game.createdAt || now,
            game.updatedAt || now
        );
    }

    /**
     * Update game
     */
    updateGame(id, updates) {
        const now = new Date().toISOString();
        return this.statements.update.run(
            updates.name,
            updates.description,
            updates.icon,
            updates.players,
            updates.estimatedTime,
            updates.category,
            updates.featured ? 1 : 0,
            updates.isComingSoon ? 1 : 0,
            now,
            id
        );
    }

    /**
     * Toggle game enabled status
     */
    toggleGame(id, enabled) {
        const now = new Date().toISOString();
        return this.statements.toggleEnabled.run(enabled ? 1 : 0, now, id);
    }

    /**
     * Update game maintenance settings
     */
    updateGameMaintenance(id, maintenanceMode, message, eta) {
        const now = new Date().toISOString();
        return this.statements.updateMaintenance.run(
            maintenanceMode ? 1 : 0,
            message || '',
            eta || '',
            now,
            id
        );
    }

    /**
     * Delete game
     */
    deleteGame(id) {
        return this.statements.delete.run(id);
    }

    /**
     * Convert DB row to game object (convert INTEGER to boolean)
     */
    formatGame(row) {
        if (!row) return null;
        return {
            ...row,
            enabled: Boolean(row.enabled),
            featured: Boolean(row.featured),
            aiPowered: Boolean(row.ai_powered),
            isComingSoon: Boolean(row.is_coming_soon),
            maintenanceMode: Boolean(row.maintenance_mode),
            maintenanceMessage: row.maintenance_message || '',
            maintenanceEta: row.maintenance_eta || '',
            estimatedTime: row.estimated_time,
            playerMode: row.player_mode,
            createdAt: row.created_at,
            updatedAt: row.updated_at
        };
    }

    /**
     * Format array of games
     */
    formatGames(rows) {
        return rows.map(row => this.formatGame(row));
    }
}

module.exports = new GamesDB();
