"""
Test modèles évaluations avec poids BUT
"""

from tests.unit import sco_fake_gen
from app import db
from app import models

"""
mapp.set_sco_dept("RT")
from app.auth.models import get_super_admin
admin_user = get_super_admin()
ctx.push()
login_user(admin_user)
"""


def setup_formation_test():
    G = sco_fake_gen.ScoFake(verbose=False)
    _f = G.create_formation(
        acronyme="F3", titre="Formation 2", titre_officiel="Titre officiel 2"
    )
    _ue1 = G.create_ue(formation_id=_f["formation_id"], acronyme="UE1", titre="ue 1")
    _ue2 = G.create_ue(formation_id=_f["formation_id"], acronyme="UE2", titre="ue 2")
    _ue3 = G.create_ue(formation_id=_f["formation_id"], acronyme="UE3", titre="ue 3")
    _mat = G.create_matiere(ue_id=_ue1["ue_id"], titre="matière test")
    _mod = G.create_module(
        matiere_id=_mat["matiere_id"],
        code="TSM1",
        coefficient=1.0,
        titre="module test",
        ue_id=_ue1["ue_id"],
        formation_id=_f["formation_id"],
    )
    return G, _f["id"], _ue1["id"], _ue2["id"], _ue3["id"], _mod["id"]


def test_evaluation_poids(test_client):
    """Association de poids vers les UE"""
    G, formation_id, ue1_id, ue2_id, ue3_id, module_id = setup_formation_test()
    sem = G.create_formsemestre(
        formation_id=formation_id,
        semestre_id=1,
        date_debut="01/01/2021",
        date_fin="30/06/2021",
    )  # formsemestre_id=716
    mi = G.create_moduleimpl(
        module_id=module_id,
        formsemestre_id=sem["formsemestre_id"],
    )
    moduleimpl_id = mi["id"]
    _e1 = G.create_evaluation(
        moduleimpl_id=moduleimpl_id,
        jour="01/01/2021",
        description="evaluation 1",
        coefficient=0,
    )
    evaluation_id = _e1["evaluation_id"]  # evaluation_id=25246
    # ue1_id=1684
    # formation_id=199
    #
    e1 = models.Evaluation.query.get(evaluation_id)
    ue1 = models.UniteEns.query.get(ue1_id)
    assert e1.ue_poids == []
    p1 = 3.14
    e1.set_ue_poids(ue1, p1)
    db.session.commit()
    assert e1.get_ue_poids_dict()[ue1_id] == p1
    ues = models.UniteEns.query.filter_by(formation_id=formation_id).all()
    poids = [1.0, 2.0, 3.0]
    for (ue, p) in zip(ues, poids):
        e1.set_ue_poids(ue, p)
    assert len(e1.ue_poids) == len(ues)
    assert e1.get_ue_poids_dict()[ues[1].id] == poids[1]
    e1.set_ue_poids(ue1, p1)
    db.session.commit()
    poids2 = [10, 20]
    e1.update_ue_poids_dict({ue.id: p for (ue, p) in zip(ues[:-1], poids2)})
    assert e1.get_ue_poids_dict()[ues[0].id] == poids2[0]
    assert e1.get_ue_poids_dict()[ues[1].id] == poids2[1]
    assert e1.get_ue_poids_dict()[ues[2].id] == poids[2]
    # Delete UE
    db.session.delete(ues[2])
    db.session.commit()
    # Delete eval
    db.session.delete(e1)
    db.session.commit()
    assert len(models.EvaluationUEPoids.query.all()) == 0


def test_modules_coefs(test_client):
    """Coefs vers les UE (BUT)"""
    G, formation_id, ue1_id, ue2_id, ue3_id, module_id = setup_formation_test()
    ue1 = models.UniteEns.query.get(ue1_id)
    ue2 = models.UniteEns.query.get(ue2_id)
    mod = models.Module.query.get(module_id)
    coef = 2.5
    mod.set_ue_coef(ue1, coef)
    db.session.commit()
    assert mod.ue_coefs[0].coef == coef
    mod.set_ue_coef(ue2, 2 * coef)
    db.session.commit()
    assert set(mod.get_ue_coef_dict().values()) == {coef, 2 * coef}
    assert set(mod.get_ue_coef_dict().keys()) == {ue1_id, ue2_id}
    mod.delete_ue_coef(ue1)
    db.session.commit()
    assert len(mod.ue_coefs) == 1
    # Gestion des coefs nuls:
    mod.set_ue_coef(ue2, 0.0)
    db.session.commit()
    assert len(mod.ue_coefs) == 0
