import pytest

from sokannonser.settings import OCCUPATION, OCCUPATION_GROUP, OCCUPATION_FIELD, MUNICIPALITY
from tests.test_resources.concept_ids import occupation, occupation_group, occupation_field, concept_ids_geo as geo
from tests.test_resources.helper import get_total
from tests.test_resources.settings import NUMBER_OF_ADS
from sokannonser.settings import REMOTE, UNSPECIFIED_SWEDEN_WORKPLACE, ABROAD, PUBLISHED_BEFORE, PUBLISHED_AFTER, \
    EXPERIENCE_REQUIRED

TEST_DATE = "2020-12-10T23:59:59"
NUMBER_OF_REMOTE_ADS = 11


@pytest.mark.parametrize("params, expected", [
    ({REMOTE: True, 'q': 'utvecklare'}, 2),
    ({REMOTE: False, 'q': 'utvecklare'}, 47),
    ({REMOTE: None, 'q': 'utvecklare'}, 49),
    ({REMOTE: True, 'q': 'säljare'}, 1),
    ({REMOTE: False, 'q': 'säljare'}, 59),
    ({REMOTE: None, 'q': 'säljare'}, 60),
    ({REMOTE: True, OCCUPATION: occupation.saljkonsulent}, 1),
    ({REMOTE: None, OCCUPATION: occupation.saljkonsulent}, 2),
    ({REMOTE: False, OCCUPATION: occupation.saljkonsulent}, 1),
    ({OCCUPATION: occupation.saljkonsulent}, 2),
    ({OCCUPATION: occupation.mjukvaruutvecklare}, 20),
    ({REMOTE: True, OCCUPATION: occupation.mjukvaruutvecklare}, 0),
    ({REMOTE: False, OCCUPATION: occupation.mjukvaruutvecklare}, 20),
])
def test_remote_occupation(session, params, expected):
    """
    AND condition between REMOTE and other params
    """
    assert get_total(session, params) == expected


@pytest.mark.parametrize("params, expected", [
    ({REMOTE: True, OCCUPATION_GROUP: occupation_group.mjukvaru__och_systemutvecklare_m_fl_}, 3),
    ({REMOTE: None, OCCUPATION_GROUP: occupation_group.mjukvaru__och_systemutvecklare_m_fl_}, 88),
    ({REMOTE: False, OCCUPATION_GROUP: occupation_group.mjukvaru__och_systemutvecklare_m_fl_}, 85),
    ({REMOTE: True, OCCUPATION_GROUP: occupation_group.telefonforsaljare_m_fl_}, 0),
    ({REMOTE: None, OCCUPATION_GROUP: occupation_group.telefonforsaljare_m_fl_}, 24),
    ({REMOTE: True, OCCUPATION_GROUP: occupation_group.foretagssaljare}, 2),
    ({REMOTE: None, OCCUPATION_GROUP: occupation_group.foretagssaljare}, 66),
])
def test_remote_occupation_group(session, params, expected):
    """
    AND condition between REMOTE and other params
    """
    assert get_total(session, params) == expected


@pytest.mark.parametrize("params, expected", [
    ({REMOTE: True, OCCUPATION_FIELD: occupation_field.data_it}, 3),
    ({REMOTE: None, OCCUPATION_FIELD: occupation_field.data_it}, 156),
    ({REMOTE: True, OCCUPATION_FIELD: occupation_field.forsaljning__inkop__marknadsforing}, 3),
    ({REMOTE: None, OCCUPATION_FIELD: occupation_field.forsaljning__inkop__marknadsforing}, 188),
    ({REMOTE: False, OCCUPATION_FIELD: occupation_field.forsaljning__inkop__marknadsforing}, 185),
])
def test_remote_occupation_field(session, params, expected):
    """
    AND condition between REMOTE and other params
    """
    assert get_total(session, params) == expected


@pytest.mark.parametrize("params, expected", [
    ({'q': 'Stockholm'}, 299),
    ({REMOTE: True, 'q': 'Stockholm'}, 4),
    ({REMOTE: False, 'q': 'Stockholm'}, 295),
    ({MUNICIPALITY: geo.stockholm}, 285),
    ({REMOTE: True, MUNICIPALITY: geo.stockholm}, 3),
    ({REMOTE: False, MUNICIPALITY: geo.stockholm}, 282),
])
def test_remote_municipality(session, params, expected):
    """
    AND condition between REMOTE and other params
    """
    assert get_total(session, params) == expected


@pytest.mark.parametrize("params, expected", [
    ({'q': 'Stockholms Län'}, 410),
    ({REMOTE: True, 'q': 'Stockholms Län'}, 3),
    ({REMOTE: False, 'q': 'Stockholms Län'}, 407),
    ({'region': geo.stockholms_lan}, 410),
    ({REMOTE: True, 'region': geo.stockholms_lan}, 3),
    ({REMOTE: False, 'region': geo.stockholms_lan}, 407),
    ({REMOTE: None, 'region': geo.vastra_gotalands_lan}, 220),
    ({REMOTE: True, 'region': geo.vastra_gotalands_lan}, 1),
    ({REMOTE: False, 'region': geo.vastra_gotalands_lan}, 219),
    ({'region': geo.vastra_gotalands_lan, 'q': 'säljare'}, 9),
    ({REMOTE: False, 'region': geo.vastra_gotalands_lan, 'q': 'säljare'}, 9),
    ({REMOTE: True, 'region': geo.vastra_gotalands_lan, 'q': 'säljare'}, 0),
])
def test_remote_region(session, params, expected):
    """
    AND condition between REMOTE and other params
    """
    assert get_total(session, params) == expected


@pytest.mark.parametrize("params, expected", [
    ({PUBLISHED_AFTER: TEST_DATE}, 674),
    ({REMOTE: True, PUBLISHED_AFTER: TEST_DATE}, 6),
    ({PUBLISHED_BEFORE: TEST_DATE}, 821),
    ({REMOTE: True, PUBLISHED_BEFORE: TEST_DATE}, 5)
])
def test_remote_publish_date(session, params, expected):
    """
    AND condition between REMOTE and other params
    """
    assert get_total(session, params) == expected


@pytest.mark.parametrize("params, expected", [
    ({ABROAD: True}, 10),
    ({ABROAD: False}, NUMBER_OF_ADS),
    ({REMOTE: True, ABROAD: True}, 0),
    ({REMOTE: True, ABROAD: False}, NUMBER_OF_REMOTE_ADS),  # abroad False does nothing
])
def test_abroad(session, params, expected):
    """
    AND condition between REMOTE and other params
    """
    assert get_total(session, params) == expected


@pytest.mark.parametrize("params, expected", [
    ({UNSPECIFIED_SWEDEN_WORKPLACE: True}, 46),
    ({UNSPECIFIED_SWEDEN_WORKPLACE: False}, NUMBER_OF_ADS),
    ({REMOTE: True, UNSPECIFIED_SWEDEN_WORKPLACE: True}, 3),
    ({REMOTE: True, UNSPECIFIED_SWEDEN_WORKPLACE: False}, NUMBER_OF_REMOTE_ADS)
])
def test_unspecified_workplace(session, params, expected):
    """
    AND condition between REMOTE and other params
    """
    assert get_total(session, params) == expected


@pytest.mark.parametrize('params, expected', [
    ({EXPERIENCE_REQUIRED: True}, 1193),
    ({EXPERIENCE_REQUIRED: False}, 302),
    ({REMOTE: True, EXPERIENCE_REQUIRED: True}, 10),
    ({REMOTE: False, EXPERIENCE_REQUIRED: True}, 1183),
    ({REMOTE: True, EXPERIENCE_REQUIRED: False}, 1),
    ({REMOTE: False, EXPERIENCE_REQUIRED: False}, 301)
])
def test_experience(session, params, expected):
    """
    AND condition between REMOTE and other params
    """
    assert get_total(session, params) == expected


@pytest.mark.parametrize("params, expected", [
    ({REMOTE: True, 'q': '-Stockholm'}, 7),
    ({REMOTE: True, MUNICIPALITY: f"-{geo.stockholm}"}, 8),
    ({REMOTE: True, 'region': f"-{geo.stockholms_lan}"}, 8),
    ({REMOTE: True, 'region': f"-{geo.vastra_gotalands_lan}"}, 10),
    ({REMOTE: True, 'region': f"-{geo.skane_lan}"}, 10),
    ({REMOTE: True, 'region': f"-{geo.norrbottens_lan}"}, 10),
])
def test_remote_negative_geography(session, params, expected):
    """
    Negative geographical parameters
    AND condition between REMOTE and other params
    """
    assert get_total(session, params) == expected


def test_combination_municipality(session):
    """
    numbers for REMOTE True + REMOTE False should add upp to numbers when not using REMOTE
    AND condition between REMOTE and other params
    """
    number_region = get_total(session, {'municipality': geo.stockholm})
    number_remote = get_total(session, {REMOTE: True, 'municipality': geo.stockholm})
    number_not_remote = get_total(session, {REMOTE: False, 'municipality': geo.stockholm})
    assert number_remote + number_not_remote == number_region


def test_combination_region(session):
    """
    numbers for REMOTE True + REMOTE False should add upp to numbers when not using REMOTE
    AND condition between REMOTE and other params
    """
    number_region = get_total(session, {'region': geo.stockholms_lan})
    number_remote = get_total(session, {REMOTE: True, 'region': geo.stockholms_lan})
    number_not_remote = get_total(session, {REMOTE: False, 'region': geo.stockholms_lan})
    assert number_remote + number_not_remote == number_region


@pytest.mark.parametrize("param, expected", [
    ({REMOTE: False, REMOTE: True}, NUMBER_OF_REMOTE_ADS),
    ({REMOTE: True, REMOTE: False}, (NUMBER_OF_ADS - NUMBER_OF_REMOTE_ADS)),
    ({REMOTE: True, REMOTE: False, REMOTE: True}, NUMBER_OF_REMOTE_ADS)
])
def test_duplicate_remote_param(session, param, expected):
    """
    with duplicated params, value of last param is used
    """
    assert get_total(session, params=param) == expected

import pytest

from sokannonser.settings import OCCUPATION, OCCUPATION_GROUP, OCCUPATION_FIELD, MUNICIPALITY
from tests.test_resources.concept_ids import occupation, occupation_group, occupation_field, concept_ids_geo as geo
from tests.test_resources.helper import get_total
from tests.test_resources.settings import NUMBER_OF_ADS
from sokannonser.settings import REMOTE, UNSPECIFIED_SWEDEN_WORKPLACE, ABROAD, PUBLISHED_BEFORE, PUBLISHED_AFTER, \
    EXPERIENCE_REQUIRED

TEST_DATE = "2020-12-10T23:59:59"
NUMBER_OF_REMOTE_ADS = 11


@pytest.mark.parametrize("params, expected", [
    ({REMOTE: True, 'q': 'utvecklare'}, 2),
    ({REMOTE: False, 'q': 'utvecklare'}, 47),
    ({REMOTE: None, 'q': 'utvecklare'}, 49),
    ({REMOTE: True, 'q': 'säljare'}, 1),
    ({REMOTE: False, 'q': 'säljare'}, 59),
    ({REMOTE: None, 'q': 'säljare'}, 60),
    ({REMOTE: True, OCCUPATION: occupation.saljkonsulent}, 1),
    ({REMOTE: None, OCCUPATION: occupation.saljkonsulent}, 2),
    ({REMOTE: False, OCCUPATION: occupation.saljkonsulent}, 1),
    ({OCCUPATION: occupation.saljkonsulent}, 2),
    ({OCCUPATION: occupation.mjukvaruutvecklare}, 20),
    ({REMOTE: True, OCCUPATION: occupation.mjukvaruutvecklare}, 0),
    ({REMOTE: False, OCCUPATION: occupation.mjukvaruutvecklare}, 20),
])
def test_remote_occupation(session, params, expected):
    """
    AND condition between REMOTE and other params
    """
    assert get_total(session, params) == expected


@pytest.mark.parametrize("params, expected", [
    ({REMOTE: True, OCCUPATION_GROUP: occupation_group.mjukvaru__och_systemutvecklare_m_fl_}, 3),
    ({REMOTE: None, OCCUPATION_GROUP: occupation_group.mjukvaru__och_systemutvecklare_m_fl_}, 88),
    ({REMOTE: False, OCCUPATION_GROUP: occupation_group.mjukvaru__och_systemutvecklare_m_fl_}, 85),
    ({REMOTE: True, OCCUPATION_GROUP: occupation_group.telefonforsaljare_m_fl_}, 0),
    ({REMOTE: None, OCCUPATION_GROUP: occupation_group.telefonforsaljare_m_fl_}, 24),
    ({REMOTE: True, OCCUPATION_GROUP: occupation_group.foretagssaljare}, 2),
    ({REMOTE: None, OCCUPATION_GROUP: occupation_group.foretagssaljare}, 66),
])
def test_remote_occupation_group(session, params, expected):
    """
    AND condition between REMOTE and other params
    """
    assert get_total(session, params) == expected


@pytest.mark.parametrize("params, expected", [
    ({REMOTE: True, OCCUPATION_FIELD: occupation_field.data_it}, 3),
    ({REMOTE: None, OCCUPATION_FIELD: occupation_field.data_it}, 156),
    ({REMOTE: True, OCCUPATION_FIELD: occupation_field.forsaljning__inkop__marknadsforing}, 3),
    ({REMOTE: None, OCCUPATION_FIELD: occupation_field.forsaljning__inkop__marknadsforing}, 188),
    ({REMOTE: False, OCCUPATION_FIELD: occupation_field.forsaljning__inkop__marknadsforing}, 185),
])
def test_remote_occupation_field(session, params, expected):
    """
    AND condition between REMOTE and other params
    """
    assert get_total(session, params) == expected


@pytest.mark.parametrize("params, expected", [
    ({'q': 'Stockholm'}, 299),
    ({REMOTE: True, 'q': 'Stockholm'}, 4),
    ({REMOTE: False, 'q': 'Stockholm'}, 295),
    ({MUNICIPALITY: geo.stockholm}, 285),
    ({REMOTE: True, MUNICIPALITY: geo.stockholm}, 3),
    ({REMOTE: False, MUNICIPALITY: geo.stockholm}, 282),
])
def test_remote_municipality(session, params, expected):
    """
    AND condition between REMOTE and other params
    """
    assert get_total(session, params) == expected


@pytest.mark.parametrize("params, expected", [
    ({'q': 'Stockholms Län'}, 410),
    ({REMOTE: True, 'q': 'Stockholms Län'}, 3),
    ({REMOTE: False, 'q': 'Stockholms Län'}, 407),
    ({'region': geo.stockholms_lan}, 410),
    ({REMOTE: True, 'region': geo.stockholms_lan}, 3),
    ({REMOTE: False, 'region': geo.stockholms_lan}, 407),
    ({REMOTE: None, 'region': geo.vastra_gotalands_lan}, 220),
    ({REMOTE: True, 'region': geo.vastra_gotalands_lan}, 1),
    ({REMOTE: False, 'region': geo.vastra_gotalands_lan}, 219),
    ({'region': geo.vastra_gotalands_lan, 'q': 'säljare'}, 9),
    ({REMOTE: False, 'region': geo.vastra_gotalands_lan, 'q': 'säljare'}, 9),
    ({REMOTE: True, 'region': geo.vastra_gotalands_lan, 'q': 'säljare'}, 0),
])
def test_remote_region(session, params, expected):
    """
    AND condition between REMOTE and other params
    """
    assert get_total(session, params) == expected


@pytest.mark.parametrize("params, expected", [
    ({PUBLISHED_AFTER: TEST_DATE}, 674),
    ({REMOTE: True, PUBLISHED_AFTER: TEST_DATE}, 6),
    ({PUBLISHED_BEFORE: TEST_DATE}, 821),
    ({REMOTE: True, PUBLISHED_BEFORE: TEST_DATE}, 5)
])
def test_remote_publish_date(session, params, expected):
    """
    AND condition between REMOTE and other params
    """
    assert get_total(session, params) == expected


@pytest.mark.parametrize("params, expected", [
    ({ABROAD: True}, 10),
    ({ABROAD: False}, NUMBER_OF_ADS),
    ({REMOTE: True, ABROAD: True}, 0),
    ({REMOTE: True, ABROAD: False}, NUMBER_OF_REMOTE_ADS),  # abroad False does nothing
])
def test_abroad(session, params, expected):
    """
    AND condition between REMOTE and other params
    """
    assert get_total(session, params) == expected


@pytest.mark.parametrize("params, expected", [
    ({UNSPECIFIED_SWEDEN_WORKPLACE: True}, 46),
    ({UNSPECIFIED_SWEDEN_WORKPLACE: False}, NUMBER_OF_ADS),
    ({REMOTE: True, UNSPECIFIED_SWEDEN_WORKPLACE: True}, 3),
    ({REMOTE: True, UNSPECIFIED_SWEDEN_WORKPLACE: False}, NUMBER_OF_REMOTE_ADS)
])
def test_unspecified_workplace(session, params, expected):
    """
    AND condition between REMOTE and other params
    """
    assert get_total(session, params) == expected


@pytest.mark.parametrize('params, expected', [
    ({EXPERIENCE_REQUIRED: True}, 1193),
    ({EXPERIENCE_REQUIRED: False}, 302),
    ({REMOTE: True, EXPERIENCE_REQUIRED: True}, 10),
    ({REMOTE: False, EXPERIENCE_REQUIRED: True}, 1183),
    ({REMOTE: True, EXPERIENCE_REQUIRED: False}, 1),
    ({REMOTE: False, EXPERIENCE_REQUIRED: False}, 301)
])
def test_experience(session, params, expected):
    """
    AND condition between REMOTE and other params
    """
    assert get_total(session, params) == expected


@pytest.mark.parametrize("params, expected", [
    ({REMOTE: True, 'q': '-Stockholm'}, 7),
    ({REMOTE: True, MUNICIPALITY: f"-{geo.stockholm}"}, 8),
    ({REMOTE: True, 'region': f"-{geo.stockholms_lan}"}, 8),
    ({REMOTE: True, 'region': f"-{geo.vastra_gotalands_lan}"}, 10),
    ({REMOTE: True, 'region': f"-{geo.skane_lan}"}, 10),
    ({REMOTE: True, 'region': f"-{geo.norrbottens_lan}"}, 10),
])
def test_remote_negative_geography(session, params, expected):
    """
    Negative geographical parameters
    AND condition between REMOTE and other params
    """
    assert get_total(session, params) == expected


def test_combination_municipality(session):
    """
    numbers for REMOTE True + REMOTE False should add upp to numbers when not using REMOTE
    AND condition between REMOTE and other params
    """
    number_region = get_total(session, {'municipality': geo.stockholm})
    number_remote = get_total(session, {REMOTE: True, 'municipality': geo.stockholm})
    number_not_remote = get_total(session, {REMOTE: False, 'municipality': geo.stockholm})
    assert number_remote + number_not_remote == number_region


def test_combination_region(session):
    """
    numbers for REMOTE True + REMOTE False should add upp to numbers when not using REMOTE
    AND condition between REMOTE and other params
    """
    number_region = get_total(session, {'region': geo.stockholms_lan})
    number_remote = get_total(session, {REMOTE: True, 'region': geo.stockholms_lan})
    number_not_remote = get_total(session, {REMOTE: False, 'region': geo.stockholms_lan})
    assert number_remote + number_not_remote == number_region


@pytest.mark.parametrize("param, expected", [
    ({REMOTE: False, REMOTE: True}, NUMBER_OF_REMOTE_ADS),
    ({REMOTE: True, REMOTE: False}, (NUMBER_OF_ADS - NUMBER_OF_REMOTE_ADS)),
    ({REMOTE: True, REMOTE: False, REMOTE: True}, NUMBER_OF_REMOTE_ADS)
])
def test_duplicate_remote_param(session, param, expected):
    """
    with duplicated params, value of last param is used
    """
    assert get_total(session, params=param) == expected

AND condition between REMOTE and other params AND condition between REMOTE and other params AND condition between REMOTE and other params AND condition between REMOTE and other params AND condition between REMOTE and other params AND condition between REMOTE and other params # abroad False does nothing AND condition between REMOTE and other params AND condition between REMOTE and other params AND condition between REMOTE and other params Negative geographical parameters     AND condition between REMOTE and other params numbers for REMOTE True + REMOTE False should add upp to numbers when not using REMOTE     AND condition between REMOTE and other params numbers for REMOTE True + REMOTE False should add upp to numbers when not using REMOTE     AND condition between REMOTE and other params with duplicated params, value of last param is used