import datetime

import pytest
from django.utils import timezone

from la_chariotte.order import models

pytestmark = pytest.mark.django_db


def create_grouped_order(
    days_before_delivery_date,
    days_before_deadline,
    name,
    orga_user,
    is_phone_mandatory=True,
):
    date = timezone.now().date() + datetime.timedelta(days=days_before_delivery_date)
    deadline = timezone.now() + datetime.timedelta(days=days_before_deadline)
    grouped_order = models.GroupedOrder.objects.create(
        name=name,
        orga=orga_user,
        delivery_date=date,
        deadline=deadline,
        is_phone_mandatory=is_phone_mandatory,
    )
    grouped_order.create_code_from_pk()
    grouped_order.save()
    return grouped_order


def order_items_in_grouped_order(grouped_order):
    """Creates 2 OrderedItems and orders in the given grouped order. Returns the order"""
    item_1 = grouped_order.item_set.create(name="test item", price="2")
    item_2 = grouped_order.item_set.create(name="test item 2", price="9")
    author_1 = models.OrderAuthor.objects.create(
        first_name="bob", last_name="lescargot", phone="000", email="bob@escargot.fr"
    )
    author_2 = models.OrderAuthor.objects.create(
        first_name="bob", last_name="alescargot", phone="000", email="bob2@escargot.fr"
    )
    author_3 = models.OrderAuthor.objects.create(
        first_name="bobby",
        last_name="alescargot",
        phone="000",
        email="bob3@escargot.fr",
    )
    order = grouped_order.order_set.create(author=author_1)
    order_2 = grouped_order.order_set.create(author=author_2)
    order_3 = grouped_order.order_set.create(author=author_3)
    models.OrderedItem.objects.create(order=order, item=item_1, nb=3)
    models.OrderedItem.objects.create(order=order, item=item_2, nb=2)
    models.OrderedItem.objects.create(order=order_2, item=item_1, nb=1)
    models.OrderedItem.objects.create(order=order_3, item=item_2, nb=1)
    return order
