# -*- 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_formsemestre.py
"""

import requests
from app.api.formsemestres import formsemestre

from tests.api.setup_test_api import API_URL, CHECK_CERTIFICATE, api_headers
from tests.api.tools_test_api import MODIMPL_FIELDS, verify_fields
from tests.api.tools_test_api import FSEM_FIELDS, UE_FIELDS, MODULE_FIELDS

# Etudiant pour les tests
ETUDID = 1
NIP = "1"
INE = "INE1"


def test_formsemestre(api_headers):
    """
    Route: /formsemestre/<id>
    """
    r = requests.get(
        API_URL + "/formsemestre/1",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    formsemestre = r.json()
    assert verify_fields(formsemestre, FSEM_FIELDS)


def test_etudiant_bulletin(api_headers):
    """
    Route:
    """
    formsemestre_id = 1
    r = requests.get(
        f"{API_URL}/etudiant/etudid/1/formsemestre/{formsemestre_id}/bulletin",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    bull_a = r.json()

    r = requests.get(
        f"{API_URL}/etudiant/nip/{NIP}/formsemestre/{formsemestre_id}/bulletin",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    bull_b = r.json()

    r = requests.get(
        f"{API_URL}/etudiant/ine/{INE}/formsemestre/{formsemestre_id}/bulletin",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    bull_c = r.json()
    # elimine les dates de publication pour comparer les autres champs
    del bull_a["date"]
    del bull_b["date"]
    del bull_c["date"]
    assert bull_a == bull_b == bull_c


def test_bulletins(api_headers):
    """
    Route:
    """
    r = requests.get(
        API_URL + "/formsemestre/1/bulletins",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200


# # jury
# def test_jury():
#     r = requests.get(
#         API_URL + "/formsemestre/1/jury",
#         headers=api_headers,
#         verify=CHECK_CERTIFICATE,
#     )
#     assert r.status_code == 200


def test_formsemestre_programme(api_headers):
    """
    Route:  /formsemestre/1/programme
    """

    r = requests.get(
        API_URL + "/formsemestre/1/programme",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    prog = r.json()
    assert isinstance(prog, dict)
    assert "ues" in prog
    assert "modules" in prog
    assert "ressources" in prog
    assert "saes" in prog
    assert isinstance(prog["ues"], list)
    assert isinstance(prog["modules"], list)
    ue = prog["ues"][0]
    modules = prog["modules"]
    # Il y a toujours au moins une SAE et une ressources dans notre base de test
    ressource = prog["ressources"][0]
    sae = prog["saes"][0]

    assert verify_fields(ue, UE_FIELDS)
    if len(modules) > 1:
        assert verify_fields(modules[0], MODIMPL_FIELDS)
    assert verify_fields(ressource, MODIMPL_FIELDS)
    assert verify_fields(sae, MODIMPL_FIELDS)