from django.contrib.auth import get_user_model
from django.test import TestCase
from django.utils import timezone
from unittest.mock import patch, MagicMock

from stock import models as stock_models
from business import models as business_models, services

from .fixtures import create_basic_fixtures, create_sales_fixtures


class PurchaseOrderStockServiceTestCase(TestCase):
    def setUp(self):
        (
            self.merchant,
            self.supplier,
            self.warehouse_strict,
            self.warehouse_relaxed,
            self.product,
            self.operator,
        ) = create_basic_fixtures()
        User = get_user_model()
        self.user = User.objects.create_user(username='svc-user', password='pass123')
        self.purchase_order = business_models.PurchaseOrder.objects.create(
            merchant=self.merchant,
            supplier=self.supplier,
            purchase_date=timezone.now().date(),
            operator=self.operator,
            warehouse=self.warehouse_strict,
        )
        self.items = [{'product_id': self.product.id, 'quantities': ['8']}]

    def test_service_calls_stock_flow_service(self):
        with patch('business.services.StockFlowService') as mock_flow_cls:
            mock_instance = mock_flow_cls.return_value
            mock_instance.stock_in.return_value = (MagicMock(id=321), [], 2)

            payload = services.create_purchase_order_stock_entries_sync(
                purchase_order_id=self.purchase_order.id,
                warehouse_id=self.warehouse_strict.id,
                items=self.items,
                created_by_id=self.user.id,
            )

        mock_flow_cls.assert_called_once_with(merchant=self.merchant, created_by=self.user)
        mock_instance.stock_in.assert_called_once_with(
            warehouse_id=self.warehouse_strict.id,
            source_type=stock_models.StockChangeSourceEnum.PURCHASE,
            source_id=self.purchase_order.id,
            items=[{'product_id': self.product.id, 'quantities': ['8']}],
        )
        self.assertEqual(payload['purchase_order_id'], self.purchase_order.id)
        self.assertEqual(payload['stock_change_record_id'], 321)


class SalesOrderStockServiceTestCase(TestCase):
    def setUp(self):
        (
            self.merchant,
            self.customer,
            self.warehouse_strict,
            self.warehouse_relaxed,
            self.warehouse_strict_out,
            self.product,
            self.operator,
        ) = create_sales_fixtures()
        User = get_user_model()
        self.user = User.objects.create_user(username='sales-svc', password='pass123')
        self.sales_order = business_models.SalesOrder.objects.create(
            merchant=self.merchant,
            customer=self.customer,
            sales_date=timezone.now().date(),
            operator=self.operator,
            warehouse=self.warehouse_strict,
        )
        self.items = [{'product_id': self.product.id, 'quantities': ['5']}]

    def test_service_calls_stock_out(self):
        with patch('business.services.StockFlowService') as mock_flow_cls:
            mock_instance = mock_flow_cls.return_value
            mock_instance.stock_out.return_value = (MagicMock(id=654), [], 1)

            payload = services.create_sales_order_stock_entries_sync(
                sales_order_id=self.sales_order.id,
                warehouse_id=self.warehouse_strict.id,
                items=self.items,
                created_by_id=self.user.id,
            )

        mock_flow_cls.assert_called_once_with(merchant=self.merchant, created_by=self.user)
        mock_instance.stock_out.assert_called_once_with(
            warehouse_id=self.warehouse_strict.id,
            source_type=stock_models.StockChangeSourceEnum.SALES,
            source_id=self.sales_order.id,
            items=[{'product_id': self.product.id, 'quantities': ['5']}],
        )
        self.assertEqual(payload['sales_order_id'], self.sales_order.id)
        self.assertEqual(payload['stock_change_record_id'], 654)

    def test_service_calls_stock_out_strict_mode(self):
        strict_order = business_models.SalesOrder.objects.create(
            merchant=self.merchant,
            customer=self.customer,
            sales_date=timezone.now().date(),
            operator=self.operator,
            warehouse=self.warehouse_strict_out,
        )
        consume_items = [{'product_id': self.product.id, 'consume_detail_ids': [11, 12], 'quantity': 50}]
        with patch('business.services.StockFlowService') as mock_flow_cls:
            mock_instance = mock_flow_cls.return_value
            mock_instance.stock_out.return_value = (MagicMock(id=987), [], 2)

            payload = services.create_sales_order_stock_entries_sync(
                sales_order_id=strict_order.id,
                warehouse_id=self.warehouse_strict_out.id,
                items=consume_items,
                created_by_id=self.user.id,
            )

        mock_flow_cls.assert_called_once_with(merchant=self.merchant, created_by=self.user)
        mock_instance.stock_out.assert_called_once_with(
            warehouse_id=self.warehouse_strict_out.id,
            source_type=stock_models.StockChangeSourceEnum.SALES,
            source_id=strict_order.id,
            items=[{'product_id': self.product.id, 'consume_detail_ids': [11, 12], 'quantity': 50}],
        )
        self.assertEqual(payload['sales_order_id'], strict_order.id)
        self.assertEqual(payload['stock_change_record_id'], 987)
