#!/bin/bash

echo "=== PLESK DIAGNOSTIC SCRIPT ==="
echo ""

# Source libraries
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/lib/common-functions.sh"
source "$SCRIPT_DIR/lib/system-detect.sh"
source "$SCRIPT_DIR/lib/domain-discovery.sh"
source "$SCRIPT_DIR/lib/user-manager.sh"

echo "1. System Detection:"
echo "   Control Panel: $SYS_CONTROL_PANEL"
echo "   OS: $SYS_OS_TYPE $SYS_OS_VERSION"
echo ""

echo "2. Testing list_all_users():"
users=$(list_all_users)
user_count=$(echo "$users" | grep -v "^$" | wc -l)
echo "   Found $user_count users"
echo "   Users: $users"
echo ""

echo "3. Testing list_all_domains():"
domains=$(list_all_domains)
domain_count=$(echo "$domains" | grep -v "^$" | wc -l)
echo "   Found $domain_count domains"
echo "   Domains: $domains"
echo ""

echo "4. Check if plesk command exists:"
which plesk
echo ""

echo "5. Check if plesk bin user --list works:"
/usr/local/psa/bin/user --list 2>&1 || echo "FAILED"
echo ""

echo "6. Check if plesk bin site --list works:"
/usr/local/psa/bin/site --list 2>&1 || echo "FAILED"
echo ""

echo "7. Check plesk-helpers.sh sourced:"
type plesk_list_domains 2>&1 || echo "plesk_list_domains NOT FOUND"
type plesk_list_users 2>&1 || echo "plesk_list_users NOT FOUND"
echo ""

echo "8. Check /var/www/vhosts directory:"
ls -la /var/www/vhosts/ 2>&1 | head -20
echo ""

echo "=== END DIAGNOSTIC ==="
