from apps.services.models import Service

URL = "/api/v1/backoffice/services/"


def test_at_most_six_important_services(staff_api, api, content):
    Service.objects.update(is_featured=False)
    services = list(Service.objects.order_by("pk"))
    for service in services[:6]:
        assert (
            staff_api.patch(f"{URL}{service.pk}/", {"is_featured": True}, format="json").status_code
            == 200
        )
    seventh = staff_api.patch(f"{URL}{services[6].pk}/", {"is_featured": True}, format="json")
    assert seventh.status_code == 400
    assert "retirez-en un" in seventh.json()["is_featured"][0]
    # Une fois un service retiré, un autre peut prendre sa place.
    staff_api.patch(f"{URL}{services[0].pk}/", {"is_featured": False}, format="json")
    assert (
        staff_api.patch(f"{URL}{services[6].pk}/", {"is_featured": True}, format="json").status_code
        == 200
    )
    # Modifier un service déjà important reste possible.
    assert (
        staff_api.patch(
            f"{URL}{services[6].pk}/", {"is_featured": True, "title": "X"}, format="json"
        ).status_code
        == 200
    )
    assert len(api.get("/api/v1/home/").json()["services"]) == 6
