```php
<?php
ob_start(); // Start output buffering to prevent stray output
header('Content-Type: application/json');

session_start();
include 'config.php';
require 'vendor/autoload.php'; 

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Enable error reporting for debugging
ini_set('display_errors', 0);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
ini_set('log_errors', 1);
ini_set('error_log', 'php_errors.log');

// Check if user is logged in
if (!isset($_SESSION['user_id'], $_SESSION['role'])) {
    error_log("Unauthorized access to update_employee_acknowledgment.php. Session data: " . print_r($_SESSION, true));
    echo json_encode(['success' => false, 'error' => 'Unauthorized access. Please log in.']);
    exit();
}

$employee_id = (int)$_SESSION['user_id'];
$username = $_POST['username'] ?? $_SESSION['username'] ?? 'Unknown User';
$email = $_POST['email'] ?? null;
$document_id = isset($_POST['document_id']) && is_numeric($_POST['document_id']) ? (int)$_POST['document_id'] : null;
$action = $_POST['action'] ?? null;

if ($action !== 'acknowledge' || !$document_id || !$email) {
    error_log("Invalid request parameters: action=$action, document_id=$document_id, email=$email");
    echo json_encode(['success' => false, 'error' => 'Invalid request parameters.']);
    exit();
}

// Validate database connection
if ($conn->connect_error) {
    error_log("Database connection failed: " . $conn->connect_error);
    echo json_encode(['success' => false, 'error' => 'Database connection failed.']);
    exit();
}

// Check document_status table existence
$result = $conn->query("SHOW TABLES LIKE 'document_status'");
if (!$result || $result->num_rows === 0) {
    error_log("document_status table does not exist.");
    echo json_encode(['success' => false, 'error' => 'Document status tracking is not configured.']);
    exit();
}

// Check if document exists
$doc_sql = "SELECT title FROM documents WHERE id = ?";
$doc_stmt = $conn->prepare($doc_sql);
if (!$doc_stmt) {
    error_log("Document check query preparation failed: " . $conn->error);
    echo json_encode(['success' => false, 'error' => 'Failed to verify document.']);
    exit();
}
$doc_stmt->bind_param("i", $document_id);
$doc_stmt->execute();
$doc_result = $doc_stmt->get_result();
if ($doc_result->num_rows === 0) {
    error_log("Document not found: ID=$document_id");
    echo json_encode(['success' => false, 'error' => 'Document not found.']);
    exit();
}
$doc_stmt->close();

// Update document status to 'Acknowledged'
try {
    $update_sql = "INSERT INTO document_status (employee_id, document_id, status) 
                   VALUES (?, ?, 'Acknowledged') 
                   ON DUPLICATE KEY UPDATE status = 'Acknowledged'";
    $stmt = $conn->prepare($update_sql);
    if (!$stmt) {
        error_log("Status update query preparation failed: " . $conn->error . ", Query: $update_sql");
        echo json_encode(['success' => false, 'error' => 'Failed to prepare status update query.']);
        exit();
    }

    $stmt->bind_param("ii", $employee_id, $document_id);
    if (!$stmt->execute()) {
        error_log("Status update query execution failed: " . $stmt->error);
        echo json_encode(['success' => false, 'error' => 'Failed to update document status: ' . $stmt->error]);
        exit();
    }
    $stmt->close();
    error_log("Document status updated to Acknowledged for employee_id: $employee_id, document_id: $document_id");
} catch (Exception $e) {
    error_log("Exception in document status update: " . $e->getMessage());
    echo json_encode(['success' => false, 'error' => 'Error updating document status: ' . $e->getMessage()]);
    exit();
}

// Send email with PHPMailer
$mail = new PHPMailer(true);
try {
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'prabhakaran.cirs@gmail.com';
    $mail->Password = 'rvlgkmpmyugdswqp';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = 587;
    $mail->CharSet = 'UTF-8';
    $mail->Encoding = 'base64';

    $mail->setFrom('prabhakaran.cirs@gmail.com', 'Your Name');
    $mail->addAddress($email, $username);
    
    $mail->Subject = 'Document Acknowledgment';
    $mail->Body = 'You have acknowledged the company policies document.';
    $mail->IsHTML(true);

    // Generate PDF file
    $documentPath = "generated_docs/document_{$document_id}.pdf";
    $command = "php generate_pdf.php $document_id $employee_id " . escapeshellarg($documentPath);
    error_log("Executing PDF generation command: $command");
    exec($command, $output, $return_var);
    if ($return_var !== 0) {
        error_log("PDF generation failed. Return code: $return_var, Output: " . print_r($output, true));
        throw new Exception("PDF generation failed.");
    }

    // Attach PDF if exists
    if (file_exists($documentPath)) {
        $mail->addAttachment($documentPath);
        error_log("PDF attached: $documentPath");
    } else {
        error_log("PDF file not found: $documentPath");
        throw new Exception("PDF file not found: $documentPath");
    }

    // Send email
    $mail->send();
    error_log("Email sent successfully to $email for document_id: $document_id");
    ob_end_clean();
    echo json_encode(['success' => true, 'message' => 'Document acknowledged and email sent successfully']);
} catch (Exception $e) {
    error_log("Email failed for document_id: $document_id, email: $email, error: " . $e->getMessage());
    ob_end_clean();
    echo json_encode(['success' => false, 'error' => 'Email failed: ' . $e->getMessage()]);
    exit();
}

$conn->close();
ob_end_clean();
?>
```