from flask import Blueprint, Response, request
from app import mongo_utils
import tldextract
from bson import json_util
from datetime import datetime

mod_api = Blueprint('api', __name__, url_prefix='/api')


@mod_api.route('/entry/submit', methods=['POST'])
def submit_entry():
    req = request.json

    extracted = tldextract.extract(req['url'])
    domain = "{}.{}".format(extracted.domain, extracted.suffix)

    doc = {
        'url': req['url'],
        'domain': domain,
        'chromeUserId': req['chrome_user_id'],
        'text': req['text'],
        'timestamp': datetime.fromtimestamp(req['date'] / 1e3),
        "classification": req['classification'],
        "version":req['version']
    }
    mongo_utils.insert(doc)
    return Response(status=200)


@mod_api.route('/entry/get', methods=['POST'])
def get_entries():
    if 'chrome_user_id' in request.json:
        result = mongo_utils.get({}, request.json['chrome_user_id'])
    else:
        result = mongo_utils.get(request.json, None)
    return Response(response=json_util.dumps(result), status=200, mimetype="application/json")


@mod_api.route('/get-page-fact-check-requests', methods=['POST'])
def get_page_fact_check_requests():

    if 'currentUrl' in request.json:
        result = mongo_utils.find_entry_based_on_url(request.json['currentUrl'])
        return Response(response=json_util.dumps(result), status=200, mimetype="application/json")
    else:
        return Response(status=404)
@@ -666,19 +666,25 @@
 '%5D,%0A    
-   
+%7D%0A%0A    if
  %22versio
@@ -685,17 +685,50 @@
 version%22
-:
+ in req:%0A        doc%5B%22version%22%5D = 
 req%5B'ver
@@ -738,17 +738,20 @@
 n'%5D%0A    
-%7D
+    
 %0A    mon

check if version key exists in req before including it in doc