-- 016_encryption_alerts_unique.sql: Add UNIQUE constraint to prevent duplicate alerts

-- Remove existing duplicates, keeping the earliest alert per device/drive/type/status
DELETE FROM encryption_alerts WHERE id NOT IN (
    SELECT MIN(id) FROM encryption_alerts GROUP BY device_uid, drive_letter, alert_type, status
);

-- Add unique index so ON CONFLICT DO NOTHING works correctly
CREATE UNIQUE INDEX IF NOT EXISTS idx_encryption_alerts_unique
    ON encryption_alerts(device_uid, drive_letter, alert_type, status);
