import datetime

import pytest
from django.utils import timezone

from la_chariotte.order.models import GroupedOrder, Item
from la_chariotte.tests.utils import create_grouped_order


@pytest.fixture
def client_log(client, django_user_model):
    username = "test@user.fr"
    password = "azertypassword"
    user = django_user_model.objects.create_user(username=username, password=password)
    client.login(username=username, password=password)
    return client


@pytest.fixture
def other_user(django_user_model):
    username = "other@user.fr"
    password = "azertypassword"
    user = django_user_model.objects.create_user(username=username, password=password)
    return user


@pytest.fixture
def authenticated_user_with_name(django_user_model):
    username = "toto@gmail.com"
    password = "tata"
    first_name = "boule"
    last_name = "bill"
    user = django_user_model.objects.create_user(
        username=username, password=password, first_name=first_name, last_name=last_name
    )
    return user


@pytest.fixture
def simple_grouped_order(other_user):
    date = timezone.now().date() + datetime.timedelta(days=30)
    deadline = timezone.now() + datetime.timedelta(days=5)
    grouped_order = GroupedOrder.objects.create(
        name="Test grouped order",
        orga=other_user,
        delivery_date=date,
        deadline=deadline,
    )
    grouped_order.create_code_from_pk()
    grouped_order.save()
    item = Item.objects.create(name="test item", grouped_order=grouped_order, price=2)
    return grouped_order


@pytest.fixture
def connected_grouped_order(client, authenticated_user_with_name):
    client.force_login(authenticated_user_with_name)
    grouped_order = create_grouped_order(
        name="Test grouped order",
        orga_user=authenticated_user_with_name,
        days_before_delivery_date=30,
        days_before_deadline=5,
    )
    item = Item.objects.create(name="test item", grouped_order=grouped_order, price=2)
    return grouped_order


@pytest.fixture(autouse=True)
def password_hasher_setup(settings):
    # Use a weaker password hasher during tests, for speed
    settings.PASSWORD_HASHERS = [
        "django.contrib.auth.hashers.MD5PasswordHasher",
    ]
