# -*- coding: utf-8 -*-
import datetime
import pytest


from django.conf import settings

from creditor.tests.fixtures.tags import generate_standard_set
from creditor.models import TransactionTag
from creditor.tests.fixtures.transactions import TransactionFactory
from members.tests.fixtures.memberlikes import MemberFactory

from velkoja.nordeachecker import NordeaOverdueInvoicesHandler


@pytest.mark.django_db
@pytest.fixture
def basic_setup():
    generate_standard_set()
    member = MemberFactory()
    return member


@pytest.mark.django_db
@pytest.fixture
def uniform_transactions_zerosum(basic_setup):
    member = basic_setup
    cutoff = datetime.datetime.now().date() - datetime.timedelta(days=settings.VELKOJA_NORDEACHECKER_GRACE_DAYS+2)
    tag = TransactionTag.objects.get(tmatch='2')
    for refno, amount in [ ('109', 40), ('107', 20) ]:
        for x in range(6):
            credit_date = cutoff - datetime.timedelta(days=x*30)
            debit_date = credit_date - datetime.timedelta(days=3)
            debit = TransactionFactory(
                owner = member,
                reference = refno,
                amount = -amount,
                tag = tag,
                stamp = datetime.datetime.combine(debit_date, datetime.datetime.min.time())
            )
            credit = TransactionFactory(
                owner = member,
                reference = refno,
                amount = amount,
                tag = tag,
                stamp = datetime.datetime.combine(credit_date, datetime.datetime.min.time())
            )
    return (member, cutoff)


@pytest.mark.django_db
def test_no_overdue(uniform_transactions_zerosum):
    member, cutoff = uniform_transactions_zerosum
    assert member.creditor_transactions.count() > 0
    handler = NordeaOverdueInvoicesHandler()
    overdue = handler.list_overdue()
    assert not overdue


@pytest.mark.django_db
def test_uniform_overdue(uniform_transactions_zerosum):
    member, cutoff = uniform_transactions_zerosum
    old_count = member.creditor_transactions.count()
    assert old_count > 0
    templ = member.creditor_transactions.filter(amount__lt=0).order_by('-stamp')[0]
    debit = TransactionFactory(
        owner=member,
        reference=templ.reference,
        amount=templ.amount,
        tag=templ.tag,
        stamp=templ.stamp + datetime.timedelta(days=2)
    )
    print(debit, debit.unique_id)
    assert member.creditor_transactions.count() > old_count
    handler = NordeaOverdueInvoicesHandler()
    overdue = handler.list_overdue()
    assert overdue
    assert overdue[0].unique_id == debit.unique_id

@@ -794,16 +794,31 @@
 ch='2')%0A
+    months = 6%0A
     for 
@@ -887,17 +887,22 @@
 n range(
-6
+months
 ):%0A     
@@ -1629,24 +1629,1406 @@
 ango_db%0A
-def test
+@pytest.fixture%0Adef nonuniform_transactions_zerosum(basic_setup):%0A    member = basic_setup%0A    cutoff = datetime.datetime.now().date() - datetime.timedelta(days=settings.VELKOJA_NORDEACHECKER_GRACE_DAYS+2)%0A    tag = TransactionTag.objects.get(tmatch='2')%0A    months = 6%0A    for refno, amount in %5B ('109', 40), ('107', 20) %5D:%0A        for x in range(months):%0A            debit_date = cutoff - datetime.timedelta(days=x*30)%0A            debit = TransactionFactory(%0A                owner = member,%0A                reference = refno,%0A                amount = -amount,%0A                tag = tag,%0A                stamp = datetime.datetime.combine(debit_date, datetime.datetime.min.time())%0A            )%0A        credit_date = cutoff - datetime.timedelta(days=months/3*30)%0A        first_amount = amount*(months/3)%0A        credit = TransactionFactory(%0A            owner = member,%0A            reference = refno,%0A            amount = first_amount,%0A            tag = tag,%0A            stamp = datetime.datetime.combine(credit_date, datetime.datetime.min.time())%0A        )%0A        credit2 = TransactionFactory(%0A            owner = member,%0A            reference = refno,%0A            amount = months*amount-first_amount,%0A            tag = tag,%0A            stamp = datetime.datetime.combine(cutoff, datetime.datetime.min.time())%0A        )%0A%0A    return (member, cutoff)%0A%0A%0A@pytest.mark.django_db%0Adef test_uniform
 _no_over
@@ -3959,16 +3959,16 @@
 overdue%0A
-
     asse
@@ -4010,8 +4010,563 @@
 ique_id%0A
+%0A%0A@pytest.mark.django_db%0Adef test_nonuniform_no_overdue(nonuniform_transactions_zerosum):%0A    member, cutoff = nonuniform_transactions_zerosum%0A    assert member.creditor_transactions.count() %3E 0%0A    # Make sure the distribution is non-uniform%0A    debits = member.creditor_transactions.filter(amount__lt=0)%0A    credits = member.creditor_transactions.filter(amount__gt=0)%0A    assert debits.count() != credits.count()%0A    # Make sure nothing is overdue%0A    handler = NordeaOverdueInvoicesHandler()%0A    overdue = handler.list_overdue()%0A    assert not overdue%0A

Test nonuniform payment distribution