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

"""Test API: accès aux étudiants

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_etudiants.py
"""

import requests

from tests.api.setup_test_api import (
    API_URL,
    CHECK_CERTIFICATE,
    DEPT_ACRONYM,
    api_headers,
)
from tests.api.tools_test_api import verify_fields
from tests.api.tools_test_api import ETUD_FIELDS, FSEM_FIELDS


def test_etudiants_courant(api_headers):
    """
    Route: /etudiants/courant
    """
    fields = {"id", "nip", "ine", "nom", "nom_usuel", "prenom", "civilite"}

    r = requests.get(
        API_URL + "/etudiants/courant",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    etudiants = r.json()
    assert len(etudiants) > 0

    etud = etudiants[-1]
    assert verify_fields(etud, fields) is True

    ########## Version long ################
    r = requests.get(
        API_URL + "/etudiants/courant/long",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    etudiants = r.json()
    assert len(etudiants) == 16  # HARDCODED

    etud = etudiants[-1]
    assert verify_fields(etud, ETUD_FIELDS) is True


def test_etudiant(api_headers):
    """
    Routes: /etudiant/etudid, /etudiant/nip, /etudiant/ine
    """

    ######### Test etudid #########
    r = requests.get(
        API_URL + "/etudiant/etudid/1",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    etud = r.json()

    assert verify_fields(etud, ETUD_FIELDS) is True

    ######### Test code nip #########

    r = requests.get(
        API_URL + "/etudiant/nip/1",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    etud = r.json()
    fields_ok = verify_fields(etud, ETUD_FIELDS)
    assert fields_ok is True
    assert etud["dept_acronym"] == DEPT_ACRONYM
    ######### Test code ine #########

    r = requests.get(
        API_URL + "/etudiant/ine/INE1",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    etud = r.json()
    assert len(etud) == 25
    fields_ok = verify_fields(etud, ETUD_FIELDS)
    assert fields_ok is True

    # Vérifie le requetage des 3 1er étudiants
    for etudid in (1, 2, 3):
        r = requests.get(
            f"{API_URL }/etudiant/etudid/{etudid}",
            headers=api_headers,
            verify=CHECK_CERTIFICATE,
        )
        assert r.status_code == 200
        etud = r.json()
        nip = etud["code_nip"]
        ine = etud["code_ine"]
        assert isinstance(etud["id"], int)
        assert isinstance(nip, str)
        assert isinstance(ine, str)
        r = requests.get(
            f"{API_URL }/etudiant/nip/{nip}",
            headers=api_headers,
            verify=CHECK_CERTIFICATE,
        )
        assert r.status_code == 200
        etud_nip = r.json()
        # On doit avoir obtenue le même étudiant
        assert etud_nip == etud
        r = requests.get(
            f"{API_URL }/etudiant/ine/{ine}",
            headers=api_headers,
            verify=CHECK_CERTIFICATE,
        )
        assert r.status_code == 200
        etud_ine = r.json()
        # On doit avoir obtenue le même étudiant
        assert etud_ine == etud


def test_etudiant_formsemestres(api_headers):
    """
    Route: /etudiant/etudid/<etudid:int>/formsemestres
    """

    ######### Test etudid #########

    r = requests.get(
        API_URL + "/etudiant/etudid/1/formsemestres",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    formsemestres = r.json()
    assert len(formsemestres) == 1

    formsemestre = formsemestres[0]
    assert verify_fields(formsemestre, FSEM_FIELDS) is True

    ######### Test code nip #########
    r = requests.get(
        API_URL + "/etudiant/nip/1/formsemestres",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    formsemestres = r.json()
    assert len(formsemestres) == 1

    formsemestre = formsemestres[0]
    assert verify_fields(formsemestre, FSEM_FIELDS) is True

    ######### Test code ine #########
    r = requests.get(
        API_URL + "/etudiant/ine/INE1/formsemestres",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    formsemestres = r.json()
    assert len(formsemestres) == 1

    formsemestre = formsemestres[0]
    assert verify_fields(formsemestre, FSEM_FIELDS) is True


def test_etudiant_bulletin_semestre(api_headers):
    """
    Route: /etudiant/etudid/<etudid>/formsemestre/<formsemestre_id>/bulletin
    """
    ######### Test etudid #########

    r = requests.get(
        API_URL + "/etudiant/etudid/1/formsemestre/1/bulletin",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    bul = r.json()
    assert len(bul) == 13  # HARDCODED

    ######### Test code nip #########

    r = requests.get(
        API_URL + "/etudiant/nip/1/formsemestre/1/bulletin",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    bul = r.json()
    assert len(bul) == 13  # HARDCODED

    ######### Test code ine #########
    r = requests.get(
        API_URL + "/etudiant/ine/INE1/formsemestre/1/bulletin",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    bul = r.json()
    assert len(bul) == 13  # HARDCODED

    ### --- Test étudiant inexistant
    r = requests.get(
        API_URL + "/etudiant/ine/189919919119191/formsemestre/1/bulletin",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 404


def test_etudiant_groups(api_headers):
    """
    Route:
    /etudiant/etudid/<int:etudid>/formsemestre/<int:formsemestre_id>/groups
    """
    fields = [
        "partition_id",
        "id",
        "formsemestre_id",
        "partition_name",
        "numero",
        "bul_show_rank",
        "show_in_lists",
        "group_id",
        "group_name",
    ]

    ######### Test etudid #########

    r = requests.get(
        API_URL + "/etudiant/etudid/1/formsemestre/1/groups",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    groups = r.json()
    assert len(groups) == 1  # dans un seul groupe
    group = groups[0]
    fields_ok = verify_fields(group, fields)
    assert fields_ok is True

    ######### Test code nip #########
    r = requests.get(
        API_URL + "/etudiant/nip/1/formsemestre/1/groups",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    groups = r.json()
    assert len(groups) == 1  # dans un seul groupe
    group = groups[0]
    fields_ok = verify_fields(group, fields)
    assert fields_ok is True

    ######### Test code ine #########
    r = requests.get(
        API_URL + "/etudiant/ine/INE1/formsemestre/1/groups",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    groups = r.json()
    assert len(groups) == 1  # dans un seul groupe
    group = groups[0]
    fields_ok = verify_fields(group, fields)
    assert fields_ok is True
