<?php
// admin/test_debug.php - File untuk debugging

echo "<h2>Debugging Step by Step</h2>";

// Step 1: Test config.php
echo "<h4>Step 1: Testing config.php</h4>";
if (file_exists('config.php')) {
    echo "✅ config.php ditemukan<br>";
    require_once 'config.php';
    echo "✅ config.php berhasil di-load<br>";
} else {
    echo "❌ config.php TIDAK ditemukan!<br>";
    die();
}

// Step 2: Test email_config.php
echo "<h4>Step 2: Testing email_config.php</h4>";
if (file_exists('email_config.php')) {
    echo "✅ email_config.php ditemukan<br>";
    require_once 'email_config.php';
    echo "✅ email_config.php berhasil di-load<br>";
} else {
    echo "❌ email_config.php TIDAK ditemukan!<br>";
    die();
}

// Step 3: Test PHPMailer
echo "<h4>Step 3: Testing PHPMailer</h4>";
$phpmailer_path = $_SERVER['DOCUMENT_ROOT'] . '/phpmailer/src/PHPMailer.php';
if (file_exists($phpmailer_path)) {
    echo "✅ PHPMailer.php ditemukan di: " . $phpmailer_path . "<br>";
    require_once $phpmailer_path;
    echo "✅ PHPMailer.php berhasil di-load<br>";
} else {
    echo "❌ PHPMailer.php TIDAK ditemukan di: " . $phpmailer_path . "<br>";
    die();
}

// Step 4: Test database connection
echo "<h4>Step 4: Testing Database Connection</h4>";
try {
    $db = new PDO('mysql:host=localhost;dbname=erniinrk_solo', 'erniinrk_solo', 'Kelentit123@');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "✅ Koneksi database berhasil<br>";
} catch (PDOException $e) {
    echo "❌ Koneksi database GAGAL: " . $e->getMessage() . "<br>";
    die();
}

// Step 5: Test table email_messages
echo "<h4>Step 5: Testing Table email_messages</h4>";
try {
    $stmt = $db->query("SHOW TABLES LIKE 'email_messages'");
    if ($stmt->rowCount() > 0) {
        echo "✅ Tabel email_messages ditemukan<br>";
    } else {
        echo "❌ Tabel email_messages TIDAK ditemukan!<br>";
        echo "Silahkan buat tabel dengan SQL:<br>";
        echo "<pre>
CREATE TABLE IF NOT EXISTS `email_messages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `message_id` varchar(255) DEFAULT NULL,
  `subject` varchar(255) DEFAULT NULL,
  `sender` varchar(255) NOT NULL,
  `recipient` varchar(255) NOT NULL,
  `message` text NOT NULL,
  `html_message` text,
  `status` enum('unread','read','replied','archived') DEFAULT 'unread',
  `is_incoming` tinyint(1) DEFAULT 1,
  `reply_to` int(11) DEFAULT NULL,
  `attachments` text,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
        </pre>";
    }
} catch (PDOException $e) {
    echo "❌ Error: " . $e->getMessage() . "<br>";
}

// Step 6: Test IMAP Extension
echo "<h4>Step 6: Testing IMAP Extension</h4>";
if (function_exists('imap_open')) {
    echo "✅ IMAP extension aktif<br>";
} else {
    echo "❌ IMAP extension TIDAK aktif!<br>";
    echo "Silahkan aktifkan IMAP extension di cPanel > Select PHP Version > Extension<br>";
}

echo "<h4>Step 7: Test Session</h4>";
session_start();
if (isset($_SESSION['username'])) {
    echo "✅ Session aktif, username: " . $_SESSION['username'] . "<br>";
} else {
    echo "❌ Session TIDAK aktif!<br>";
}

echo "<hr>";
echo "<h3>✅ Debug selesai! Jika semua langkah berhasil, coba akses email.php lagi.</h3>";
?>