#!/bin/bash

# Script to verify basic functionality of the staging site
# This script performs HTTP checks rather than SSH commands

# Get absolute path to this script's directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Navigate to wordpress-dev directory
cd "$(dirname "$SCRIPT_DIR")" || exit 1

# Load environment variables
ENV_FILE=".env"
if [ ! -f "$ENV_FILE" ]; then
    echo "Error: .env file not found at: $ENV_FILE"
    exit 1
fi

source "$ENV_FILE"

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

# Function to check if a command was successful
check_status() {
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}✓ $1${NC}"
        return 0
    else
        echo -e "${RED}✗ $1${NC}"
        return 1
    fi
}

# Get the base URL from environment variable
BASE_URL="${UPSKILL_STAGING_URL}"
if [[ $BASE_URL == */ ]]; then
    # Remove trailing slash if present
    BASE_URL="${BASE_URL%/}"
fi

echo "=== Verifying Staging Site Functionality ==="
echo "Base URL: $BASE_URL"
echo "==============================="

# Check if the site is up
echo -e "\n${YELLOW}Checking if the site is up...${NC}"
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$BASE_URL")
if [ "$HTTP_STATUS" = "200" ]; then
    echo -e "${GREEN}✓ Site is up (HTTP $HTTP_STATUS)${NC}"
else
    echo -e "${RED}✗ Site is not responding correctly (HTTP $HTTP_STATUS)${NC}"
fi

# Check login page
echo -e "\n${YELLOW}Checking login page...${NC}"
LOGIN_CHECK=$(curl -s "$BASE_URL/community-login/" | grep -i "login")
if [ -n "$LOGIN_CHECK" ]; then
    echo -e "${GREEN}✓ Login page is accessible${NC}"
else
    echo -e "${RED}✗ Login page not found or not accessible${NC}"
fi

# Check plugin-specific pages
echo -e "\n${YELLOW}Checking for HVAC Community Events pages...${NC}"

# Check redirect from dashboard (should redirect to login since we're not authenticated)
DASHBOARD_REDIRECT=$(curl -s -I "$BASE_URL/hvac-dashboard/" | grep -i location)
if [ -n "$DASHBOARD_REDIRECT" ]; then
    echo -e "${GREEN}✓ Dashboard page correctly redirects to login${NC}"
    echo "   $DASHBOARD_REDIRECT"
else
    echo -e "${RED}✗ Dashboard page doesn't redirect to login (possible plugin issue)${NC}"
fi

# Check for plugin's CSS files
CSS_CHECK=$(curl -s -I "$BASE_URL/wp-content/plugins/hvac-community-events/assets/css/hvac-dashboard.css" | grep -i "200 OK")
if [ -n "$CSS_CHECK" ]; then
    echo -e "${GREEN}✓ Plugin CSS files are accessible${NC}"
else
    echo -e "${RED}✗ Plugin CSS files not found (possible plugin installation issue)${NC}"
fi

# Check for Zoho admin JavaScript file
JS_CHECK=$(curl -s -I "$BASE_URL/wp-content/plugins/hvac-community-events/assets/js/zoho-admin.js" | grep -i "200 OK")
if [ -n "$JS_CHECK" ]; then
    echo -e "${GREEN}✓ Zoho admin JavaScript file is accessible${NC}"
else
    echo -e "${RED}✗ Zoho admin JavaScript file not found (possible plugin installation issue)${NC}"
fi

# Check plugin directory existence
PLUGIN_DIR_CHECK=$(curl -s -I "$BASE_URL/wp-content/plugins/hvac-community-events/" | grep -i "403\|200")
if [ -n "$PLUGIN_DIR_CHECK" ]; then
    echo -e "${GREEN}✓ Plugin directory exists on server${NC}"
else
    echo -e "${RED}✗ Plugin directory not found on server${NC}"
fi

# Summary
echo -e "\n${YELLOW}Verification Summary:${NC}"
echo "1. Main site is accessible: $HTTP_STATUS"
echo "2. Login page is accessible: $([ -n "$LOGIN_CHECK" ] && echo "Yes" || echo "No")"
echo "3. Dashboard redirects to login: $([ -n "$DASHBOARD_REDIRECT" ] && echo "Yes" || echo "No")"
echo "4. Plugin CSS files accessible: $([ -n "$CSS_CHECK" ] && echo "Yes" || echo "No")"
echo "5. Zoho JS files accessible: $([ -n "$JS_CHECK" ] && echo "Yes" || echo "No")"
echo "6. Plugin directory exists: $([ -n "$PLUGIN_DIR_CHECK" ] && echo "Yes" || echo "No")"

echo -e "\n${GREEN}Verification completed!${NC}"