#!/usr/bin/env python3
"""
Lookup and update missing CVR numbers using CVR.dk API
"""
import sys
import os
import asyncio
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# Override DATABASE_URL for local execution
if "postgres:5432" in os.getenv("DATABASE_URL", ""):
    os.environ["DATABASE_URL"] = os.getenv("DATABASE_URL").replace("postgres:5432", "localhost:5433")

# Add parent directory to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

from app.core.database import execute_query, execute_update, init_db
from app.services.cvr_service import get_cvr_service


async def lookup_missing_cvr():
    """Lookup CVR numbers for customers without CVR using CVR.dk API"""
    
    print("🔌 Forbinder til database...")
    init_db()
    
    print("🔌 Initialiserer CVR service...")
    cvr_service = get_cvr_service()
    
    # Get customers without CVR
    print("\n📊 Henter kunder uden CVR...")
    customers = execute_query("""
        SELECT id, name, cvr_number, city
        FROM customers 
        WHERE (cvr_number IS NULL OR cvr_number = '')
        AND name NOT LIKE %s
        AND name NOT LIKE %s
        ORDER BY name
        LIMIT 100
    """, ('%privat%', '%test%'))
    
    print(f"✅ Fundet {len(customers)} kunder uden CVR (henter max 100)\n")
    
    if len(customers) == 0:
        print("✅ Alle kunder har allerede CVR numre!")
        return
    
    updated = 0
    not_found = 0
    errors = 0
    
    print("🔍 Slår CVR op via CVR.dk API...\n")
    
    for idx, customer in enumerate(customers, 1):
        try:
            print(f"[{idx}/{len(customers)}] {customer['name']:<50} ... ", end='', flush=True)
            
            # Lookup by company name
            result = await cvr_service.lookup_by_name(customer['name'])
            
            if result and result.get('vat'):
                cvr = result['vat']
                
                # Update customer
                execute_update(
                    "UPDATE customers SET cvr_number = %s WHERE id = %s",
                    (cvr, customer['id'])
                )
                
                print(f"✓ CVR: {cvr}")
                updated += 1
            else:
                print("✗ Ikke fundet")
                not_found += 1
            
            # Rate limiting - wait a bit between requests
            await asyncio.sleep(0.5)
            
        except Exception as e:
            print(f"❌ Fejl: {e}")
            errors += 1
    
    # Summary
    print(f"\n{'='*60}")
    print(f"✅ Opslag færdig!")
    print(f"   Opdateret: {updated}")
    print(f"   Ikke fundet: {not_found}")
    print(f"   Fejl: {errors}")
    print(f"{'='*60}\n")


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