# -*- coding: utf-8 -*-

"""Test Logos

Utilisation :
    créer les variables d'environnement: (indiquer les valeurs
    pour le serveur ScoDoc que vous voulez interroger)

    export SCODOC_URL="https://scodoc.xxx.net/"
    export SCODOC_USER="xxx"
    export SCODOC_PASSWD="xxx"
    export CHECK_CERTIFICATE=0 # ou 1 si serveur de production avec certif SSL valide

    (on peut aussi placer ces valeurs dans un fichier .env du répertoire tests/api).

    Lancer :
        pytest tests/api/test_api_evaluations.py
"""

import requests

from app.scodoc import sco_utils as scu
from tests.api.setup_test_api import API_URL, CHECK_CERTIFICATE, api_headers
from tests.api.tools_test_api import (
    verify_fields,
    EVALUATIONS_FIELDS,
    EVALUATION_FIELDS,
)


def test_evaluations(api_headers):
    """
    Test 'evaluations'

    Route :
        - /moduleimpl/<int:moduleimpl_id>/evaluations
    """
    moduleimpl_id = 1
    r = requests.get(
        f"{API_URL}/moduleimpl/{moduleimpl_id}/evaluations",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
        timeout=scu.SCO_TEST_API_TIMEOUT,
    )
    assert r.status_code == 200
    list_eval = r.json()
    assert isinstance(list_eval, list)
    for eval in list_eval:
        assert verify_fields(eval, EVALUATIONS_FIELDS) is True
        assert isinstance(eval["id"], int)
        assert isinstance(eval["note_max"], float)
        assert isinstance(eval["visi_bulletin"], bool)
        assert isinstance(eval["evaluation_type"], int)
        assert isinstance(eval["moduleimpl_id"], int)
        assert eval["description"] is None or isinstance(eval["description"], str)
        assert isinstance(eval["coefficient"], float)
        assert isinstance(eval["publish_incomplete"], bool)
        assert isinstance(eval["numero"], int)
        assert eval["date_debut"] is None or isinstance(eval["date_debut"], str)
        assert eval["date_fin"] is None or isinstance(eval["date_fin"], str)
        assert isinstance(eval["poids"], dict)

        assert eval["moduleimpl_id"] == moduleimpl_id


def test_evaluation_notes(
    api_headers,
):  # XXX TODO changer la boucle pour parcourir le dict sans les indices
    """
    Test 'evaluation_notes'

    Route :
        - /evaluation/<int:evaluation_id>/notes
    """
    eval_id = 1
    r = requests.get(
        f"{API_URL}/evaluation/{eval_id}/notes",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
        timeout=scu.SCO_TEST_API_TIMEOUT,
    )
    assert r.status_code == 200
    eval_notes = r.json()
    for i in range(1, len(eval_notes)):
        assert verify_fields(eval_notes[f"{i}"], EVALUATION_FIELDS)
        assert isinstance(eval_notes[f"{i}"]["id"], int)
        assert isinstance(eval_notes[f"{i}"]["etudid"], int)
        assert isinstance(eval_notes[f"{i}"]["evaluation_id"], int)
        assert isinstance(eval_notes[f"{i}"]["value"], float)
        assert isinstance(eval_notes[f"{i}"]["comment"], str)
        assert isinstance(eval_notes[f"{i}"]["date"], str)
        assert isinstance(eval_notes[f"{i}"]["uid"], int)

        assert eval_id == eval_notes[f"{i}"]["evaluation_id"]
