import pool from '../config/db.js';

export class SystemService {
    static async checkHealth() {
        await pool.query('SELECT 1');
        return {
            status: 'ok',
            version: 'v4-secured-full-PROD',
            database: 'connected',
            timestamp: new Date().toISOString()
        };
    }

    static async getDbSummary() {
        const tables = [
            'paper_types', 'materials', 'finishing_options', 'printing_options', 
            'chatbots', 'customers', 'suppliers', 'orders'
        ];
        const summary = {};
        for (const table of tables) {
            const [rows] = await pool.query(`SELECT COUNT(*) as count FROM ${table}`);
            summary[table] = rows[0].count;
        }
        return summary;
    }

    static async getSettings() {
        const [rows] = await pool.execute('SELECT * FROM app_settings');
        return rows;
    }

    static async updateSetting(key, value) {
        const jsonValue = typeof value === 'object' ? JSON.stringify(value) : value;
        await pool.execute(
            'INSERT INTO app_settings (id, value) VALUES (?, ?) ON DUPLICATE KEY UPDATE value = ?',
            [key, jsonValue, jsonValue]
        );
    }
}
