#!/bin/bash

# Script to run HTML entity cleaning migration
# This script provides safety checks and backup functionality

set -e  # Exit on any error

# Configuration
DB_URL="${DATABASE_URL:-postgresql://localhost/church_api}"
MIGRATION_FILE="migrations/20250811000001_clean_html_entities.sql"
TEST_FILE="test_html_cleaning_migration.sql"
BACKUP_DIR="./migration_backups"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${BLUE}Church API - HTML Entity Cleaning Migration${NC}"
echo "============================================="
echo

# Check if migration file exists
if [ ! -f "$MIGRATION_FILE" ]; then
    echo -e "${RED}Error: Migration file not found: $MIGRATION_FILE${NC}"
    exit 1
fi

# Check if test file exists
if [ ! -f "$TEST_FILE" ]; then
    echo -e "${RED}Error: Test file not found: $TEST_FILE${NC}"
    exit 1
fi

# Create backup directory
mkdir -p "$BACKUP_DIR"

echo -e "${YELLOW}Step 1: Testing the cleaning function...${NC}"
echo "Running test script to verify the cleaning logic works correctly..."

# Run the test script
if psql "$DB_URL" -f "$TEST_FILE" > /dev/null 2>&1; then
    echo -e "${GREEN}✓ Test passed! The cleaning function works correctly.${NC}"
else
    echo -e "${RED}✗ Test failed! Please check the test output.${NC}"
    echo "Test output:"
    psql "$DB_URL" -f "$TEST_FILE"
    exit 1
fi

echo

echo -e "${YELLOW}Step 2: Creating database backup...${NC}"
BACKUP_FILE="$BACKUP_DIR/backup_before_html_cleaning_$TIMESTAMP.sql"

# Create backup of affected tables
echo "Creating backup of tables that will be modified..."
pg_dump "$DB_URL" \
    --table=bulletins \
    --table=events \
    --table=pending_events \
    --table=members \
    --table=church_config \
    --table=media_items \
    --table=transcoded_media \
    --table=users \
    --data-only \
    --no-owner \
    --no-privileges > "$BACKUP_FILE" 2>/dev/null || {
    echo -e "${YELLOW}Note: Some tables may not exist, continuing with available tables...${NC}"
    # Try with just the core tables that definitely exist
    pg_dump "$DB_URL" \
        --table=bulletins \
        --table=events \
        --table=pending_events \
        --table=church_config \
        --table=users \
        --data-only \
        --no-owner \
        --no-privileges > "$BACKUP_FILE" 2>/dev/null || {
        echo -e "${RED}Failed to create backup. Aborting migration.${NC}"
        exit 1
    }
}

echo -e "${GREEN}✓ Backup created: $BACKUP_FILE${NC}"

echo

echo -e "${YELLOW}Step 3: Analyzing current data for HTML entities...${NC}"

# Check for HTML entities in the database
ENTITY_COUNT=$(psql "$DB_URL" -t -c "
    SELECT COUNT(*) FROM (
        SELECT id FROM bulletins WHERE 
            title ~ '<[^>]*>' OR sabbath_school ~ '<[^>]*>' OR divine_worship ~ '<[^>]*>' OR 
            scripture_reading ~ '<[^>]*>' OR sunset ~ '<[^>]*>' OR
            title ~ '&(nbsp|amp|lt|gt|quot|#39);' OR sabbath_school ~ '&(nbsp|amp|lt|gt|quot|#39);' OR 
            divine_worship ~ '&(nbsp|amp|lt|gt|quot|#39);' OR scripture_reading ~ '&(nbsp|amp|lt|gt|quot|#39);' OR 
            sunset ~ '&(nbsp|amp|lt|gt|quot|#39);'
        UNION ALL
        SELECT id FROM events WHERE 
            title ~ '<[^>]*>' OR description ~ '<[^>]*>' OR location ~ '<[^>]*>' OR 
            location_url ~ '<[^>]*>' OR approved_from ~ '<[^>]*>' OR
            title ~ '&(nbsp|amp|lt|gt|quot|#39);' OR description ~ '&(nbsp|amp|lt|gt|quot|#39);' OR 
            location ~ '&(nbsp|amp|lt|gt|quot|#39);' OR location_url ~ '&(nbsp|amp|lt|gt|quot|#39);' OR 
            approved_from ~ '&(nbsp|amp|lt|gt|quot|#39);'
        UNION ALL
        SELECT id FROM pending_events WHERE 
            title ~ '<[^>]*>' OR description ~ '<[^>]*>' OR location ~ '<[^>]*>' OR 
            location_url ~ '<[^>]*>' OR admin_notes ~ '<[^>]*>' OR
            title ~ '&(nbsp|amp|lt|gt|quot|#39);' OR description ~ '&(nbsp|amp|lt|gt|quot|#39);' OR 
            location ~ '&(nbsp|amp|lt|gt|quot|#39);' OR admin_notes ~ '&(nbsp|amp|lt|gt|quot|#39);'
    ) AS dirty_records;
" | xargs)

echo "Found $ENTITY_COUNT records with HTML tags or entities that need cleaning."

if [ "$ENTITY_COUNT" -eq 0 ]; then
    echo -e "${GREEN}✓ No HTML entities found! Database is already clean.${NC}"
    echo "Migration can still be run to install the cleaning function for future use."
fi

echo

echo -e "${YELLOW}Step 4: Ready to run migration${NC}"
echo "This will:"
echo "  • Install the clean_html_entities() function"
echo "  • Clean HTML tags and entities from all text fields"
echo "  • Update the updated_at timestamps for modified records"
echo "  • Provide detailed logging of what was cleaned"
echo
echo "Backup location: $BACKUP_FILE"

# Ask for confirmation
read -p "Do you want to proceed with the migration? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo -e "${YELLOW}Migration cancelled by user.${NC}"
    exit 0
fi

echo

echo -e "${YELLOW}Step 5: Running migration...${NC}"

# Run the migration
if psql "$DB_URL" -f "$MIGRATION_FILE"; then
    echo
    echo -e "${GREEN}✓ Migration completed successfully!${NC}"
    echo
    echo -e "${BLUE}Summary:${NC}"
    echo "• HTML tags and entities have been cleaned from all text fields"
    echo "• Database backup is available at: $BACKUP_FILE"
    echo "• The clean_html_entities() function is now available for future use"
    echo "• All API responses will now return clean data"
    
    echo
    echo -e "${YELLOW}Next steps:${NC}"
    echo "1. Test your API endpoints to verify clean data"
    echo "2. Monitor for any issues with data formatting"
    echo "3. Keep the backup file until you're confident everything works correctly"
    
    echo
    echo -e "${GREEN}Migration completed successfully!${NC}"
else
    echo -e "${RED}✗ Migration failed!${NC}"
    echo
    echo -e "${YELLOW}Rollback instructions:${NC}"
    echo "1. Restore from backup: psql \"$DB_URL\" < \"$BACKUP_FILE\""
    echo "2. Check the migration logs above for error details"
    echo "3. Fix any issues and try again"
    exit 1
fi