#!/bin/bash
# Synkroniser BMC Office abonnementer med customer ID mapping

set -e

PROD_SERVER="bmcadmin@172.16.31.183"

echo "🔍 Eksporterer BMC Office abonnementer fra lokal dev..."

# Export with firma_name for mapping (not customer_id)
docker exec bmc-hub-postgres psql -U bmc_hub -d bmc_hub -t -A -F'|' -c "
SELECT 
  firma_id,
  firma_name,
  start_date,
  text,
  antal,
  pris,
  rabat,
  beskrivelse,
  faktura_firma_id,
  faktura_firma_name,
  active
FROM bmc_office_subscriptions
ORDER BY firma_name, id;" > /tmp/bmc_subs_export.csv

LINES=$(wc -l < /tmp/bmc_subs_export.csv | tr -d ' ')
echo "✅ Eksporteret $LINES records"

echo ""
echo "📤 Uploader og importerer til prod med customer mapping..."

# SSH to prod and run import with customer mapping
ssh $PROD_SERVER << 'ENDSSH'
  echo "📥 Modtager data..."
  cat > /tmp/bmc_subs_data.csv
  
  echo "🗑️  Rydder eksisterende abonnementer..."
  podman exec bmc-hub-postgres-prod psql -U bmc_hub -d bmc_hub -c "TRUNCATE TABLE bmc_office_subscriptions RESTART IDENTITY CASCADE;" > /dev/null
  
  echo "📥 Importerer med customer ID mapping..."
  podman exec -i bmc-hub-postgres-prod psql -U bmc_hub -d bmc_hub << 'EOSQL'
    CREATE TEMP TABLE bmc_import (
      firma_id VARCHAR(50),
      firma_name VARCHAR(255),
      start_date DATE,
      text VARCHAR(500),
      antal DECIMAL(10,2),
      pris DECIMAL(10,2),
      rabat DECIMAL(10,2),
      beskrivelse TEXT,
      faktura_firma_id VARCHAR(50),
      faktura_firma_name VARCHAR(255),
      active BOOLEAN
    );
    
    \COPY bmc_import FROM '/tmp/bmc_subs_data.csv' WITH (FORMAT csv, DELIMITER '|');
    
    INSERT INTO bmc_office_subscriptions (
      customer_id, firma_id, firma_name, start_date, text, antal, pris, rabat,
      beskrivelse, faktura_firma_id, faktura_firma_name, active
    )
    SELECT 
      c.id as customer_id,
      bi.firma_id,
      bi.firma_name,
      bi.start_date,
      bi.text,
      bi.antal,
      bi.pris,
      bi.rabat,
      bi.beskrivelse,
      bi.faktura_firma_id,
      bi.faktura_firma_name,
      bi.active
    FROM bmc_import bi
    LEFT JOIN customers c ON LOWER(TRIM(c.name)) = LOWER(TRIM(bi.firma_name))
    WHERE c.id IS NOT NULL;
    
    SELECT 
      COUNT(*) as total_imported,
      COUNT(DISTINCT customer_id) as unique_customers
    FROM bmc_office_subscriptions;
EOSQL
  
  echo ""
  echo "✅ Import færdig! Verificerer..."
  podman exec bmc-hub-postgres-prod psql -U bmc_hub -d bmc_hub -c "
    SELECT 
      COUNT(*) as total_records, 
      COUNT(CASE WHEN active THEN 1 END) as active_records,
      ROUND(SUM(CASE WHEN active THEN total_inkl_moms ELSE 0 END)::numeric, 2) as total_value_dkk
    FROM bmc_office_subscription_totals;
  "
  
  echo ""
  echo "📊 Sample kunde check (Mate.Bike):"
  podman exec bmc-hub-postgres-prod psql -U bmc_hub -d bmc_hub -c "
    SELECT 
      c.id as customer_id,
      c.name,
      COUNT(bos.id) as bmc_subs_count,
      ROUND(SUM(bos.total_inkl_moms)::numeric, 2) as total_value
    FROM customers c
    JOIN bmc_office_subscription_totals bos ON bos.customer_id = c.id
    WHERE c.name LIKE '%Mate%'
    GROUP BY c.id, c.name;
  "
  
  echo ""
  echo "🧹 Rydder op..."
  rm /tmp/bmc_subs_data.csv
ENDSSH < /tmp/bmc_subs_export.csv

echo ""
echo "🎉 Færdig! BMC Office abonnementer er synkroniseret til produktion."
echo ""
echo "🔗 Test på: http://172.16.31.183:8000/customers/183 -> Abonnnents tjek tab (Mate.Bike)"
