import sys
import pytest
import requests

from tests.test_resources.helper import get_search, get_search_expect_error, get_stats
from tests.test_resources.ad_fields_data_type import check_historical_stats
from tests.test_resources.historical import compare_keys, check_default_values, all_keys, all_stats, default_values
from tests.test_resources.settings import NUMBER_OF_ADS
from common.settings import TAXONOMY_TYPE_LIST


pytestmark = pytest.mark.historical


def test_all_stats_key_names(historical_stats):
    compare_keys(historical_stats)


def test_all_stats_format(historical_stats):
    """
    check types in the return value
    """
    for key, stats in historical_stats.items():
        check_historical_stats(stats)


def test_all_stats_sorting_order_in_stat_type(historical_stats):
    """
    Checks that items for each stats type are sorted descending
    """
    check_sort_order(historical_stats)


def check_sort_order(stats):
    for key, stats in stats.items():
        highest_value = sys.maxsize
        for item in stats:
            assert item['occurrences'] <= highest_value
            highest_value = item['occurrences']


@pytest.mark.parametrize('limit', [0, 1, 2, 4, 6, 9, 10, 11, 99, 1025, '1'])
def test_all_stats_limit(session, limit):
    stats = get_stats(session, {'limit': limit})
    compare_keys(stats)


def test_all_stats_default_limit(historical_stats):
    """
    Check that there are 10 items, except for 'employment-type' which only has 6 possible values
    """
    check_default_values(historical_stats)


def test_all_stats_zero_limit(session):
    """
    Check that all fields are present and their lists are empty when using limit: 0
    """
    stats = get_stats(session, {'limit': 0})
    compare_keys(stats)


def test_all_stats_none_limit(session):
    """
    Check that all fields are present and their lists have default values when using limit: None
    """
    stats = get_stats(session, {'limit': None})
    compare_keys(stats)
    check_default_values(stats)


@pytest.mark.smoke
@pytest.mark.parametrize("taxonomy_type", TAXONOMY_TYPE_LIST)
def test_taxonomy_type_single(session, taxonomy_type):
    params = {'taxonomy-type': taxonomy_type}
    result = get_stats(session, params)
    assert len(result) == 1
    if taxonomy_type == 'employment-type':
        assert len(result[taxonomy_type]) == 6
    else:
        assert len(result[taxonomy_type]) == default_values
    check_sort_order(result)


def test_taxonomy_type_all(session):
    taxonomy_types = ','.join(TAXONOMY_TYPE_LIST)  # comma-separated string
    params = {'taxonomy-type': taxonomy_types}
    result = get_stats(session, params)
    assert len(result) == 9
    for key, value in result.items():
        if key == 'employment-type':
            assert len(value) == 6
        else:
            assert len(value) == default_values
        check_sort_order(result)


#    SEARCH

@pytest.mark.smoke
@pytest.mark.parametrize("stats_type", ['occupation-name', 'occupation-group', 'municipality', 'region', 'country'])
def test_stats(session, stats_type):
    """
    add checks
    """
    result = get_search(session, {'stats': stats_type})
    assert result['total']['value'] == NUMBER_OF_ADS
    assert len(result) == 7
    assert len(result['stats'][0]['values']) == 5


@pytest.mark.parametrize('stats_type', ['employment-type', 'language', 'skill'])
def test_bad_request(session, stats_type):
    get_search_expect_error(session, {'stats': stats_type}, expected_http_code=requests.codes.bad_request)


def test_multiple_stats(session):
    """
    Do call to stats with increasing number of parameters
    """
    for ix, stats_type in enumerate(all_stats):
        current_len = ix + 1
        stats_list = all_stats[0:current_len]
        print(stats_list)

        params = {'stats': stats_list}
        result = get_search(session, params)
        result_stats = result['stats']
        assert len(result_stats) == current_len
        for item in result_stats:
            assert len(item['values']) == 5
