<?php
// CoreFlux/core/CheckHtaccess.php

namespace Core; // IMPORTANT: Add a namespace for autoloading

class CheckHtaccess {
    private $initialized = false;

    public function init() {
        if (!$this->initialized) {
            $this->initialized = true;
            echo "<p style='color: blue;'>✅ <code>CheckHtaccess</code> initialized.</p>";
        }
    }

    // This method remains largely the same
    public function updateHtaccessFile(string $filePath, string $correctRewriteBase, string $fileType): bool {
        if (!$this->initialized) {
            echo "<p style='color: red;'>❌ Error: <code>CheckHtaccess</code> not initialized. Call <code>init()</code> first.</p>";
            return false;
        }
        echo "<p>Checking <code>" . htmlspecialchars($fileType) . "</code> .htaccess at: <code>" . htmlspecialchars($filePath) . "</code></p>";

        if (!file_exists($filePath)) {
            echo "<p style='color: orange;'>⚠️ Warning: <code>" . htmlspecialchars($fileType) . "</code> .htaccess file not found. Please create it first.</p>";
            return false;
        }

        $content = file_get_contents($filePath);
        if ($content === false) {
            echo "<p style='color: red;'>❌ Error: Could not read <code>" . htmlspecialchars($fileType) . "</code> .htaccess. Check file permissions.</p>";
            return false;
        }

        // Regex to find the RewriteBase line
        $pattern = '/^(\s*RewriteBase\s+).*$/m';
        $replacement = '${1}' . preg_quote($correctRewriteBase, '/');

        if (preg_match($pattern, $content)) {
            $newContent = preg_replace($pattern, $replacement, $content);
            if ($newContent === null) {
                echo "<p style='color: red;'>❌ Error: Regex replacement failed for <code>" . htmlspecialchars($fileType) . "</code> .htaccess.</p>";
                return false;
            }

            if ($newContent !== $content) {
                if (file_put_contents($filePath, $newContent) !== false) {
                    echo "<p style='color: green;'>✅ Success: <code>RewriteBase</code> in <code>" . htmlspecialchars($fileType) . "</code> .htaccess updated to: <code>" . htmlspecialchars($correctRewriteBase) . "</code></p>";
                    return true;
                } else {
                    echo "<p style='color: red;'>❌ Error: Could not write to <code>" . htmlspecialchars($fileType) . "</code> .htaccess. Check file permissions.</p>";
                    return false;
                }
            } else {
                echo "<p style='color: blue;'>✔️ OK: <code>RewriteBase</code> in <code>" . htmlspecialchars($fileType) . "</code> .htaccess is already correct: <code>" . htmlspecialchars($correctRewriteBase) . "</code></p>";
                return true;
            }
        } else {
            // RewriteBase line does not exist, attempt to insert it after RewriteEngine On
            $patternInsert = '/^(\s*RewriteEngine\s+On\s*)$/mi'; // Case-insensitive 'On'
            $insertContent = "\nRewriteBase " . $correctRewriteBase . "\n";
            $newContent = preg_replace($patternInsert, '$1' . $insertContent, $content);

            if ($newContent !== null && $newContent !== $content) {
                if (file_put_contents($filePath, $newContent) !== false) {
                    echo "<p style='color: green;'>✅ Success: <code>RewriteBase</code> line added to <code>" . htmlspecialchars($fileType) . "</code> .htaccess: <code>" . htmlspecialchars($correctRewriteBase) . "</code></p>";
                    return true;
                } else {
                    echo "<p style='color: red;'>❌ Error: Could not write to <code>" . htmlspecialchars($fileType) . "</code> .htaccess. Check file permissions.</p>";
                    return false;
                }
            } else {
                echo "<p style='color: orange;'>⚠️ Warning: <code>RewriteBase</code> line not found and could not be automatically inserted after <code>RewriteEngine On</code>. Please add it manually.</p>";
                return false;
            }
        }
    }
}