import { test, expect } from '@playwright/test';
import { STAGING_URL } from './config/staging-config';

/**
 * Test Zoho CRM connection with enhanced error reporting
 */
test.describe('Zoho CRM Connection Test', () => {
  test('Test Zoho connection with credentials', async ({ page }) => {
    console.log('Testing Zoho CRM connection with deployed credentials');
    
    // Try to access wp-admin first to see if we can get any admin credentials working
    await page.goto(`${STAGING_URL}/wp-admin/`);
    await page.waitForLoadState('networkidle');
    
    // We'll need to test the connection via a direct API call since we don't have admin access
    // Let's test if the Zoho configuration is loading properly by checking for debug output
    
    console.log('Step 1: Testing direct access to Zoho debug information');
    
    // Try to access the Zoho test endpoint directly
    const testEndpoints = [
      `${STAGING_URL}/wp-content/plugins/hvac-community-events/includes/zoho/diagnostics.php`,
      `${STAGING_URL}/wp-content/plugins/hvac-community-events/includes/zoho/test-integration.php`,
      `${STAGING_URL}/?hvac_zoho_test=1`,
      `${STAGING_URL}/wp-json/hvac/v1/zoho/test`
    ];
    
    for (const endpoint of testEndpoints) {
      try {
        console.log(`Testing endpoint: ${endpoint}`);
        const response = await page.request.get(endpoint);
        console.log(`${endpoint}: ${response.status()} ${response.statusText()}`);
        
        if (response.status() === 200) {
          const content = await response.text();
          console.log(`Response content preview: ${content.substring(0, 200)}...`);
          
          if (content.includes('zoho') || content.includes('token') || content.includes('auth')) {
            console.log('✓ Found Zoho-related content in response');
          }
        }
      } catch (error) {
        console.log(`${endpoint}: Error - ${error}`);
      }
    }
    
    console.log('Step 2: Check if WordPress can load environment variables');
    
    // Test if we can create a simple PHP script to test environment loading
    console.log('Zoho connection test completed');
  });
  
  test('Verify enhanced error reporting is working', async ({ page }) => {
    console.log('Step 3: Verify enhanced error reporting is accessible');
    
    // Check if the debug log exists and can be accessed
    try {
      const logResponse = await page.request.get(`${STAGING_URL}/wp-content/plugins/hvac-community-events/logs/zoho-debug.log`);
      console.log(`Debug log access: ${logResponse.status()} ${logResponse.statusText()}`);
      
      if (logResponse.status() === 200) {
        const logContent = await logResponse.text();
        console.log(`Log content length: ${logContent.length} characters`);
        if (logContent.length > 0) {
          console.log('Log content preview:', logContent.substring(0, 300));
        }
      }
    } catch (error) {
      console.log(`Debug log access error: ${error}`);
    }
    
    // Check if enhanced error reporting is visible on the homepage
    await page.goto(STAGING_URL);
    await page.waitForLoadState('networkidle');
    
    const pageContent = await page.content();
    
    // Look for any Zoho-related debug information
    const hasZohoDebug = pageContent.includes('zoho') || pageContent.includes('ZOHO') || 
                        pageContent.includes('no_token') || pageContent.includes('access_token');
    
    console.log(`Homepage contains Zoho debug info: ${hasZohoDebug}`);
    
    if (hasZohoDebug) {
      console.log('✓ Enhanced error reporting is visible');
    } else {
      console.log('✗ No Zoho debug information visible on homepage');
    }
    
    console.log('Enhanced error reporting verification completed');
  });
});