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

import requests

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


# formations
def test_formations():
    fields = [
        "id",
        "acronyme",
        "titre_officiel",
        "formation_code",
        "code_specialite",
        "dept_id",
        "titre",
        "version",
        "type_parcours",
        "referentiel_competence_id",
        "formation_id",
    ]

    r = requests.get(
        SCODOC_URL + "/ScoDoc/api/formations",
        headers=HEADERS,
        verify=CHECK_CERTIFICATE,
    )

    formation = r.json()[0]

    fields_OK = verify_fields(formation, fields)

    assert r.status_code == 200
    assert len(r.json()) == 1
    assert fields_OK is True


# formations_by_id
def test_formations_by_id():
    fields = [
        "id",
        "acronyme",
        "titre_officiel",
        "formation_code",
        "code_specialite",
        "dept_id",
        "titre",
        "version",
        "type_parcours",
        "referentiel_competence_id",
        "formation_id",
    ]

    r = requests.get(
        SCODOC_URL + "/ScoDoc/api/formations/1",
        headers=HEADERS,
        verify=CHECK_CERTIFICATE,
    )

    formation = r.json()

    fields_OK = verify_fields(formation, fields)

    assert r.status_code == 200
    assert fields_OK is True


# formation_export_by_formation_id
def test_formation_export_by_formation_id():
    fields = [
        "id",
        "acronyme",
        "titre_officiel",
        "formation_code",
        "code_specialite",
        "dept_id",
        "titre",
        "version",
        "type_parcours",
        "referentiel_competence_id",
        "formation_id",
        "ue",
    ]
    r = requests.get(
        SCODOC_URL + "/ScoDoc/api/formations/formation_export/1",
        headers=HEADERS,
        verify=CHECK_CERTIFICATE,
    )

    export_formation = r.json()

    fields_OK = verify_fields(export_formation, fields)

    assert r.status_code == 200
    assert fields_OK is True


# formsemestre_apo
# def test_formsemestre_apo():
#     r = requests.get(
#         SCODOC_URL + "/ScoDoc/api/formations/apo/<string:etape_apo>",
#         headers=HEADERS,
#         verify=CHECK_CERTIFICATE,
#     )
#     assert r.status_code == 200


# moduleimpl
def test_moduleimpl():

    fields = [
        "id",
        "formsemestre_id",
        "computation_expr",
        "module_id",
        "responsable_id",
        "moduleimpl_id",
        "ens",
        "module",
    ]

    r = requests.get(
        SCODOC_URL + "/ScoDoc/api/formations/moduleimpl/1",
        headers=HEADERS,
        verify=CHECK_CERTIFICATE,
    )

    moduleimpl = r.json()

    fields_OK = verify_fields(moduleimpl, fields)

    assert r.status_code == 200
    assert fields_OK is True


# moduleimpls_sem
def test_moduleimpls_sem():

    fields = [
        "id",
        "formsemestre_id",
        "computation_expr",
        "module_id",
        "responsable_id",
        "moduleimpl_id",
        "ens",
        "module",
        "moduleimpl_id",
        "ens",
    ]
    r = requests.get(
        SCODOC_URL + "/ScoDoc/api/formations/moduleimpl/formsemestre/1/liste",
        headers=HEADERS,
        verify=CHECK_CERTIFICATE,
    )
    moduleimpl = r.json()[0]

    fields_OK = verify_fields(moduleimpl, fields)

    assert r.status_code == 200
    assert len(r.json()) == 21
    assert fields_OK is True
