# -*- 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_etudiants.py
"""
from random import randint

import requests

from tests.api.setup_test_api import SCODOC_URL, CHECK_CERTIFICATE, HEADERS
from tests.api.tools_test_api import verify_fields

# etudiants_courant
def test_etudiants_courant():

    fields = [
        "id",
        "nip",
        "nom",
        "nom_usuel",
        "prenom",
        "civilite",
    ]

    r = requests.get(
        SCODOC_URL + "/ScoDoc/api/etudiants/courant",
        headers=HEADERS,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    assert len(r.json()) == 16

    # Choisis aléatoirement un étudiant dans la liste des étudiants
    etu = r.json()[randint(0, len(r.json())) - 1]

    fields_OK = verify_fields(etu, fields)

    assert fields_OK is True

    ########## Version long################

    fields_long = [
        "civilite",
        "code_ine",
        "code_nip",
        "date_naissance",
        "email",
        "emailperso",
        "etudid",
        "nom",
        "prenom",
        "nomprenom",
        "lieu_naissance",
        "dept_naissance",
        "nationalite",
        "boursier",
        "id",
        "codepostaldomicile",
        "paysdomicile",
        "telephonemobile",
        "typeadresse",
        "domicile",
        "villedomicile",
        "telephone",
        "fax",
        "description",
    ]

    r = requests.get(
        SCODOC_URL + "/ScoDoc/api/etudiants/courant/long",
        headers=HEADERS,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    assert len(r.json()) == 16

    # Choisis aléatoirement un étudiant dans la liste des étudiants
    etu = r.json()[randint(0, len(r.json())) - 1]

    fields_OK = verify_fields(etu, fields_long)

    assert fields_OK is True


# etudiant
def test_etudiant():

    fields = [
        "civilite",
        "code_ine",
        "code_nip",
        "date_naissance",
        "email",
        "emailperso",
        "etudid",
        "nom",
        "prenom",
        "nomprenom",
        "lieu_naissance",
        "dept_naissance",
        "nationalite",
        "boursier",
        "id",
        "domicile",
        "villedomicile",
        "telephone",
        "fax",
        "description",
        "codepostaldomicile",
        "paysdomicile",
        "telephonemobile",
        "typeadresse",
    ]

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

    r = requests.get(
        SCODOC_URL + "/ScoDoc/api/etudiant/etudid/1",
        headers=HEADERS,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    assert len(r.json()) == 24

    etu = r.json()

    fields_OK = verify_fields(etu, fields)

    assert fields_OK is True

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

    r = requests.get(
        SCODOC_URL + "/ScoDoc/api/etudiant/nip/1",
        headers=HEADERS,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    assert len(r.json()) == 24

    etu = r.json()

    fields_OK = verify_fields(etu, fields)

    assert fields_OK is True

    ######### Test code ine #########

    r = requests.get(
        SCODOC_URL + "/ScoDoc/api/etudiant/ine/1",
        headers=HEADERS,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    assert len(r.json()) == 24

    etu = r.json()

    fields_OK = verify_fields(etu, fields)

    assert fields_OK is True


# etudiant_formsemestres
def test_etudiant_formsemestres():

    fields = [
        "date_fin",
        "resp_can_edit",
        "dept_id",
        "etat",
        "resp_can_change_ens",
        "id",
        "modalite",
        "ens_can_edit_eval",
        "formation_id",
        "gestion_compensation",
        "elt_sem_apo",
        "semestre_id",
        "bul_hide_xml",
        "elt_annee_apo",
        "titre",
        "block_moyennes",
        "scodoc7_id",
        "date_debut",
        "gestion_semestrielle",
        "bul_bgcolor",
        "formsemestre_id",
        "titre_num",
        "date_debut_iso",
        "date_fin_iso",
        "responsables",
    ]

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

    r = requests.get(
        SCODOC_URL + "/ScoDoc/api/etudiant/etudid/1/formsemestres",
        headers=HEADERS,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    assert len(r.json()) == 1

    formsemestre = r.json()[0]

    fields_OK = verify_fields(formsemestre, fields)

    assert fields_OK is True

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

    r = requests.get(
        SCODOC_URL + "/ScoDoc/api/etudiant/nip/1/formsemestres",
        headers=HEADERS,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    assert len(r.json()) == 1

    formsemestre = r.json()[0]

    fields_OK = verify_fields(formsemestre, fields)

    assert fields_OK is True

    ######### Test code ine #########

    r = requests.get(
        SCODOC_URL + "/ScoDoc/api/etudiant/ine/1/formsemestres",
        headers=HEADERS,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    assert len(r.json()) == 1

    formsemestre = r.json()[0]

    fields_OK = verify_fields(formsemestre, fields)

    assert fields_OK is True


# etudiant_bulletin_semestre
def test_etudiant_bulletin_semestre():

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

    r = requests.get(
        SCODOC_URL + "/ScoDoc/api/etudiant/etudid/1/formsemestre/1/bulletin",
        headers=HEADERS,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    assert len(r.json()) == 13

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

    r = requests.get(
        SCODOC_URL + "/ScoDoc/api/etudiant/nip/1/formsemestre/1/bulletin",
        headers=HEADERS,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    assert len(r.json()) == 13

    ######### Test code ine #########

    r = requests.get(
        SCODOC_URL + "/ScoDoc/api/etudiant/ine/1/formsemestre/1/bulletin",
        headers=HEADERS,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    assert len(r.json()) == 13


# etudiant_groups
def test_etudiant_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(
        SCODOC_URL + "/ScoDoc/api/etudiant/etudid/1/semestre/1/groups",
        headers=HEADERS,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    assert len(r.json()) == 1

    groups = r.json()[0]

    fields_OK = verify_fields(groups, fields)

    assert fields_OK is True

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

    r = requests.get(
        SCODOC_URL + "/ScoDoc/api/etudiant/nip/1/semestre/1/groups",
        headers=HEADERS,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    assert len(r.json()) == 1

    groups = r.json()[0]

    fields_OK = verify_fields(groups, fields)

    assert fields_OK is True

    ######### Test code ine #########

    r = requests.get(
        SCODOC_URL + "/ScoDoc/api/etudiant/ine/1/semestre/1/groups",
        headers=HEADERS,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    assert len(r.json()) == 1

    groups = r.json()[0]

    fields_OK = verify_fields(groups, fields)

    assert fields_OK is True
