# retoor <retoor@molodetz.nl>

from devplacepy.database import CONSENT_KINDS
from devplacepy.services.acceptance.agreements import (
    AGREEMENTS,
    agreement_for,
    label_for,
    setting_key,
)
from devplacepy.services.acceptance.service import AcceptanceService


def test_every_consent_kind_is_classified_as_an_agreement():
    assert {agreement.kind for agreement in AGREEMENTS} == set(CONSENT_KINDS)


def test_setting_keys_are_unique_and_namespaced():
    keys = [setting_key(agreement.kind) for agreement in AGREEMENTS]
    assert len(keys) == len(set(keys))
    for key in keys:
        assert key.startswith("acceptance_grant_")


def test_every_agreement_has_a_config_field_in_the_agreements_group():
    service = AcceptanceService()
    fields = {field.key: field for field in service.config_fields}
    for agreement in AGREEMENTS:
        field = fields[setting_key(agreement.kind)]
        assert field.type == "bool"
        assert field.default is False
        assert field.group == "Agreements"


def test_labels_come_from_the_consent_registry():
    for agreement in AGREEMENTS:
        assert label_for(agreement.kind) == CONSENT_KINDS[agreement.kind]


def test_only_terms_declares_a_gate_column():
    gated = [agreement.kind for agreement in AGREEMENTS if agreement.gate_column]
    assert gated == ["terms"]


def test_versioned_agreements_match_the_application_version_keys():
    from devplacepy.routers.profile.consent import VERSION_KEYS

    versioned = {
        agreement.kind: agreement.version_setting
        for agreement in AGREEMENTS
        if agreement.version_setting
    }
    assert versioned == VERSION_KEYS


def test_agreement_lookup_is_total_over_the_registry():
    for agreement in AGREEMENTS:
        assert agreement_for(agreement.kind) is agreement
    assert agreement_for("not_a_consent") is None


def test_the_service_is_opt_in_and_runs_every_five_minutes():
    service = AcceptanceService()
    assert service.default_enabled is False
    assert service.interval_seconds == 300
    assert service.is_enabled() is False
