# -*- mode: python -*-
# -*- coding: utf-8 -*-

##############################################################################
#
# Gestion scolarite IUT
#
# Copyright (c) 1999 - 2021 Emmanuel Viennet.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#   Emmanuel Viennet      emmanuel.viennet@viennet.net
#
##############################################################################

"""Fonctions de calcul des moyennes d'UE
"""
import numpy as np
import pandas as pd

from app import db
from app import models


def df_load_ue_coefs(formation_id):
    """Load coefs of all modules in formation and returns a DataFrame
    rows = modules, columns = UE, value = coef.
    Unspecified coefs (not defined in db) are set to zero.
    """
    ues = models.UniteEns.query.filter_by(formation_id=formation_id).all()
    modules = models.Module.query.filter_by(formation_id=formation_id).all()
    ue_ids = [ue.id for ue in ues]
    module_ids = [module.id for module in modules]
    df = pd.DataFrame(columns=ue_ids, index=module_ids, dtype=float)
    for mod_coef in (
        db.session.query(models.ModuleUECoef)
        .filter(models.UniteEns.formation_id == formation_id)
        .filter(models.ModuleUECoef.ue_id == models.UniteEns.id)
    ):
        df[mod_coef.ue_id][mod_coef.module_id] = mod_coef.coef
    df.fillna(value=0, inplace=True)
    return df
