from datetime import datetime, timedelta

from django.contrib import auth
from django.urls import reverse

from .utils import create_grouped_order, order_items_in_grouped_order


def test_stats(client_log):
    response = client_log.get(reverse("stats"))
    assert response.status_code == 200
    assert response.json() == {
        "total_grouped_orders": 0,
        "total_orders": 0,
        "total_users": 1,
        "last_month_grouped_orders": 0,
    }

    # Create a new grouped-order, place an order and check that the stats changed.
    grouped_order = create_grouped_order(
        days_before_delivery_date=5,
        days_before_deadline=2,
        name="gr order test",
        orga_user=auth.get_user(client_log),
    )
    order_items_in_grouped_order(grouped_order)

    grouped_order.deadline = datetime.now() - timedelta(days=10)
    grouped_order.save()

    response = client_log.get(reverse("stats"))
    assert response.status_code == 200
    assert response.json() == {
        "total_grouped_orders": 1,
        "total_orders": 3,
        "total_users": 1,
        "last_month_grouped_orders": 1,
    }
