import { withTransaction } from "./db.js";
export async function mergeNotifyData(guestUserId, targetUserId) {
    await withTransaction(async (tx) => {
        await tx.query(`DELETE FROM outbox g
        WHERE g.user_id = $1 AND g.dedupe_key IS NOT NULL
          AND EXISTS (SELECT 1 FROM outbox t
                       WHERE t.user_id = $2 AND t.app_id = g.app_id
                         AND t.dedupe_key = g.dedupe_key)`, [guestUserId, targetUserId]);
        await tx.query(`UPDATE outbox SET user_id = $2 WHERE user_id = $1`, [
            guestUserId,
            targetUserId,
        ]);
        await tx.query(`INSERT INTO notify_prefs
         (user_id, app_id, enabled, muted_categories, quiet_start, quiet_end, timezone, updated_at)
       SELECT $2, app_id, enabled, muted_categories, quiet_start, quiet_end, timezone, updated_at
         FROM notify_prefs WHERE user_id = $1
       ON CONFLICT (user_id, app_id) DO NOTHING`, [guestUserId, targetUserId]);
        await tx.query(`DELETE FROM notify_prefs WHERE user_id = $1`, [guestUserId]);
        await tx.query(`UPDATE device SET user_id = $2, updated_at = now() WHERE user_id = $1`, [
            guestUserId,
            targetUserId,
        ]);
    });
}
export async function deleteNotifyData(userId, appId) {
    await withTransaction(async (tx) => {
        await tx.query(`DELETE FROM outbox WHERE user_id = $1 AND app_id = $2`, [userId, appId]);
        await tx.query(`DELETE FROM notify_prefs WHERE user_id = $1 AND app_id = $2`, [userId, appId]);
        await tx.query(`DELETE FROM device WHERE user_id = $1 AND app_id = $2`, [userId, appId]);
    });
}
