\n";
    }
}
// Start diagnostics
diagnostics_log('Starting Zoho CRM diagnostics');
// Check for required files
$required_files = array(
    'class-zoho-crm-auth.php' => dirname(__FILE__) . '/class-zoho-crm-auth.php',
    'zoho-config.php' => dirname(__FILE__) . '/zoho-config.php',
);
$missing_files = array();
foreach ($required_files as $name => $path) {
    if (!file_exists($path)) {
        $missing_files[] = $name;
        diagnostics_log("Missing required file: $name", 'ERROR');
    } else {
        diagnostics_log("Found required file: $name");
    }
}
if (!empty($missing_files)) {
    diagnostics_log('Diagnostics failed due to missing files', 'ERROR');
    die('Missing required files: ' . implode(', ', $missing_files));
}
// Check for config constants
require_once $required_files['zoho-config.php'];
$required_constants = array(
    'ZOHO_CLIENT_ID',
    'ZOHO_CLIENT_SECRET',
    'ZOHO_REFRESH_TOKEN',
    'ZOHO_ACCOUNTS_URL',
    'ZOHO_API_BASE_URL',
);
$missing_constants = array();
$empty_constants = array();
foreach ($required_constants as $constant) {
    if (!defined($constant)) {
        $missing_constants[] = $constant;
        diagnostics_log("Missing required constant: $constant", 'ERROR');
    } else {
        $value = constant($constant);
        if (empty($value)) {
            $empty_constants[] = $constant;
            diagnostics_log("Constant is empty: $constant", 'WARNING');
        } else {
            // Mask the actual value for security
            $masked_value = $constant === 'ZOHO_CLIENT_ID' ? substr($value, 0, 4) . '...' : '[MASKED]';
            diagnostics_log("Found constant: $constant = $masked_value");
        }
    }
}
if (!empty($missing_constants)) {
    diagnostics_log('Diagnostics found missing constants', 'ERROR');
    echo 'Missing required constants: ' . implode(', ', $missing_constants) . "
\n";
}
if (!empty($empty_constants)) {
    diagnostics_log('Diagnostics found empty constants', 'WARNING');
    echo 'Empty constants: ' . implode(', ', $empty_constants) . "
\n";
}
// Initialize Zoho CRM Auth
require_once $required_files['class-zoho-crm-auth.php'];
$auth = new HVAC_Zoho_CRM_Auth();
// Check the configuration status
$config_status = $auth->get_configuration_status();
diagnostics_log('Configuration status: ' . json_encode($config_status));
foreach ($config_status as $key => $value) {
    $status = $value ? 'OK' : 'FAIL';
    $type = $value ? 'INFO' : 'ERROR';
    diagnostics_log("$key: $status", $type);
    
    echo "$key: " . ($value ? '✅' : '❌') . "
\n";
}
// Test getting an access token
try {
    diagnostics_log('Testing access token retrieval');
    $access_token = $auth->get_access_token();
    
    if ($access_token) {
        diagnostics_log('Successfully retrieved access token');
        echo "Access token retrieval: ✅
\n";
    } else {
        diagnostics_log('Failed to retrieve access token', 'ERROR');
        echo "Access token retrieval: ❌
\n";
    }
} catch (Exception $e) {
    diagnostics_log('Exception while retrieving access token: ' . $e->getMessage(), 'ERROR');
    echo "Access token retrieval exception: " . $e->getMessage() . "
\n";
}
// Test API connection
try {
    diagnostics_log('Testing API connection');
    $response = $auth->make_api_request('/settings/modules', 'GET');
    
    if (is_wp_error($response)) {
        diagnostics_log('API connection failed: ' . $response->get_error_message(), 'ERROR');
        echo "API connection: ❌ - " . $response->get_error_message() . "
\n";
    } else if (isset($response['modules'])) {
        $module_count = count($response['modules']);
        diagnostics_log("API connection successful. Found $module_count modules.");
        echo "API connection: ✅ - Found $module_count modules
\n";
        
        // List first few modules
        echo "Available Modules:
\n";
        echo "
" . json_encode($response, JSON_PRETTY_PRINT) . "\n"; } } catch (Exception $e) { diagnostics_log('Exception while testing API connection: ' . $e->getMessage(), 'ERROR'); echo "API connection exception: " . $e->getMessage() . "
Diagnostics completed. Check the log file for more details: " . ZOHO_LOG_FILE . "
\n"; // Include a simple CSS for better presentation echo "\n";