package org.restheart.examples;

import java.util.Map;
import com.mashape.unirest.http.Unirest;

import org.bson.json.JsonWriterSettings;
import org.restheart.exchange.MongoRequest;
import org.restheart.exchange.MongoResponse;
import org.restheart.plugins.InjectConfiguration;
import org.restheart.plugins.InterceptPoint;
import org.restheart.plugins.MongoInterceptor;
import org.restheart.plugins.RegisterPlugin;
import static org.restheart.utils.GsonUtils.object;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RegisterPlugin(
        name = "slackNotifierInterceptor",
        description = "sends a message to a slack channel upon document creation",
        interceptPoint = InterceptPoint.RESPONSE_ASYNC,
        enabledByDefault = true)
public class SlackNotifierInterceptor implements MongoInterceptor {
    private static final Logger LOGGER = LoggerFactory.getLogger(SlackNotifierInterceptor.class);

    private String channel = null;
    private String oauthToken = null;
    private String db = null;
    private String collection = null;

    @InjectConfiguration
    public void init(Map<String, Object> config) {
        this.channel = arg(config, "channel");
        this.oauthToken = arg(config, "oauth-token");
        this.db = arg(config, "db");
        this.collection = arg(config, "collection");

        LOGGER.debug("SlackNotifierInterceptor initialized with channel: {}, oauthToken: {}, db: {}, collection: {}", channel, oauthToken, db, collection);
    }
    @Override
    public void handle(MongoRequest request, MongoResponse response) throws Exception {
        var doc = response.getDbOperationResult().getNewData();

        var body = object()
            .put("text", ":tada: New document\n```" + doc.toJson(JsonWriterSettings.builder().indent(true).build()) + "```")
            .put("channel", this.channel);

        var resp = Unirest.post("https://slack.com/api/chat.postMessage")
            .header("Authorization", "Bearer " + this.oauthToken)
            .header("Content-Type", "application/json")
            .body(body)
            .asJson();

        LOGGER.debug("SlackNotifierInterceptor sent message to slack channel: {} with status {}", resp.getBody(), resp.getStatus());
    }

    @Override
    public boolean resolve(MongoRequest request, MongoResponse response) {
        return request.isPost() && request.isCollection()
            && this.collection.equals(request.getCollectionName())
            && this.db.equals(request.getDBName())
            && response.getDbOperationResult() != null
            && response.getDbOperationResult().getNewData() != null;
    }
}

@@ -37,62 +37,74 @@
 ort 
-java.util.Map;%0Aimport com.mashape.unirest.http.Unirest
+static org.restheart.utils.GsonUtils.object;%0A%0Aimport java.util.Map
 ;%0A%0Ai
@@ -423,60 +423,8 @@
 in;%0A
-import static org.restheart.utils.GsonUtils.object;%0A
 impo
@@ -477,16 +477,119 @@
 ctory;%0A%0A
+import com.mashape.unirest.http.Unirest;%0Aimport com.mashape.unirest.http.exceptions.UnirestException;%0A%0A
 @Registe
@@ -1396,37 +1396,36 @@
 %0A        LOGGER.
-debug
+info
 (%22SlackNotifierI
@@ -1505,16 +1505,32 @@
 on: %7B%7D%22,
+%0A               
  channel
@@ -1566,16 +1566,17 @@
 ;%0A    %7D%0A
+%0A
     @Ove
@@ -1657,16 +1657,23 @@
  throws 
+Unirest
 Exceptio
@@ -1777,24 +1777,28 @@
             
+    
 .put(%22text%22,
@@ -1797,16 +1797,40 @@
 (%22text%22,
+%0A                       
  %22:tada:
@@ -1912,16 +1912,48 @@
 build())
+%0A                               
  + %22%60%60%60%22
@@ -1954,16 +1954,20 @@
  %22%60%60%60%22)%0A
+    
         
@@ -2080,32 +2080,36 @@
 e%22)%0A            
+ 
+   
 .header(%22Authori
@@ -2150,32 +2150,36 @@
 en)%0A            
+    
 .header(%22Content
@@ -2218,16 +2218,20 @@
         
+ 
+   
 .body(bo
@@ -2234,16 +2234,20 @@
 y(body)%0A
+    
         
@@ -2281,13 +2281,12 @@
 GER.
-debug
+info
 (%22Sl
@@ -2374,16 +2374,32 @@
 tBody(),
+%0A               
  resp.ge
@@ -2568,32 +2568,36 @@
 n()%0A            
+    
 && this.collecti
@@ -2627,32 +2627,36 @@
 llectionName())%0A
+    
             && t
@@ -2694,32 +2694,36 @@
 ())%0A            
+    
 && response.getD
@@ -2749,16 +2749,20 @@
 != null%0A
+    
         

Set log level to INFO