<?php
/**
 * Neptuno ERP - Permission Fixer
 * Recursively fixes file/directory permissions across the entire project.
 * Required because ZIP extraction on shared hosting often strips read permissions.
 */

set_time_limit(600);
ini_set('display_errors', 1);
error_reporting(E_ALL);

echo '<!DOCTYPE html><html><head><meta charset="utf-8"><title>Fixing Permissions</title></head>';
echo '<body style="background:#0f172a;color:#e2e8f0;font-family:monospace;padding:40px;">';
echo '<h1 style="color:#38bdf8;">🔧 Neptuno — Permission Fixer</h1>';

$root = __DIR__;
$dirCount = 0;
$fileCount = 0;
$errorCount = 0;

function fixPermsRecursive($path, &$dirCount, &$fileCount, &$errorCount) {
    $items = @scandir($path);
    if ($items === false) {
        $errorCount++;
        return;
    }

    foreach ($items as $item) {
        if ($item === '.' || $item === '..') continue;
        $full = $path . '/' . $item;

        if (is_dir($full)) {
            if (@chmod($full, 0755)) {
                $dirCount++;
            } else {
                $errorCount++;
            }
            fixPermsRecursive($full, $dirCount, $fileCount, $errorCount);
        } else {
            if (@chmod($full, 0644)) {
                $fileCount++;
            } else {
                $errorCount++;
            }
        }
    }
}

echo '<p style="color:#f59e0b;">⏳ Scanning and fixing permissions recursively... This may take a minute.</p>';
ob_flush(); flush();

// Fix the entire project
fixPermsRecursive($root, $dirCount, $fileCount, $errorCount);

// Make storage and bootstrap/cache extra writable
$writableDirs = [
    $root . '/storage',
    $root . '/storage/app',
    $root . '/storage/app/public',
    $root . '/storage/framework',
    $root . '/storage/framework/cache',
    $root . '/storage/framework/cache/data',
    $root . '/storage/framework/sessions',
    $root . '/storage/framework/views',
    $root . '/storage/logs',
    $root . '/bootstrap/cache',
];

foreach ($writableDirs as $dir) {
    if (is_dir($dir)) {
        @chmod($dir, 0775);
    }
}

echo '<hr style="border-color:#334155;">';

$color = $errorCount === 0 ? '#10b981' : '#f59e0b';
echo "<p style='color:#10b981;'>✅ Directories fixed: <strong>{$dirCount}</strong></p>";
echo "<p style='color:#10b981;'>✅ Files fixed: <strong>{$fileCount}</strong></p>";

if ($errorCount > 0) {
    echo "<p style='color:#ef4444;'>❌ Errors: <strong>{$errorCount}</strong> (some files may be owned by root)</p>";
} else {
    echo "<p style='color:#10b981;'>✅ Zero errors!</p>";
}

// Quick verification test
echo '<hr style="border-color:#334155;">';
echo '<h3 style="color:#f59e0b;">Quick Boot Verification</h3>';

$testFile = $root . '/vendor/symfony/deprecation-contracts/function.php';
if (file_exists($testFile) && is_readable($testFile)) {
    echo "<p style='color:#10b981;'>✅ vendor/symfony/deprecation-contracts/function.php — NOW READABLE</p>";
} else {
    echo "<p style='color:#ef4444;'>❌ Still not readable. The hosting may require a support ticket to fix ownership.</p>";
}

try {
    require $root . '/vendor/autoload.php';
    echo "<p style='color:#10b981;'>✅ Composer autoload — LOADED SUCCESSFULLY</p>";
    
    $app = require_once $root . '/bootstrap/app.php';
    echo "<p style='color:#10b981;'>✅ Laravel Bootstrap — APPLICATION CREATED</p>";

    $kernel = $app->make(\Illuminate\Contracts\Http\Kernel::class);
    echo "<p style='color:#10b981;'>✅ HTTP Kernel — RESOLVED</p>";

    echo '<hr style="border-color:#334155;">';
    echo "<p style='color:#10b981;font-size:18px;'>🎉 <strong>SUCCESS!</strong> Laravel is now bootable.</p>";
    echo "<p style='color:#38bdf8;'>➡️ Visit <a href='public/' style='color:#38bdf8;'>neptuno.app.br/public/</a> to access your ERP.</p>";
    echo "<p style='color:#ef4444;'>⚠️ DELETE this file and diagnose.php after confirming the system works!</p>";
} catch (\Throwable $e) {
    echo "<p style='color:#ef4444;'>❌ Boot still failing: " . htmlspecialchars($e->getMessage()) . "</p>";
}

echo '</body></html>';
?>
