import { SystemService } from '../services/SystemService.js';
import { safeError } from '../utils/errorHandler.js';

export class SystemController {
    static async checkHealth(req, res) {
        try {
            const result = await SystemService.checkHealth();
            res.json(result);
        } catch (error) {
            console.error('❌ Database Health Check Failed:', error.message);
            res.status(500).json({ status: 'error', database: 'disconnected' });
        }
    }

    static async getDbSummary(req, res) {
        try {
            const summary = await SystemService.getDbSummary();
            res.json({
                database: process.env.DB_NAME,
                host: process.env.DB_HOST,
                summary
            });
        } catch (error) {
            safeError(res, error);
        }
    }

    static async getSettings(req, res) {
        try {
            const settings = await SystemService.getSettings();
            res.json(settings);
        } catch (error) {
            safeError(res, error);
        }
    }

    static async updateSetting(req, res) {
        try {
            const { key } = req.params;
            const { value } = req.body;
            await SystemService.updateSetting(key, value);
            res.json({ success: true });
        } catch (error) {
            safeError(res, error);
        }
    }
}
