const dbManager = require('../database/DatabaseManager');

class DeletionLogRepository {
    
    // Erstellt Lösch-Protokoll
    async createDeletionEntry(logData) {
        const result = await dbManager.run(`
            INSERT INTO deletion_log (group_id, year, image_count, upload_date, deletion_reason, total_file_size)
            VALUES (?, ?, ?, ?, ?, ?)
        `, [
            logData.groupId,
            logData.year,
            logData.imageCount,
            logData.uploadDate,
            logData.deletionReason || 'auto_cleanup_7days',
            logData.totalFileSize || null
        ]);

        return result.id;
    }
    
    // Hole letzte N Einträge
    async getRecentDeletions(limit = 10) {
        const deletions = await dbManager.all(`
            SELECT * FROM deletion_log
            ORDER BY deleted_at DESC
            LIMIT ?
        `, [limit]);

        return deletions;
    }
    
    // Hole alle Einträge (für Admin-Übersicht)
    async getAllDeletions() {
        const deletions = await dbManager.all(`
            SELECT * FROM deletion_log
            ORDER BY deleted_at DESC
        `);

        return deletions;
    }
    
    // Statistiken (Anzahl gelöschte Gruppen, Bilder, Speicherplatz)
    async getDeletionStatistics() {
        const stats = await dbManager.get(`
            SELECT 
                COUNT(*) as totalDeleted,
                SUM(image_count) as totalImages,
                SUM(total_file_size) as totalSize,
                MAX(deleted_at) as lastCleanup
            FROM deletion_log
        `);

        return {
            totalDeleted: stats.totalDeleted || 0,
            totalImages: stats.totalImages || 0,
            totalSize: stats.totalSize || 0,
            lastCleanup: stats.lastCleanup || null
        };
    }
}

module.exports = new DeletionLogRepository();
