#!/bin/bash

# Deploy Certificate Fixes Script
# This script deploys fixes for the certificate functionality in the HVAC Community Events plugin.

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

# Function to print colored messages
print_message() {
    local type=$1
    local message=$2
    local color=$BLUE
    
    case $type in
        "info")
            color=$BLUE
            ;;
        "success")
            color=$GREEN
            ;;
        "warning")
            color=$YELLOW
            ;;
        "error")
            color=$RED
            ;;
    esac
    
    echo -e "${color}${message}${NC}"
}

# Check if we have a WordPress installation
if [ ! -d "wordpress" ]; then
    print_message "error" "No WordPress installation found in ./wordpress directory."
    print_message "info" "Please run this script from the wordpress-dev directory."
    exit 1
fi

# Check that we're in the right directory
if [ ! -f "bin/debug-certificate-system.sh" ]; then
    print_message "error" "Please run this script from the wordpress-dev directory."
    exit 1
fi

print_message "info" "=== Deploying Certificate Fixes ==="
print_message "info" ""

# Step 1: Deactivate plugin to clear any cached code
print_message "info" "Step 1: Running check and fix database script..."
php bin/check-and-fix-certificate-tables.php

# Step 2: Fix certificate directory permissions
print_message "info" ""
print_message "info" "Step 2: Fixing certificate directory permissions..."

# Get the certificate directory path
UPLOAD_DIR=$(php -r "echo wp_upload_dir()['basedir'];")
CERT_DIR="$UPLOAD_DIR/hvac-certificates"

if [ -d "$CERT_DIR" ]; then
    chmod -R 755 "$CERT_DIR"
    print_message "success" "✓ Fixed permissions for certificate directory: $CERT_DIR"
else
    print_message "warning" "! Certificate directory not found at: $CERT_DIR"
    print_message "info" "Creating certificate directory..."
    mkdir -p "$CERT_DIR"
    chmod -R 755 "$CERT_DIR"
    
    if [ -d "$CERT_DIR" ]; then
        print_message "success" "✓ Created certificate directory: $CERT_DIR"
    else
        print_message "error" "✗ Failed to create certificate directory"
    fi
fi

# Step 3: Clear cache
print_message "info" ""
print_message "info" "Step 3: Clearing cache..."

# Clear WordPress transients
php -r "include_once './wordpress/wp-load.php'; wp_cache_flush(); delete_transient('hvac_certificate_cache');"
print_message "success" "✓ WordPress cache cleared"

# Step 4: Check plugin status
print_message "info" ""
print_message "info" "Step 4: Checking plugin status..."

# Check if the plugin is active
PLUGIN_ACTIVE=$(php -r "include_once './wordpress/wp-load.php'; include_once(ABSPATH . 'wp-admin/includes/plugin.php'); echo (int)is_plugin_active('hvac-community-events/hvac-community-events.php');")

if [ "$PLUGIN_ACTIVE" -eq "1" ]; then
    print_message "success" "✓ Plugin is active."
else
    print_message "warning" "! Plugin is not active. Attempting to activate..."
    php -r "include_once './wordpress/wp-load.php'; include_once(ABSPATH . 'wp-admin/includes/plugin.php'); activate_plugin('hvac-community-events/hvac-community-events.php');"
    
    # Check again
    PLUGIN_ACTIVE=$(php -r "include_once './wordpress/wp-load.php'; include_once(ABSPATH . 'wp-admin/includes/plugin.php'); echo (int)is_plugin_active('hvac-community-events/hvac-community-events.php');")
    
    if [ "$PLUGIN_ACTIVE" -eq "1" ]; then
        print_message "success" "✓ Plugin activated successfully."
    else
        print_message "error" "✗ Failed to activate plugin."
    fi
fi

# Final step: Flush rewrite rules
print_message "info" ""
print_message "info" "Final step: Flushing rewrite rules..."
php -r "include_once './wordpress/wp-load.php'; flush_rewrite_rules();"
print_message "success" "✓ Rewrite rules flushed"

# Summary
print_message "info" ""
print_message "info" "=== Deployment Summary ==="
print_message "info" "1. Database table checked and fixed (if needed)"
print_message "info" "2. Certificate directory permissions fixed"
print_message "info" "3. Cache cleared"
print_message "info" "4. Plugin status checked"
print_message "info" "5. Rewrite rules flushed"
print_message "info" ""
print_message "success" "Certificate fixes deployed successfully. Please test the functionality."