"""Find which e-conomic customer has Hosting - AR2 product"""
import asyncio
import sys
import os

# Add app to path
sys.path.insert(0, '/Users/christianthomas/DEV/bmc_hub_dev')

from app.services.economic_service import get_economic_service


async def main():
    service = get_economic_service()
    
    # Search products from screenshot: Office 365, Microsoft 365, Hosting - AR2
    search_terms = ['AR2', 'HOSTING', 'OFFICE 365', 'MICROSOFT 365', 'FAKTURERINGSGEBYR']
    
    # Try a few known customer numbers
    test_customers = [3, 9, 17, 43, 44473156, 30480748, 702007708]
    
    for customer_num in test_customers:
        print(f"\n{'='*60}")
        print(f"Checking customer {customer_num}")
        print('='*60)
        
        try:
            invoices = await service.get_customer_invoices(customer_num, include_lines=True)
            
            if not invoices:
                print(f"  No invoices found")
                continue
                
            print(f"Found {len(invoices)} invoices")
            found_products = set()
            
            for inv in invoices:
                inv_num = inv.get('bookedInvoiceNumber') or inv.get('draftInvoiceNumber')
                lines = inv.get('lines', [])
                
                for line in lines:
                    product_name = line.get('product', {}).get('name') or line.get('description', '')
                    
                    # Check if any search term matches
                    for term in search_terms:
                        if term in product_name.upper():
                            found_products.add(product_name)
                            print(f"  ✅ Invoice {inv_num} ({inv.get('date')}): {product_name}")
                            print(f"     Amount: {line.get('netAmount', 0)}")
                            print(f"     Period: {line.get('period')}")
                            break
            
            if found_products:
                print(f"\n  📦 Unique products found:")
                for prod in sorted(found_products):
                    print(f"     - {prod}")
                    
        except Exception as e:
            print(f"  ❌ Error: {e}")


if __name__ == "__main__":
    asyncio.run(main())
