<?php
// ════════════════════════════════════════════════════════
// EMAIL CONFIGURATION TEST
// Place in: Triangle_Tree_Life_Number/api/test_email.php
// ════════════════════════════════════════════════════════

header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');

$results = [];
$errors = [];

// Test 1: Check if mail function exists
if (function_exists('mail')) {
    $results[] = '✅ PHP mail() function is available';
} else {
    $errors[] = '❌ PHP mail() function NOT available';
}

// Test 2: Check sendmail path
$sendmail_path = ini_get('sendmail_path');
if ($sendmail_path) {
    $results[] = '✅ Sendmail path configured: ' . $sendmail_path;
} else {
    $errors[] = '⚠️ Sendmail path not configured';
}

// Test 3: Check SMTP settings from php.ini
$smtp_host = ini_get('SMTP');
$smtp_port = ini_get('smtp_port');

if ($smtp_host && $smtp_host !== '') {
    $results[] = '✅ SMTP Host: ' . $smtp_host . ':' . $smtp_port;
} else {
    $errors[] = '⚠️ SMTP not configured in php.ini (common on Linux)';
}

// Test 4: Try to send a test email
$test_email = 'joelim8278@gmail.com'; // Your email
$subject = 'Numerology Test Email';
$message = '
<!DOCTYPE html>
<html>
<body style="font-family: Arial, sans-serif; padding: 20px;">
    <h2 style="color: #2563a8;">Email Test Successful!</h2>
    <p>This is a test email from your Numerology system.</p>
    <p>If you received this, your email configuration is working correctly.</p>
    <p><strong>Test Time:</strong> ' . date('Y-m-d H:i:s') . '</p>
</body>
</html>
';

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: Numerology System <noreply@decode.com.my>" . "\r\n";

$mail_sent = @mail($test_email, $subject, $message, $headers);

if ($mail_sent) {
    $results[] = '✅ Test email sent successfully to ' . $test_email;
    $results[] = '📧 Check your inbox (and spam folder) in 1-2 minutes';
} else {
    $errors[] = '❌ Failed to send test email';
    $error = error_get_last();
    if ($error) {
        $errors[] = '❌ Last error: ' . $error['message'];
    }
}

// Test 5: Check common email issues
$hostname = gethostname();
$results[] = 'ℹ️ Server hostname: ' . $hostname;

// Check if running on localhost
if (strpos($hostname, 'localhost') !== false || strpos($_SERVER['SERVER_NAME'], 'localhost') !== false) {
    $errors[] = '⚠️ Running on localhost - mail() typically does not work on local dev environments';
}

// Output results
echo json_encode([
    'success' => count($errors) === 0 || $mail_sent,
    'results' => $results,
    'warnings' => $errors,
    'recommendation' => count($errors) > 0 ? 
        'Email may not work reliably. Consider using SMTP service (SendGrid, AWS SES, or Mailgun) for production.' : 
        'Email configuration looks good. Check spam folder if emails not arriving.',
    'server_info' => [
        'php_version' => phpversion(),
        'server_software' => $_SERVER['SERVER_SOFTWARE'] ?? 'Unknown',
        'hostname' => $hostname
    ]
], JSON_PRETTY_PRINT);
?>
