import copy

from flask import current_app

from ...utils import _test_case_client, _test_case_client_without_response, assert_model_changes, _test_case_group
from ...fake_data import create_year_group, create_groups, create_group, create_students, create_project_supervisors
from ....app.dependencies import db
from ....app.students.models import Group
from ....app.project_supervisor.models import YearGroupProjectSupervisors

valid_data = {
    'name': 'System Pri'
}

new_data = {
    'name': 'Mobile app'
}

invalid_data = {
    'name': 'Mobile app v2',
    'students': [123_344, 455_444],
    'project_supervisor_id': 1
}


def test_list_groups(test_app_with_context) -> None:
    with test_app_with_context.test_client() as client:
        yg = create_year_group()
        create_groups(yg, 33)
        data = _test_case_client_without_response(client, f'/api/coordinator/groups/{yg.id}/?per_page=10', None, 200,
                                                  method='get')
        assert data.get('max_pages') == 4
        assert len(data.get('groups')) == 10


def test_detail_group(test_app_with_context) -> None:
    with test_app_with_context.test_client() as client:
        yg = create_year_group()
        group = create_group(valid_data, yg)
        data = _test_case_client_without_response(client, f'/api/coordinator/groups/{group.id}/detail/', None, 200,
                                                  method='get')
        assert_model_changes(group, data)


def test_detail_group_if_group_doesnt_exist(test_app_with_context) -> None:
    with test_app_with_context.test_client() as client:
        _test_case_client(client, '/api/coordinator/groups/11/detail/', None, 'Not found group!', 404, method='get',
                          key='error')


def test_delete_group(test_app_with_context) -> None:
    with test_app_with_context.test_client() as client:
        yg = create_year_group()
        group = create_group(valid_data, yg)
        _test_case_client(client, f'/api/coordinator/groups/{group.id}/', None, 'Group was deleted!', 202,
                          method='delete')


def test_delete_group_if_group_doesnt_exist(test_app_with_context) -> None:
    with test_app_with_context.test_client() as client:
        _test_case_client(client, '/api/coordinator/groups/32/', None, 'Not found group!', 404, method='delete',
                          key='error')


def test_edit_group(test_app_with_context) -> None:
    data = copy.copy(new_data)
    with test_app_with_context.test_client() as client:
        yg = create_year_group()
        students = create_students(yg, 3)
        ps = create_project_supervisors(yg, 1)[0]
        group = create_group(valid_data, yg)
        data['students'] = [student.index for student in students]
        data['project_supervisor_id'] = ps.id

        _test_case_client(client, f'/api/coordinator/groups/{group.id}/', data, 'Group was updated!', 200,
                          method='put')
        _test_case_group(group, data)


def test_edit_group_with_invalid_project_supervisor_id(test_app_with_context) -> None:
    data = copy.copy(new_data)
    with test_app_with_context.test_client() as client:
        yg = create_year_group()
        students = create_students(yg, 3)
        group = create_group(valid_data, yg)
        data['students'] = [student.index for student in students]
        data['project_supervisor_id'] = 10
        _test_case_client(client, f'/api/coordinator/groups/{group.id}/', data, 'Not found project supervisor!', 404,
                          method='put', key='error')


def test_edit_group_with_invalid_data(test_app_with_context) -> None:
    with test_app_with_context.test_client() as client:
        yg = create_year_group()
        group = create_group(valid_data, yg)
        data = {'students': [123_4356, 243_533, 434_343]}
        _test_case_client(client, f'/api/coordinator/groups/{group.id}/', data, 'Validation error', 400, method='put')


def test_edit_group_with_invalid_student_indexes(test_app_with_context) -> None:
    data = copy.deepcopy(new_data)
    with test_app_with_context.test_client() as client:
        yg = create_year_group()
        group = create_group(valid_data, yg)
        data['students'] = [123_456, 243_533, 434_343]
        _test_case_client(client, f'/api/coordinator/groups/{group.id}/', data, 'Not found students!', 404,
                          method='put', key='error')


def test_edit_group_if_group_doesnt_exist(test_app_with_context) -> None:
    with test_app_with_context.test_client() as client:
        _test_case_client(client, '/api/coordinator/groups/333/', new_data, 'Not found group!', 404,
                          method='put', key='error')


def test_edit_group_if_you_pass_empty_data(test_app_with_context) -> None:
    with test_app_with_context.test_client() as client:
        _test_case_client(client, '/api/coordinator/groups/333/', {}, 'You have passed empty data!', 400,
                          method='put', key='error')


def test_create_group(test_app_with_context) -> None:
    data = copy.deepcopy(new_data)
    with test_app_with_context.test_client() as client:
        yg = create_year_group()
        students = create_students(yg, 3)
        ps = create_project_supervisors(yg, 1)[0]
        data['students'] = [student.index for student in students]
        data['project_supervisor_id'] = ps.id

        _test_case_client(client, f'/api/coordinator/groups/{yg.id}/', data, 'Group was created!', 201,
                          method='post')
        assert Group.query.count() == 1
        _test_case_group(Group.query.first(), data)


def test_create_group_if_year_group_doesnt_exist(test_app_with_context) -> None:
    with test_app_with_context.test_client() as client:
        _test_case_client(client, '/api/coordinator/groups/22/', invalid_data, 'Not found year group!', 404,
                          method='post', key='error')


def test_create_group_if_project_supervisor_doesnt_exist(test_app_with_context) -> None:
    with test_app_with_context.test_client() as client:
        yg = create_year_group()
        _test_case_client(client, f'/api/coordinator/groups/{yg.id}/', invalid_data, 'Not found project supervisor!',
                          404, method='post', key='error')


def test_create_group_if_you_exceed_the_group_limit(test_app_with_context) -> None:
    data = copy.deepcopy(invalid_data)
    with test_app_with_context.test_client() as client:
        yg = create_year_group()
        ps = create_project_supervisors(yg, 1)[0]
        data['project_supervisor_id'] = ps.id
        limit_group = current_app.config.get('LIMIT_STUDENTS_PER_GROUP')

        data['students'].extend([999_000 + i for i in range(limit_group + 4)])

        _test_case_client(client, f'/api/coordinator/groups/{yg.id}/', data,
                          f"Too much students you want add to group, The group can have only {limit_group}"
                          " students", 400, method='post', key='error')


def test_create_group_if_students_doesnt_exist(test_app_with_context) -> None:
    data = copy.deepcopy(invalid_data)
    with test_app_with_context.test_client() as client:
        yg = create_year_group()
        ps = create_project_supervisors(yg, 1)[0]
        data['project_supervisor_id'] = ps.id
        _test_case_client(client, f'/api/coordinator/groups/{yg.id}/', data, "Not found students!", 404, method='post',
                          key='error')


def test_create_group_if_at_least_one_student_belong_to_other_group(test_app_with_context) -> None:
    data = copy.deepcopy(invalid_data)
    with test_app_with_context.test_client() as client:
        yg = create_year_group()
        ps = create_project_supervisors(yg, 1)[0]
        group = create_group(valid_data, yg)
        data['project_supervisor_id'] = ps.id
        student = create_students(yg, 1)[0]
        group.students.append(student)
        db.session.commit()

        data['students'].extend([student.index])
        _test_case_client(client, f'/api/coordinator/groups/{yg.id}/', data,
                          "One or more students have already belonged to group!", 400, method='post', key='error')


def test_create_group_if_limit_of_group_was_exceed_for_project_supervisor(test_app_with_context) -> None:
    data = copy.deepcopy(invalid_data)
    with test_app_with_context.test_client() as client:
        yg = create_year_group()
        ps = create_project_supervisors(yg, 1)[0]
        data['project_supervisor_id'] = ps.id

        ygps = YearGroupProjectSupervisors.query.filter_by(project_supervisor_id=ps.id, year_group_id=yg.id). \
            first()
        limit_group = ygps.limit_group

        groups = create_groups(yg, limit_group)
        for group in groups:
            group.project_supervisor_id = ps.id
        db.session.commit()

        _test_case_client(client, f'/api/coordinator/groups/{yg.id}/', data,
                          "Can't create new group, project supervisor achieved a limit of groups",
                          400, method='post', key='error')