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

class TestimonialsDB {
    constructor() {
        this.statements = {
            insert: db.prepare(`
                INSERT INTO testimonials (
                    player_name, player_rank, player_avatar, message, rating, created_at
                ) VALUES (?, ?, ?, ?, ?, ?)
            `),
            getAll: db.prepare(`
                SELECT * FROM testimonials ORDER BY created_at DESC
            `),
            delete: db.prepare(`
                DELETE FROM testimonials WHERE id = ?
            `),
            update: db.prepare(`
                UPDATE testimonials 
                SET player_name = ?, player_rank = ?, player_avatar = ?, message = ?, rating = ?
                WHERE id = ?
            `)
        };
    }

    /**
     * Add a new testimonial
     */
    addTestimonial(data) {
        const now = new Date().toISOString();
        return this.statements.insert.run(
            data.playerName,
            data.playerRank,
            data.playerAvatar,
            data.message,
            data.rating || 5,
            now
        );
    }

    /**
     * Get all testimonials
     */
    getAllTestimonials() {
        return this.statements.getAll.all();
    }

    /**
     * Update a testimonial
     */
    updateTestimonial(id, data) {
        return this.statements.update.run(
            data.playerName,
            data.playerRank,
            data.playerAvatar,
            data.message,
            data.rating,
            id
        );
    }

    /**
     * Delete a testimonial
     */
    deleteTestimonial(id) {
        return this.statements.delete.run(id);
    }
}

module.exports = new TestimonialsDB();
