from ...utils import _test_case_client, _test_case_client_without_response
from ...fake_data import create_year_group, create_student
from app.base.mode import ModeGroups
from app.dependencies import db
from app.students.models import YearGroupStudents

valid_data = {
    'first_name': 'Dominic',
    'last_name': 'Mozart',
    'index': 123_345
}

year_group_data = [
    {
        'name': '2022/2023',
        'mode': ModeGroups.STATIONARY.value
    },
    {
        'name': '2021/2022',
        'mode': ModeGroups.STATIONARY.value
    },
    {
        'name': '2023/2024',
        'mode': ModeGroups.NON_STATIONARY.value
    },
    {
        'name': '1997/1998',
        'mode': ModeGroups.NON_STATIONARY.value
    },
]


def test_list_year_group_for_specific_student(test_app_with_context) -> None:
    with test_app_with_context.test_client() as client:
        year_groups = [create_year_group(data) for data in year_group_data]
        student = create_student(valid_data)
        for yg in year_groups[:-1]:
            db.session.add(YearGroupStudents(year_group_id=yg.id, student_index=student.index))
        db.session.commit()

        url = f'/api/students/year-group/?per_page=10&index={student.index}'
        data = _test_case_client_without_response(client, url, None, 200, method='get')
        assert data.get('max_pages') == 1
        assert len(data.get('year_groups')) == len(year_groups) - 1


def test_list_year_group_if_student_doesnt_exist(test_app_with_context) -> None:
    with test_app_with_context.test_client() as client:
        _test_case_client(client, '/api/students/year-group/?per_page=10&index=23', None, 'Not found student!', 404,
                          method='get', key='error')