from datetime import timedelta

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.customer import Customer
from shop_management.models.data.customer_payment import CustomerPayment, CustomerPaymentAllocation
from shop_management.models.data.sale import Sale, SaleItem
from shop_management.models.data.sale_return import SaleReturn
from shop_management.models.data.shop import Shop
from shop_management.models.data.shop_account import Account
from shop_management.models.data.utils import Currency


class SalesPerformanceTests(TestCase):
    def setUp(self):
        self.user = get_user_model().objects.create_superuser(username="sales-report-admin", password="test")
        self.shop = Shop.objects.create(owner=self.user, name="Kabul")
        self.currency = Currency.objects.create(shop=self.shop, title="AFN")
        self.customer = Customer.objects.create(shop=self.shop, name="Nasim Ltd")
        self.account = Account.objects.create(shop=self.shop, name="Cash", currency=self.currency)
        self.today = timezone.localdate()
        self.sale = Sale.objects.create(shop=self.shop, customer=self.customer, currency=self.currency, invoice_date=self.today, due_date=self.today, calculated_subtotal=1000, calculated_total=900, exchange_rate_used=1, uses_inventory_engine=True, document_status=Sale.DocumentStatus.POSTED, sale_number="KAB-SAL-TEST-1")
        SaleItem.objects.create(
            sale=self.sale,
            quantity=1,
            sale_price=1000,
            cogs_total=600,
        )
        SaleReturn.objects.create(shop=self.shop, sale=self.sale, total_refound_amount=100, uses_inventory_engine=True, document_status=SaleReturn.DocumentStatus.POSTED)
        payment = CustomerPayment.objects.create(customer=self.customer, shop=self.shop, financial_account=self.account, currency=self.currency, amount=300, exchange_rate_used=1, base_amount=300, payment_date=self.today, status=CustomerPayment.Status.POSTED)
        CustomerPaymentAllocation.objects.create(payment=payment, sale=self.sale, allocated_amount=300)
        self.client = APIClient(); self.client.force_authenticate(self.user)

    def test_standard_sales_summary_uses_posted_documents(self):
        response = self.client.get("/api/shop/sales-performance-report/", {"shop": self.shop.pk, "start_date": self.today - timedelta(days=1), "end_date": self.today})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data["summary"]["gross_sales"], "900.00")
        self.assertEqual(response.data["summary"]["returns"], "100.00")
        self.assertEqual(response.data["summary"]["net_sales"], "800.00")
        self.assertEqual(response.data["summary"]["collected"], "300.00")
        self.assertEqual(response.data["invoices"][0]["cogs"], "600.00")
        self.assertEqual(response.data["invoices"][0]["outstanding"], "500.00")
        self.assertEqual(response.data["invoices"][0]["gross_profit"], "200.00")
