#!/usr/bin/env python3
"""
Sync CVR numbers from Simply-CRM (OLD vTiger system) to local customers database
"""
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.simplycrm_service import SimplyCRMService


async def sync_cvr_from_simplycrm():
    """Sync CVR numbers from Simply-CRM to local customers"""
    
    print("🔌 Forbinder til database...")
    init_db()
    
    print("🔌 Forbinder til Simply-CRM (bmcnetworks.simply-crm.dk)...")
    
    # Get all customers with vtiger_id but no cvr_number
    print("\n📊 Henter kunder uden CVR...")
    customers = execute_query("""
        SELECT id, name, vtiger_id, cvr_number
        FROM customers 
        WHERE vtiger_id IS NOT NULL 
        AND (cvr_number IS NULL OR cvr_number = '')
        ORDER BY name
    """, ())
    
    print(f"✅ Fundet {len(customers)} kunder uden CVR\n")
    
    if len(customers) == 0:
        print("✅ Alle kunder har allerede CVR numre!")
        return
    
    # Fetch accounts from Simply-CRM
    async with SimplyCRMService() as simplycrm:
        print("📥 Henter alle firmaer fra Simply-CRM...")
        
        # Query all accounts - lad os se hvilke felter der findes
        query = "SELECT * FROM Accounts LIMIT 10;"
        sample = await simplycrm.query(query)
        
        if sample:
            print(f"\n📋 Sample account:")
            print(f"   account_id: {sample[0].get('account_id')}")
            print(f"   id: {sample[0].get('id')}")
            print(f"   accountname: {sample[0].get('accountname')}")
            print(f"   vat_number: {sample[0].get('vat_number')}")
        
        # Query alle accounts
        query = "SELECT * FROM Accounts LIMIT 5000;"
        accounts = await simplycrm.query(query)
        
        print(f"✅ Fundet {len(accounts)} firmaer i Simply-CRM med CVR\n")
        
        if len(accounts) == 0:
            print("⚠️  Ingen firmaer med CVR i Simply-CRM!")
            return
        
        # Map Company NAME to CVR (Simply-CRM og vTiger har forskellige ID systemer!)
        name_to_cvr = {}
        for acc in accounts:
            company_name = acc.get('accountname', '').strip().lower()
            cvr = acc.get('vat_number', '').strip()
            if company_name and cvr and cvr not in ['', 'null', 'NULL']:
                # Clean CVR (remove spaces, dashes, "DK" prefix)
                cvr_clean = cvr.replace(' ', '').replace('-', '').replace('DK', '').replace('dk', '')
                if cvr_clean.isdigit() and len(cvr_clean) == 8:
                    name_to_cvr[company_name] = cvr_clean
        
        print(f"✅ {len(name_to_cvr)} unikke firmanavne mapped med CVR\n")
        
        # Match and update by company name
        updated = 0
        skipped = 0
        
        print("🔄 Opdaterer CVR numre...\n")
        
        for customer in customers:
            customer_name = customer['name'].strip().lower()
            
            if customer_name in name_to_cvr:
                cvr = name_to_cvr[customer_name]
                
                # Check if CVR already exists on another customer
                existing = execute_query(
                    "SELECT id, name FROM customers WHERE cvr_number = %s AND id != %s",
                    (cvr, customer['id']),
                    fetchone=True
                )
                
                if existing:
                    print(f"  ⚠️ {customer['name']:<50} CVR {cvr} allerede brugt af: {existing['name']}")
                    skipped += 1
                    continue
                
                # Update customer
                try:
                    execute_update(
                        "UPDATE customers SET cvr_number = %s WHERE id = %s",
                        (cvr, customer['id'])
                    )
                    print(f"  ✓ {customer['name']:<50} CVR: {cvr}")
                    updated += 1
                except Exception as e:
                    print(f"  ❌ {customer['name']:<50} Error: {e}")
                    skipped += 1
            else:
                skipped += 1
        
        # Summary
        print(f"\n{'='*60}")
        print(f"✅ Opdatering færdig!")
        print(f"   Opdateret: {updated}")
        print(f"   Ikke fundet i Simply-CRM: {skipped}")
        print(f"{'='*60}\n")


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