from app.dependencies import db

from ...fake_data import create_groups, create_project_supervisors, create_year_group
from ...utils import _test_case_client_without_response, assert_model_changes


def test_list_year_group_for_specific_student(test_app_with_context) -> None:
    with test_app_with_context.test_client() as client:
        year_group = create_year_group()
        amount_of_project_supervisors = 3
        project_supervisors = create_project_supervisors(
            year_group, amount_of_project_supervisors
        )
        groups = create_groups(year_group, 6)

        for i in range(1, 4):
            for _ in range(i):
                gr = groups.pop()
                gr.project_supervisor_id = project_supervisors[i - 1].id
        db.session.commit()

        url = f"/api/students/registrations/{year_group.id}/?per_page=10"
        data = _test_case_client_without_response(client, url, None, 200, method="get")
        assert data.get("max_pages") == 1
        project_supervisors_data = data.get("project_supervisors")
        assert len(project_supervisors_data) == amount_of_project_supervisors

        for ps, expected_available_groups in zip(project_supervisors, [2, 1, 0]):
            ps_dict = list(
                filter(lambda p: p.get("email") == ps.email, project_supervisors_data)
            )[0]
            assert ps_dict.get("available_groups") == expected_available_groups