<?php
// ⚠️ DELETE THIS FILE AFTER USE

$target = dirname(__DIR__) . '/public/storage';
$shortcut = dirname(__DIR__) . '/storage/app/public';

echo '<pre style="font-family:monospace;font-size:14px;padding:20px;">';
echo "<b>Storage Link Status & Fix:</b>\n\n";

if (file_exists($target)) {
    if (is_link($target)) {
        echo "ℹ️  Existing symlink found. Checking if it is broken...\n";
        $linkTarget = readlink($target);
        echo "   Symlink points to: $linkTarget\n";
        
        if (!file_exists($linkTarget)) {
            echo "❌ Link target does not exist! Re-creating symlink...\n";
            @unlink($target);
        } else {
            echo "✅ Symlink is working and healthy!\n";
        }
    } else {
        echo "❌ public/storage is a physical directory, NOT a symlink! This blocks file uploads. Deleting it...\n";
        // Helper to recursively delete directory
        function deleteDir($dirPath) {
            if (!is_dir($dirPath)) return;
            $files = array_diff(scandir($dirPath), ['.','..']);
            foreach ($files as $file) {
                (is_dir("$dirPath/$file")) ? deleteDir("$dirPath/$file") : @unlink("$dirPath/$file");
            }
            return @rmdir($dirPath);
        }
        deleteDir($target);
    }
}

if (!file_exists($target)) {
    echo "Creating symbolic link...\n";
    if (symlink($shortcut, $target)) {
        echo "✅ Symbolic link created successfully!\n";
    } else {
        echo "❌ Failed to create symbolic link! Check folder permissions.\n";
    }
}

echo "\n<b style='color:red'>⚠️ DELETE THIS FILE NOW from public/ folder!</b>";
echo '</pre>';
