# retoor <retoor@molodetz.nl>

import requests
from tests.conftest import BASE_URL
from devplacepy.database import (
    clear_settings_cache,
    get_setting,
    get_table,
    refresh_snapshot,
    set_setting,
)
from devplacepy.services.acceptance.agreements import AGREEMENTS, setting_key

JSON = {"Accept": "application/json"}
SERVICE_URL = f"{BASE_URL}/admin/services/acceptance"


def _admin(seeded_db):
    refresh_snapshot()
    key = get_table("users").find_one(username="alice_test")["api_key"]
    session = requests.Session()
    session.headers.update({"X-API-KEY": key, **JSON})
    return session


def test_the_service_is_listed_and_stopped_by_default(seeded_db):
    admin = _admin(seeded_db)
    r = admin.get(f"{BASE_URL}/admin/services/data")
    assert r.status_code == 200, r.text[:300]
    entry = next(
        item for item in r.json()["services"] if item["name"] == "acceptance"
    )
    assert entry["enabled"] is False
    assert entry["status"] == "stopped"
    assert entry["title"] == "Acceptance convergence"


def test_the_detail_page_renders_one_field_per_agreement(seeded_db):
    admin = _admin(seeded_db)
    r = admin.get(f"{SERVICE_URL}/data")
    assert r.status_code == 200, r.text[:300]
    fields = {field["key"]: field for field in r.json()["service"]["fields"]}
    for agreement in AGREEMENTS:
        field = fields[setting_key(agreement.kind)]
        assert field["type"] == "bool"
        assert field["value"] == "0"
        assert field["group"] == "Agreements"
    assert fields["acceptance_dry_run"]["value"] == "1"
    assert fields["service_acceptance_enabled"]["value"] == "0"


def test_the_detail_page_renders_html_for_an_admin(seeded_db):
    admin = _admin(seeded_db)
    r = requests.get(SERVICE_URL, headers={"X-API-KEY": admin.headers["X-API-KEY"]})
    assert r.status_code == 200
    assert "Acceptance convergence" in r.text


def test_an_admin_saves_an_agreement_toggle(seeded_db):
    admin = _admin(seeded_db)
    key = setting_key("container_credentials")
    original = get_setting(key, "0")
    try:
        r = admin.post(f"{SERVICE_URL}/config", data={key: "1"})
        assert r.status_code == 200, r.text[:300]
        assert r.json().get("ok") is True
        refresh_snapshot()
        clear_settings_cache()
        assert get_setting(key, "0") == "1"
    finally:
        set_setting(key, original)


def test_an_invalid_batch_size_is_refused(seeded_db):
    admin = _admin(seeded_db)
    r = admin.post(f"{SERVICE_URL}/config", data={"acceptance_batch_size": "banana"})
    assert r.status_code == 400
    assert r.json()["errors"]["acceptance_batch_size"]


def test_a_guest_cannot_read_the_service(app_server):
    r = requests.get(f"{SERVICE_URL}/data", headers=JSON, allow_redirects=False)
    assert r.status_code in (302, 303, 401, 403)


def test_the_master_switch_stays_off_for_the_suite(seeded_db):
    refresh_snapshot()
    clear_settings_cache()
    assert get_setting("service_acceptance_enabled", "0") != "1"
