import os
from collections import defaultdict

from flask import render_template
from flask_login import login_required
from sqlalchemy import and_

from app import db
from app.decorators import operator_required
from app.models import Student, MonthNameList, Course, PaymentStatus, Payment, Teacher, Schedule
from app.users.operator import operator


@operator.route('/')
@login_required
@operator_required
def index():
    title = os.environ.get('APP_NAME')

    # get all students data on schedule, except if the student tuition payment is None, PENDING, REJECTED or WARNING_3
    students_courses_data = db.session.query(Schedule, Payment).join(Payment).filter(
        and_(Payment.status_of_payment is not None,
             Payment.status_of_payment != PaymentStatus.PENDING.name,
             Payment.status_of_payment != PaymentStatus.REJECTED.name,
             Payment.status_of_payment != PaymentStatus.WARNING_3.name))

    # get the amount of Teachers and Students
    total_students = Student.query.count()
    total_teachers = Teacher.query.count()

    month_name_list = []
    for data in MonthNameList:
        month_name_list.append(str(data))

    # make a query object for "Tahsin" and "Arabic Language" course
    tahsin = students_courses_data.join(Course).filter(Course.name == "Tahsin")
    arabic = students_courses_data.join(Course).filter(Course.name == "Bahasa Arab")

    # the total payment for the courses each month
    tahsin_course_data = []
    arabic_course_data = []
    for data in tahsin:
        for month_name in month_name_list:
            tahsin_course_data.append({str(month_name): data.Payment.created_at.strftime('%B').count(month_name)})
    for data in arabic:
        for month_name in month_name_list:
            arabic_course_data.append({str(month_name): data.Payment.created_at.strftime('%B').count(month_name)})

    # merge and sum the total value from the dictionary on the same month from the _courses_data result above
    total_tahsin_students_per_month = defaultdict(int)
    total_arabic_students_per_month = defaultdict(int)
    for d in tahsin_course_data:
        for key, value in d.items():
            total_tahsin_students_per_month[key] += value
    for d in arabic_course_data:
        for key, value in d.items():
            total_arabic_students_per_month[key] += value

    # store all of the month values on a list for each course
    tahsin_values = []
    arabic_values = []
    for key, value in total_tahsin_students_per_month.items():
        tahsin_values.append(value)
    for key, value in total_arabic_students_per_month.items():
        arabic_values.append(value)

    # make a dictionary to represent course name with the matching total student that do the payment for each month
    data_courses_each_month = [
        {
            'Tahsin': tahsin_values,
        },
        {
            'Bahasa Arab': arabic_values
        }
    ]

    return render_template('main/operator/operator-dashboard.html', title=title, total_teachers=total_teachers,
                           total_students=total_students, month_name_list=month_name_list,
                           data_courses_each_month=data_courses_each_month)

[((353, 372), 'app.users.operator.operator.route', 'operator.route', (['"""/"""'], {}), "('/')\n", (367, 372), False, 'from app.users.operator import operator\n'), ((433, 459), 'os.environ.get', 'os.environ.get', (['"""APP_NAME"""'], {}), "('APP_NAME')\n", (447, 459), False, 'import os\n'), ((1000, 1021), 'app.models.Student.query.count', 'Student.query.count', ([], {}), '()\n', (1019, 1021), False, 'from app.models import Student, MonthNameList, Course, PaymentStatus, Payment, Teacher, Schedule\n'), ((1043, 1064), 'app.models.Teacher.query.count', 'Teacher.query.count', ([], {}), '()\n', (1062, 1064), False, 'from app.models import Student, MonthNameList, Course, PaymentStatus, Payment, Teacher, Schedule\n'), ((2019, 2035), 'collections.defaultdict', 'defaultdict', (['int'], {}), '(int)\n', (2030, 2035), False, 'from collections import defaultdict\n'), ((2074, 2090), 'collections.defaultdict', 'defaultdict', (['int'], {}), '(int)\n', (2085, 2090), False, 'from collections import defaultdict\n'), ((2940, 3166), 'flask.render_template', 'render_template', (['"""main/operator/operator-dashboard.html"""'], {'title': 'title', 'total_teachers': 'total_teachers', 'total_students': 'total_students', 'month_name_list': 'month_name_list', 'data_courses_each_month': 'data_courses_each_month'}), "('main/operator/operator-dashboard.html', title=title,\n    total_teachers=total_teachers, total_students=total_students,\n    month_name_list=month_name_list, data_courses_each_month=\n    data_courses_each_month)\n", (2955, 3166), False, 'from flask import render_template\n'), ((674, 900), 'sqlalchemy.and_', 'and_', (['(Payment.status_of_payment is not None)', '(Payment.status_of_payment != PaymentStatus.PENDING.name)', '(Payment.status_of_payment != PaymentStatus.REJECTED.name)', '(Payment.status_of_payment != PaymentStatus.WARNING_3.name)'], {}), '(Payment.status_of_payment is not None, Payment.status_of_payment !=\n    PaymentStatus.PENDING.name, Payment.status_of_payment != PaymentStatus.\n    REJECTED.name, Payment.status_of_payment != PaymentStatus.WARNING_3.name)\n', (678, 900), False, 'from sqlalchemy import and_\n'), ((608, 643), 'app.db.session.query', 'db.session.query', (['Schedule', 'Payment'], {}), '(Schedule, Payment)\n', (624, 643), False, 'from app import db\n')]