
# !page: admin_register
# !latest updated: 09Jul2021

from flask import render_template, request, session, redirect, url_for, flash, Blueprint, jsonify
from functions.notification_function import fcmBroadcastNotification
from htmlmin.minify import html_minify


# ? export blueprint section #################################################################################################################################

admin_send_notification = Blueprint("admin_send_notification", __name__, static_folder="static",
                                    template_folder="templates")


@admin_send_notification.route('/')
def notiIndex():
    if 'innerSession' in session:
        return html_minify(render_template('admin_notification.html', title='noti'))
    return redirect(url_for('admin_login.adminLogin'))


@admin_send_notification.route('/push-noti', methods=['POST'])
def sendPushNotification():
    if 'innerSession' in session:
        title = request.form['title']
        message = request.form['message']
        resource = {'title': title, 'message': message}
        fcmBroadcastNotification(resource)
        flash('Broadcast notification is successfully sent.', 'success')
        return jsonify({"message": "successfully send", 'status': 'success'}), 200
    return jsonify({"message": 'unauthorized access'}), 401
