"""
Test payment method filtering by country
"""
from django.test import TestCase, Client
from django.contrib.auth import get_user_model
from igny8_core.business.billing.models import PaymentMethodConfig

User = get_user_model()


class PaymentMethodFilteringTest(TestCase):
    """Test payment method filtering by billing country"""
    
    def setUp(self):
        """Create test payment method configs"""
        # Global methods (available everywhere)
        PaymentMethodConfig.objects.create(
            country_code='*',
            payment_method='stripe',
            display_name='Credit/Debit Card',
            is_enabled=True,
            sort_order=1,
        )
        PaymentMethodConfig.objects.create(
            country_code='*',
            payment_method='paypal',
            display_name='PayPal',
            is_enabled=True,
            sort_order=2,
        )
        
        # Country-specific methods
        PaymentMethodConfig.objects.create(
            country_code='GB',
            payment_method='bank_transfer',
            display_name='Bank Transfer (UK)',
            is_enabled=True,
            sort_order=3,
        )
        PaymentMethodConfig.objects.create(
            country_code='IN',
            payment_method='local_wallet',
            display_name='UPI/Wallets',
            is_enabled=True,
            sort_order=4,
        )
        PaymentMethodConfig.objects.create(
            country_code='PK',
            payment_method='bank_transfer',
            display_name='Bank Transfer (Pakistan)',
            is_enabled=True,
            sort_order=5,
        )
        
        # Disabled method (should not appear)
        PaymentMethodConfig.objects.create(
            country_code='*',
            payment_method='manual',
            display_name='Manual',
            is_enabled=False,
            sort_order=99,
        )
        
        self.client = Client()
    
    def test_filter_payment_methods_by_us(self):
        """Test filtering for US country - should get only global methods"""
        response = self.client.get('/api/v1/billing/admin/payment-methods/?country=US')
        
        self.assertEqual(response.status_code, 200)
        data = response.json()
        
        self.assertTrue(data['success'])
        self.assertEqual(len(data['results']), 2)  # Only stripe and paypal
        
        methods = [m['type'] for m in data['results']]
        self.assertIn('stripe', methods)
        self.assertIn('paypal', methods)
    
    def test_filter_payment_methods_by_gb(self):
        """Test filtering for GB - should get global + GB-specific"""
        response = self.client.get('/api/v1/billing/admin/payment-methods/?country=GB')
        
        self.assertEqual(response.status_code, 200)
        data = response.json()
        
        self.assertTrue(data['success'])
        self.assertEqual(len(data['results']), 3)  # stripe, paypal, bank_transfer(GB)
        
        methods = [m['type'] for m in data['results']]
        self.assertIn('stripe', methods)
        self.assertIn('paypal', methods)
        self.assertIn('bank_transfer', methods)
    
    def test_filter_payment_methods_by_in(self):
        """Test filtering for IN - should get global + IN-specific"""
        response = self.client.get('/api/v1/billing/admin/payment-methods/?country=IN')
        
        self.assertEqual(response.status_code, 200)
        data = response.json()
        
        self.assertTrue(data['success'])
        self.assertEqual(len(data['results']), 3)  # stripe, paypal, local_wallet(IN)
        
        methods = [m['type'] for m in data['results']]
        self.assertIn('stripe', methods)
        self.assertIn('paypal', methods)
        self.assertIn('local_wallet', methods)
    
    def test_disabled_methods_not_returned(self):
        """Test that disabled payment methods are not included"""
        response = self.client.get('/api/v1/billing/admin/payment-methods/?country=*')
        
        self.assertEqual(response.status_code, 200)
        data = response.json()
        
        methods = [m['type'] for m in data['results']]
        self.assertNotIn('manual', methods)  # Disabled method should not appear
    
    def test_sort_order_respected(self):
        \"\"\"Test that payment methods are returned in sort_order\"\"\"
        response = self.client.get('/api/v1/billing/admin/payment-methods/?country=GB')
        
        self.assertEqual(response.status_code, 200)
        data = response.json()
        
        # Verify first method has lowest sort_order
        self.assertEqual(data['results'][0]['type'], 'stripe')
        self.assertEqual(data['results'][0]['sort_order'], 1)
    
    def test_default_country_fallback(self):
        """Test that missing country parameter defaults to global (*)\"\"\"\n        response = self.client.get('/api/v1/billing/admin/payment-methods/')
        
        self.assertEqual(response.status_code, 200)
        data = response.json()
        
        self.assertTrue(data['success'])
        # Should get at least global methods
        methods = [m['type'] for m in data['results']]
        self.assertIn('stripe', methods)
        self.assertIn('paypal', methods)
