#!/usr/bin/env python3
"""
Import e-conomic customers fra CSV til Hub database
"""
import sys
import csv
import subprocess

def run_sql(sql):
    """Kør SQL kommando via podman"""
    cmd = [
        'sudo', 'podman', 'exec', '-i', 'bmc-hub-postgres-prod',
        'psql', '-U', 'bmc_hub', '-d', 'bmc_hub', '-t', '-c', sql
    ]
    result = subprocess.run(cmd, capture_output=True, text=True)
    return result.stdout.strip()

def main():
    if len(sys.argv) < 2:
        print("Brug: python3 import_economic_csv.py <csv-file>")
        sys.exit(1)
    
    csv_file = sys.argv[1]
    print(f"📂 Læser {csv_file}...")
    
    updated = 0
    created = 0
    skipped = 0
    
    with open(csv_file, 'r', encoding='utf-8-sig') as f:
        # Skip første 3 linjer (header osv.)
        for _ in range(3):
            next(f)
        
        reader = csv.DictReader(f, delimiter=';')
        
        for row in reader:
            nummer = row.get('Nr.', '').strip()
            navn = row.get('Navn', '').strip()
            
            if not nummer or not navn:
                continue
            
            # Skip non-numeric
            if not nummer.isdigit():
                continue
            
            nummer_int = int(nummer)
            
            # Skip for store numre
            if nummer_int > 999999999:
                print(f"⚠️  Springer over {navn} - Nummer for stort: {nummer}")
                skipped += 1
                continue
            
            print(f"[{updated + created + skipped + 1}] {nummer} - {navn}")
            
            # Escape quotes
            navn_esc = navn.replace("'", "''")
            
            # Find kunde
            find_sql = f"SELECT id FROM customers WHERE LOWER(name) = LOWER('{navn_esc}') LIMIT 1;"
            kunde_id = run_sql(find_sql).strip()
            
            if kunde_id and kunde_id.isdigit():
                # Update
                update_sql = f"UPDATE customers SET economic_customer_number = {nummer_int} WHERE id = {kunde_id};"
                run_sql(update_sql)
                print(f"  ✅ Opdateret ID {kunde_id}")
                updated += 1
            else:
                # Create
                create_sql = f"INSERT INTO customers (name, economic_customer_number, created_at, updated_at) VALUES ('{navn_esc}', {nummer_int}, NOW(), NOW());"
                run_sql(create_sql)
                print(f"  ➕ Oprettet")
                created += 1
    
    print(f"\n✅ Færdig!")
    print(f"   Opdateret: {updated}")
    print(f"   Oprettet:  {created}")
    print(f"   Sprunget over: {skipped}")
    print(f"   Total: {updated + created}")

if __name__ == '__main__':
    main()
