#!/bin/bash

# Script to clear certificate test data on staging server
# This script should be run on the staging server

echo "=== Certificate Test Data Cleanup Script ==="
echo "This script will clear certificate test data from the staging server."
echo ""

# Check if wp-cli is available
if ! command -v wp &> /dev/null; then
    echo "Error: wp-cli is not installed or not in PATH"
    exit 1
fi

# Navigate to WordPress directory
cd /home/uberrxmprk/cloudwaysapps.com/rfymqitokx/public_html

echo "1. Checking current certificate count..."
CERT_COUNT=$(wp db query "SELECT COUNT(*) FROM wp_hvac_certificates" --skip-column-names 2>/dev/null || echo "0")
echo "   Found $CERT_COUNT certificates in database"

echo ""
echo "2. Checking certificate files..."
CERT_DIR="/home/uberrxmprk/cloudwaysapps.com/rfymqitokx/public_html/wp-content/uploads/hvac-certificates"
if [ -d "$CERT_DIR" ]; then
    FILE_COUNT=$(find "$CERT_DIR" -type f -name "*.pdf" 2>/dev/null | wc -l)
    echo "   Found $FILE_COUNT PDF files in certificate directory"
else
    echo "   Certificate directory not found"
    FILE_COUNT=0
fi

echo ""
echo "What would you like to do?"
echo "1) Clear ALL certificates (database and files)"
echo "2) Clear only TEST certificates (generated by test_trainer)"
echo "3) Clear only certificate FILES (keep database records)"
echo "4) Clear only certificate DATABASE records (keep files)"
echo "5) Exit without changes"
echo ""
read -p "Enter your choice (1-5): " choice

case $choice in
    1)
        echo ""
        echo "Clearing ALL certificates..."
        
        # Clear database
        wp db query "TRUNCATE TABLE wp_hvac_certificates"
        echo "✓ Database cleared"
        
        # Clear files
        if [ -d "$CERT_DIR" ]; then
            rm -rf "$CERT_DIR"/*
            echo "✓ Certificate files cleared"
        fi
        
        # Clear any certificate tokens
        wp db query "DELETE FROM wp_options WHERE option_name LIKE '_transient_hvac_cert_%' OR option_name LIKE '_transient_timeout_hvac_cert_%'"
        echo "✓ Certificate tokens cleared"
        
        echo ""
        echo "All certificate data has been cleared!"
        ;;
        
    2)
        echo ""
        echo "Clearing TEST certificates only..."
        
        # Get test_trainer user ID
        TEST_USER_ID=$(wp user get test_trainer --field=ID 2>/dev/null)
        
        if [ -z "$TEST_USER_ID" ]; then
            echo "Error: test_trainer user not found"
            exit 1
        fi
        
        echo "Found test_trainer user ID: $TEST_USER_ID"
        
        # Get certificate IDs for test_trainer
        CERT_IDS=$(wp db query "SELECT certificate_id FROM wp_hvac_certificates WHERE generated_by = $TEST_USER_ID" --skip-column-names)
        
        if [ -n "$CERT_IDS" ]; then
            # Delete certificates from database
            wp db query "DELETE FROM wp_hvac_certificates WHERE generated_by = $TEST_USER_ID"
            echo "✓ Test certificates removed from database"
            
            # Delete certificate files (if we can identify them)
            # This is more complex as we need to match certificate numbers to files
            echo "✓ Note: Certificate files should be manually reviewed in $CERT_DIR"
        else
            echo "No test certificates found"
        fi
        ;;
        
    3)
        echo ""
        echo "Clearing certificate FILES only..."
        
        if [ -d "$CERT_DIR" ]; then
            rm -rf "$CERT_DIR"/*
            echo "✓ Certificate files cleared"
            echo "Note: Database records remain intact"
        else
            echo "Certificate directory not found"
        fi
        ;;
        
    4)
        echo ""
        echo "Clearing certificate DATABASE records only..."
        
        wp db query "TRUNCATE TABLE wp_hvac_certificates"
        echo "✓ Database cleared"
        echo "Note: Certificate files remain in $CERT_DIR"
        ;;
        
    5)
        echo ""
        echo "Exiting without changes."
        exit 0
        ;;
        
    *)
        echo ""
        echo "Invalid choice. Exiting."
        exit 1
        ;;
esac

echo ""
echo "=== Cleanup Complete ==="
echo ""
echo "To regenerate test certificates:"
echo "1. Log in as test_trainer"
echo "2. Navigate to Generate Certificates page"
echo "3. Select an event and attendees"
echo "4. Generate new certificates"