from django.contrib.auth import get_user_model
from django.test import TestCase
from django.utils import timezone
from rest_framework.test import APIClient

from shop_management.models.data.account_transaction import AccountTransaction
from shop_management.models.data.customer import Customer
from shop_management.models.data.customer_payment import CustomerPayment
from shop_management.models.data.shop import Shop
from shop_management.models.data.shop_account import Account
from shop_management.models.data.supplier import Supplier
from shop_management.models.data.supplier_payment import SupplierPayment
from shop_management.models.data.utils import Currency


class FinanceReportTests(TestCase):
    def setUp(self):
        self.user = get_user_model().objects.create_user(username="finance-report", is_staff=True)
        self.user.allowed_tabs = ["reports"]; self.user.save(update_fields=["allowed_tabs"])
        self.shop = Shop.objects.create(owner=self.user, name="Kabul")
        self.currency = Currency.objects.create(shop=self.shop, title="AFN")
        self.account = Account.objects.create(shop=self.shop, name="Main Cash", currency=self.currency, opening_balance=100)
        self.customer = Customer.objects.create(shop=self.shop, name="Customer")
        self.supplier = Supplier.objects.create(shop=self.shop, name="Supplier")
        AccountTransaction.objects.create(account=self.account, account_currency=self.currency, transaction_currency=self.currency, transaction_type="credit", amount=500, exchange_rate=1, converted_amount=500)
        AccountTransaction.objects.create(account=self.account, account_currency=self.currency, transaction_currency=self.currency, transaction_type="debit", amount=200, exchange_rate=1, converted_amount=200)
        CustomerPayment.objects.create(shop=self.shop, customer=self.customer, financial_account=self.account, currency=self.currency, amount=500, base_amount=500, status="POSTED")
        SupplierPayment.objects.create(shop=self.shop, supplier=self.supplier, financial_account=self.account, currency=self.currency, amount=200, base_amount=200, service_charge=10, status="POSTED")
        self.client = APIClient(); self.client.force_authenticate(self.user)

    def test_account_balance_and_cash_flow_summaries(self):
        response = self.client.get("/api/shop/finance-report/", {"shop": self.shop.pk, "start_date": timezone.localdate(), "end_date": timezone.localdate()})
        self.assertEqual(response.status_code, 200, response.data)
        self.assertEqual(response.data["accounts"][0]["balance"], "400.00")
        self.assertEqual(response.data["summary"]["receipts_base"], "500.00")
        self.assertEqual(response.data["summary"]["payments_base"], "200.00")
        self.assertEqual(response.data["summary"]["service_charges_by_currency"], [{"currency": "AFN", "amount": "10.00"}])

    def test_other_branch_is_rejected(self):
        other = get_user_model().objects.create_user(username="finance-other")
        other_shop = Shop.objects.create(owner=other, name="Herat")
        response = self.client.get("/api/shop/finance-report/", {"shop": other_shop.pk})
        self.assertEqual(response.status_code, 400)
