<?php
// test-cors.php - Place this in your Laravel public folder

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');

// Get the requested file path
$requestedPath = $_GET['file'] ?? '';

if (empty($requestedPath)) {
    echo "Usage: test-cors.php?file=products/image.jpg";
    exit;
}

// Build the full path to the storage file
$storagePath = __DIR__ . '/../storage/app/public/' . $requestedPath;

// Debug info
echo "<h2>Debug Information</h2>";
echo "<p><strong>Requested file:</strong> " . htmlspecialchars($requestedPath) . "</p>";
echo "<p><strong>Full path:</strong> " . htmlspecialchars($storagePath) . "</p>";
echo "<p><strong>File exists:</strong> " . (file_exists($storagePath) ? 'YES ✓' : 'NO ✗') . "</p>";

if (file_exists($storagePath)) {
    echo "<p><strong>File size:</strong> " . filesize($storagePath) . " bytes</p>";
    echo "<p><strong>MIME type:</strong> " . mime_content_type($storagePath) . "</p>";
    
    // Display the image
    $mimeType = mime_content_type($storagePath);
    $imageData = base64_encode(file_get_contents($storagePath));
    echo "<h3>Image Preview:</h3>";
    echo "<img src='data:$mimeType;base64,$imageData' style='max-width: 500px;' />";
} else {
    // List what's actually in the storage directory
    $productsDir = __DIR__ . '/../storage/app/public/products';
    echo "<h3>Files in storage/app/public/products:</h3>";
    if (is_dir($productsDir)) {
        $files = scandir($productsDir);
        echo "<ul>";
        foreach ($files as $file) {
            if ($file != '.' && $file != '..') {
                echo "<li>" . htmlspecialchars($file) . "</li>";
            }
        }
        echo "</ul>";
    } else {
        echo "<p>Directory does not exist!</p>";
    }
}

// Check if symbolic link exists
$publicStorageLink = __DIR__ . '/storage';
echo "<h3>Symbolic Link Check:</h3>";
echo "<p><strong>Public storage link exists:</strong> " . (file_exists($publicStorageLink) ? 'YES ✓' : 'NO ✗') . "</p>";

if (is_link($publicStorageLink)) {
    echo "<p><strong>Link target:</strong> " . readlink($publicStorageLink) . "</p>";
}