#!/bin/bash
# Final Production Data Import - Uses the original extracted files
# Directly pipes the SQL with proper DELETE statements

echo "=========================================="
echo "Production Data Import - Final"
echo "Date: 2025-11-13"
echo "=========================================="
echo ""

MYSQL_CMD="docker exec -i dev-mysql mysql -u root -prootpassword shopdb"

echo "Creating backups..."
$MYSQL_CMD <<'EOF'
DROP TABLE IF EXISTS notifications_backup_20251113;
DROP TABLE IF EXISTS notificationtypes_backup_20251113;
DROP TABLE IF EXISTS printers_backup_20251113;
DROP TABLE IF EXISTS knowledgebase_backup_20251113;

CREATE TABLE notifications_backup_20251113 LIKE notifications;
INSERT INTO notifications_backup_20251113 SELECT * FROM notifications;

CREATE TABLE notificationtypes_backup_20251113 LIKE notificationtypes;
INSERT INTO notificationtypes_backup_20251113 SELECT * FROM notificationtypes;

CREATE TABLE printers_backup_20251113 LIKE printers;
INSERT INTO printers_backup_20251113 SELECT * FROM printers;

CREATE TABLE knowledgebase_backup_20251113 LIKE knowledgebase;
INSERT INTO knowledgebase_backup_20251113 SELECT * FROM knowledgebase;
EOF

echo "✓ Backups created"
echo ""

echo "Importing production data..."
cat /home/camp/projects/windows/shopdb/sql/prod_notificationtypes.sql | $MYSQL_CMD 2>&1 | tail -1
cat /home/camp/projects/windows/shopdb/sql/prod_notifications.sql | $MYSQL_CMD 2>&1 | tail -1
cat /home/camp/projects/windows/shopdb/sql/prod_printers.sql | $MYSQL_CMD 2>&1 | tail -1
cat /home/camp/projects/windows/shopdb/sql/prod_knowledgebase.sql | $MYSQL_CMD 2>&1 | tail -1

echo ""
echo "✓ Import complete"
echo ""

$MYSQL_CMD <<'EOF'
SELECT '=== IMPORT RESULTS ===' AS '';
SELECT
    (SELECT COUNT(*) FROM notifications) AS notifications,
    (SELECT COUNT(*) FROM notificationtypes) AS notificationtypes,
    (SELECT COUNT(*) FROM printers) AS printers,
    (SELECT COUNT(*) FROM knowledgebase) AS knowledgebase;
EOF

echo ""
echo "=========================================="
echo "✓ Production data imported successfully!"
echo "=========================================="
echo ""
echo "NOTE: All notifications have notificationtypeid=1 (gray)"
echo "Use bulk_update_notification_types.asp to assign colors"
echo ""
echo "Access at: http://192.168.122.151:8080/bulk_update_notification_types.asp"
echo ""
