from decimal import Decimal

from django.contrib.auth.models import User
from django.test import TestCase

from .models import BusinessProfile, Transaction


class TransactionLogicTests(TestCase):
    def setUp(self):
        self.user = User.objects.create_user(username='agent', password='secret123')
        self.profile = BusinessProfile.objects.create(
            user=self.user,
            business_name='BrightPay',
            opening_ecash=Decimal('1000.00'),
            opening_physical_cash=Decimal('500.00'),
            current_ecash=Decimal('1000.00'),
            current_physical_cash=Decimal('500.00'),
        )

    def test_cash_in_moves_value_from_ecash_to_physical_cash(self):
        entry = Transaction.create_logged_transaction(
            business=self.profile,
            user=self.user,
            client_name='Ama',
            amount=Decimal('100.00'),
            transaction_type=Transaction.CASH_IN,
        )
        self.profile.refresh_from_db()
        self.assertEqual(entry.ecash_after, Decimal('900.00'))
        self.assertEqual(entry.physical_after, Decimal('600.00'))
        self.assertEqual(self.profile.current_ecash, Decimal('900.00'))
        self.assertEqual(self.profile.current_physical_cash, Decimal('600.00'))

    def test_sending_adds_one_percent_service_charge_to_physical_cash(self):
        entry = Transaction.create_logged_transaction(
            business=self.profile,
            user=self.user,
            client_name='Kojo',
            amount=Decimal('200.00'),
            transaction_type=Transaction.SENDING,
        )
        self.profile.refresh_from_db()
        self.assertEqual(entry.service_charge, Decimal('2.00'))
        self.assertEqual(entry.ecash_after, Decimal('800.00'))
        self.assertEqual(entry.physical_after, Decimal('702.00'))
