# -*- 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_departements.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

DEPARTEMENT_FIELDS = [
    "id",
    "acronym",
    "description",
    "visible",
    "date_creation",
]


def test_departements(api_headers):
    """ "
    Routes: /departements_ids, /departement, /departement/<string:dept>/formsemestres_ids

    """
    # --- Liste des ids
    r = requests.get(
        API_URL + "/departements_ids",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    departements_ids = r.json()
    assert isinstance(departements_ids, list)
    assert len(departements_ids) > 0
    assert all(isinstance(x, int) for x in departements_ids)

    dept_id = departements_ids[0]
    # --- Infos sur un département, accès par id
    r = requests.get(
        f"{API_URL}/departement/{dept_id}",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    dept_a = r.json()
    assert verify_fields(dept_a, DEPARTEMENT_FIELDS) is True
    # --- Infos sur un département, accès par acronyme4
    r = requests.get(
        f"{API_URL}/departement/{dept_a['acronym']}",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    dept_b = r.json()
    assert dept_a == dept_b

    # Liste des formsemestres
    r = requests.get(
        f"{API_URL}/departement/{dept_a['acronym']}/formsemestres_ids",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    dept_ids = r.json()
    assert isinstance(dept_ids, list)
    assert all(isinstance(x, int) for x in dept_ids)
    assert len(dept_ids) > 0
    assert dept_id in dept_ids


def test_list_etudiants(api_headers):
    fields = {"id", "nip", "ine", "nom", "nom_usuel", "prenom", "civilite"}

    r = requests.get(
        f"{API_URL}/departement/{DEPT_ACRONYM}/etudiants",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    etud = r.json()[0]
    assert verify_fields(etud, fields) is True


# liste_semestres_courant
def test_semestres_courant(api_headers):
    fields = [
        "titre",
        "gestion_semestrielle",
        "scodoc7_id",
        "date_debut",
        "bul_bgcolor",
        "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",
        "block_moyennes",
        "formsemestre_id",
        "titre_num",
        "titre_formation",
        "date_debut_iso",
        "date_fin_iso",
        "responsables",
    ]
    dept_id = 1
    r = requests.get(
        f"{API_URL}/departement/{dept_id}",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    dept = r.json()
    assert dept["id"] == dept_id
    # Accès via acronyme
    r = requests.get(
        f"{API_URL}/departement/{dept['acronym']}/formsemestres_courants",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    result_a = r.json()
    assert isinstance(result_a, list)  # liste de formsemestres
    assert len(result_a) > 0
    sem = result_a[0]
    assert verify_fields(sem, fields) is True

    # accès via dept_id
    r = requests.get(
        f"{API_URL}/departement/{dept['id']}/formsemestres_courants",
        headers=api_headers,
        verify=CHECK_CERTIFICATE,
    )
    assert r.status_code == 200
    result_b = r.json()
    assert result_a == result_b
