<?php
require_once 'config/db.php';
require_once 'services/MessageService.php';
require_once 'config/constants.php';

echo "<h1>PDF Path Debugger</h1>";

$msgService = new MessageService($conn);

// Get any valid user template
$stmt = $conn->query("SELECT * FROM user_templates LIMIT 1");
$tplData = $stmt->fetch_assoc();

if (!$tplData) {
    die("No user templates found! Upload a template first.");
}

$tplOptions = [
    'pos_x' => $tplData['pos_x'] ?? 50,
    'pos_y' => $tplData['pos_y'] ?? 90,
    'font_size' => $tplData['font_size'] ?? 20,
    'font_color' => $tplData['font_color'] ?? '#000000'
];

$pdfName = 'debug_test_' . time() . '.pdf';
$pdfPathRelative = 'uploads/temp_pdfs/' . $pdfName;
$pdfPathAbsolute = ROOT_PATH . $pdfPathRelative;

echo "<h3>1. Local PDF Generation</h3>";
echo "Attempting to generate PDF...<br>";

// We need to pass the absolute or relative path to MessageService
// MessageService uses dirname() so ROOT_PATH or relative works. Use relative for consistency with UserController
$success = $msgService->generateBirthdayPdf($tplData['image_path'], "Test Name", $pdfPathRelative, $tplOptions);

if ($success && file_exists($pdfPathAbsolute)) {
    echo "<span style='color:green;'>SUCCESS:</span> Local PDF physical file created successfully at:<br>";
    echo "<b>Physical Path:</b> <code>" . realpath($pdfPathAbsolute) . "</code><br>";
    echo "<b>File Size:</b> " . filesize($pdfPathAbsolute) . " bytes<br>";
} else {
    echo "<span style='color:red;'>FAILED:</span> Could not create PDF physical file at <code>$pdfPathRelative</code>!<br>";
    die();
}

echo "<h3>2. Public URL Resolution</h3>";
// Using the hardcoded logic requested for WhatsApp API
$pdfUrl = "http://demo.atomsgroup.in/birthday_msg/" . $pdfPathRelative;
echo "The exact URL sent to WhatsApp API is:<br>";
echo "<b>Public URL:</b> <a href='$pdfUrl' target='_blank'>$pdfUrl</a><br>";

echo "<h3>3. Public URL Accessibility Check (What Msg24 API sees)</h3>";
echo "Attempting to download the file from the public URL...<br>";

$ch = curl_init($pdfUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpCode == 200) {
    echo "<span style='color:green;'>SUCCESS:</span> The public URL is fully accessible! (HTTP 200 OK). The WhatsApp API should have absolutely no issues downloading this file.<br>";
} elseif ($httpCode == 404) {
    echo "<span style='color:red;'>FAILED:</span> The public URL returned 404 Not Found! This means your domain <code>demo.atomsgroup.in/birthday_msg/</code> does not point to the same directory where the script is saving the files.<br>";
} elseif ($httpCode == 403) {
    echo "<span style='color:red;'>FAILED:</span> The public URL returned 403 Forbidden! This means your server settings (like Hotlink Protection or .htaccess) are blocking external APIs from downloading the PDF.<br>";
} else {
    echo "<span style='color:orange;'>WARNING:</span> The public URL returned HTTP Code $httpCode. Check your server configuration.<br>";
}

echo "<br><hr>";
echo "<h3>4. Simulated Final API String</h3>";
echo "If this was an actual WhatsApp Request, the raw background string sent would look like this:<br>";
$msg = urlencode("Test MSG");
$mobile = urlencode("1234567890");
$apikey = urlencode("TEST_KEY");
$waUrl = "http://cloudapi.msg24.in/wapp/api/send?apikey=$apikey&mobile=$mobile&msg=$msg&pdf=" . $pdfUrl;
echo "<code>$waUrl</code><br>";

?>
