const db = require('../config/database');

const DeviceToken = {
    // Upsert a token for a user
    upsert: (
        userId,
        token,
        platform = 'android',
        isActive = true,
        pushEnabled = true,
        soundEnabled = true,
        vibrationEnabled = true,
        callback
    ) => {
        const query = `
            INSERT INTO device_tokens (user_id, token, platform, is_active, push_enabled, sound_enabled, vibration_enabled)
            VALUES (?, ?, ?, ?, ?, ?, ?)
            ON DUPLICATE KEY UPDATE
                user_id = VALUES(user_id),
                platform = VALUES(platform),
                is_active = VALUES(is_active),
                push_enabled = VALUES(push_enabled),
                sound_enabled = VALUES(sound_enabled),
                vibration_enabled = VALUES(vibration_enabled),
                updated_at = CURRENT_TIMESTAMP
        `;
        db.query(
            query,
            [userId, token, platform, isActive, pushEnabled, soundEnabled, vibrationEnabled],
            callback
        );
    },

    // Deactivate a token (keeps a record for audit)
    deactivate: (userId, token, callback) => {
        const query = `
            UPDATE device_tokens
            SET is_active = FALSE
            WHERE user_id = ? AND token = ?
        `;
        db.query(query, [userId, token], callback);
    },

    // Delete a token
    delete: (userId, token, callback) => {
        const query = `DELETE FROM device_tokens WHERE user_id = ? AND token = ?`;
        db.query(query, [userId, token], callback);
    },

    // Get active tokens for a user
    findActiveByUserId: (userId, callback) => {
        const query = `
            SELECT token, platform, push_enabled, sound_enabled, vibration_enabled
            FROM device_tokens
            WHERE user_id = ? AND is_active = TRUE
        `;
        db.query(query, [userId], callback);
    },

    // Get tokens for a user (all)
    findByUserId: (userId, callback) => {
        const query = `
            SELECT token, platform, is_active, push_enabled, sound_enabled, vibration_enabled, created_at, updated_at
            FROM device_tokens
            WHERE user_id = ?
            ORDER BY updated_at DESC
        `;
        db.query(query, [userId], callback);
    },

    updatePreferences: (userId, token, prefs, callback) => {
        const updates = [];
        const values = [];

        if (prefs.push_enabled !== undefined) {
            updates.push('push_enabled = ?');
            values.push(!!prefs.push_enabled);
        }
        if (prefs.sound_enabled !== undefined) {
            updates.push('sound_enabled = ?');
            values.push(!!prefs.sound_enabled);
        }
        if (prefs.vibration_enabled !== undefined) {
            updates.push('vibration_enabled = ?');
            values.push(!!prefs.vibration_enabled);
        }

        if (!updates.length) {
            return callback(new Error('No preferences provided'), null);
        }

        values.push(userId, token);
        const query = `
            UPDATE device_tokens
            SET ${updates.join(', ')}, updated_at = CURRENT_TIMESTAMP
            WHERE user_id = ? AND token = ?
        `;
        db.query(query, values, callback);
    }
};

module.exports = DeviceToken;
