from rights_filter.integrations.search_policy import SearchApiPolicy


def test_search_policy_blocks_disabled_or_unapproved_provider():
    disabled = SearchApiPolicy(disabled=True, compliance_approved=True)
    unapproved = SearchApiPolicy(disabled=False, compliance_approved=False)

    assert disabled.can_call("naver") == (False, "search API disabled")
    assert unapproved.can_call("naver") == (
        False,
        "search API compliance not approved",
    )


def test_search_policy_enforces_allowed_providers_and_daily_limit():
    policy = SearchApiPolicy(
        compliance_approved=True,
        allowed_providers={"naver"},
        daily_limit=1,
    )

    assert policy.can_call("naver") == (True, None)
    policy.record_call()

    assert policy.can_call("google") == (False, "search provider not allowed")
    assert policy.can_call("naver") == (False, "search API usage limit reached")


def test_search_policy_counts_requested_page_calls_against_daily_limit():
    policy = SearchApiPolicy(
        compliance_approved=True,
        allowed_providers={"naver"},
        daily_limit=3,
    )

    assert policy.can_call("naver", requested_calls=2) == (True, None)
    policy.record_call(2)

    assert policy.calls_made == 2
    assert policy.can_call("naver", requested_calls=2) == (
        False,
        "search API usage limit reached",
    )
    assert policy.can_call("naver", requested_calls=1) == (True, None)
