<?php
/**
 * Manual Rebranding Tool - Admin Page
 * Allows manual brand name changes across the system
 */

$page_title = 'Rebranding Tool';
require_once 'includes/header.php';
require_once 'includes/branding.php';

$message = '';
$error = '';
$result = null;
$current_brand = getBrandName();

// Handle rebranding
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['rebrand'])) {
    $old_brand = trim($_POST['old_brand']);
    $new_brand = strip_tags(trim($_POST['new_brand']));
    
    if (empty($old_brand) || empty($new_brand)) {
        $error = 'Both old and new brand names are required.';
    } elseif ($old_brand === $new_brand) {
        $error = 'Old and new brand names must be different.';
    } else {
        // Perform rebranding
        $result = updateBrandAcrossFiles($new_brand, [$old_brand]);
        
        if ($result['success']) {
            $message = 'Rebranding completed successfully! Updated ' . count($result['updated']) . ' file(s).';
            
            // Update the database settings
            try {
                $pdo = getDBConnection();
                $stmt = $pdo->prepare("INSERT INTO settings (key_name, key_value) VALUES ('site_name', ?) ON DUPLICATE KEY UPDATE key_value = VALUES(key_value)");
                $stmt->execute([$new_brand]);
                
                // Update company name
                $stmt = $pdo->prepare("UPDATE company_info SET company_name = ? WHERE id = 1");
                $stmt->execute([$new_brand . ' Car Rental Services']);
                
                $current_brand = $new_brand;
            } catch (PDOException $e) {
                $error .= ' Database update failed: ' . $e->getMessage();
            }
        } else {
            $error = 'Rebranding completed with errors. Please check the details below.';
        }
    }
}
?>

<div class="flex h-screen overflow-hidden">
    <?php include 'includes/sidebar.php'; ?>
    
    <div class="flex-1 flex flex-col overflow-hidden">
        <header class="sticky top-0 z-30 flex items-center bg-white/80 dark:bg-background-dark/80 backdrop-blur-md p-4 justify-between border-b border-slate-200 dark:border-slate-800 lg:px-8">
            <h2 class="text-slate-900 dark:text-white text-clamp-lg font-bold">Rebranding Tool</h2>
        </header>

        <main class="flex-1 overflow-y-auto p-4 lg:p-8 pb-24 admin-content">
            <?php if ($message): ?>
                <div class="bg-emerald-100 dark:bg-emerald-900/30 border border-emerald-400 text-emerald-700 dark:text-emerald-300 px-4 py-3 rounded-xl mb-6">
                    <?= htmlspecialchars($message) ?>
                </div>
            <?php endif; ?>

            <?php if ($error): ?>
                <div class="bg-red-100 dark:bg-red-900/30 border border-red-400 text-red-700 dark:text-red-300 px-4 py-3 rounded-xl mb-6">
                    <?= htmlspecialchars($error) ?>
                </div>
            <?php endif; ?>

            <?php if ($result): ?>
                <div class="bg-blue-50 dark:bg-blue-900/30 border border-blue-400 dark:border-blue-600 rounded-xl mb-6 p-4">
                    <h4 class="font-bold text-blue-800 dark:text-blue-300 mb-2">Rebranding Details</h4>
                    
                    <?php if (!empty($result['updated'])): ?>
                        <div class="mb-3">
                            <p class="text-sm text-blue-700 dark:text-blue-400 font-medium mb-2">Files Updated (<?= count($result['updated']) ?>):</p>
                            <ul class="text-sm text-blue-600 dark:text-blue-400 list-disc list-inside space-y-1">
                                <?php foreach ($result['updated'] as $file): ?>
                                    <li><?= htmlspecialchars($file) ?></li>
                                <?php endforeach; ?>
                            </ul>
                        </div>
                    <?php endif; ?>
                    
                    <?php if (!empty($result['errors'])): ?>
                        <div>
                            <p class="text-sm text-red-700 dark:text-red-400 font-medium mb-2">Errors:</p>
                            <ul class="text-sm text-red-600 dark:text-red-400 list-disc list-inside space-y-1">
                                <?php foreach ($result['errors'] as $err): ?>
                                    <li><?= htmlspecialchars($err) ?></li>
                                <?php endforeach; ?>
                            </ul>
                        </div>
                    <?php endif; ?>
                </div>
            <?php endif; ?>

            <!-- Info Card -->
            <div class="bg-gradient-to-r from-blue-50 to-indigo-50 dark:from-blue-900/20 dark:to-indigo-900/20 border border-blue-200 dark:border-blue-800 rounded-xl p-6 mb-6">
                <div class="flex items-start gap-4">
                    <div class="flex-shrink-0">
                        <span class="material-symbols-outlined text-4xl text-blue-600 dark:text-blue-400">palette</span>
                    </div>
                    <div class="flex-1">
                        <h4 class="font-bold text-blue-900 dark:text-blue-200 text-clamp-lg mb-2">Manual Rebranding Tool</h4>
                        <p class="text-blue-800 dark:text-blue-300 text-sm mb-3">
                            Use this tool to manually change your brand name across all files in the system. This will update all occurrences in user pages, admin pages, and configuration files.
                        </p>
                        <div class="bg-white/50 dark:bg-slate-900/50 rounded-lg p-3">
                            <p class="text-sm text-blue-700 dark:text-blue-400">
                                <strong>Current Brand:</strong> <span class="font-mono text-blue-900 dark:text-blue-200"><?= htmlspecialchars($current_brand) ?></span>
                            </p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Rebranding Form -->
            <div class="bg-white dark:bg-surface-dark rounded-2xl border border-slate-200 dark:border-slate-800 p-6 shadow-sm max-w-2xl">
                <h3 class="text-clamp-lg font-bold mb-6">Rebrand Your System</h3>
                
                <form method="POST" class="space-y-6">
                    <div>
                        <label class="block text-xs font-bold text-slate-500 uppercase mb-1">Current Brand Name</label>
                        <input 
                            type="text" 
                            name="old_brand" 
                            value="<?= htmlspecialchars($current_brand) ?>" 
                            class="w-full px-4 py-3 bg-slate-50 dark:bg-slate-900 border border-slate-200 dark:border-slate-800 rounded-xl focus:ring-2 focus:ring-primary"
                            required
                        >
                        <p class="text-xs text-slate-500 mt-1">This is the brand name that will be replaced</p>
                    </div>
                    
                    <div>
                        <label class="block text-xs font-bold text-slate-500 uppercase mb-1">New Brand Name</label>
                        <input 
                            type="text" 
                            name="new_brand" 
                            placeholder="e.g., You Car Drive" 
                            class="w-full px-4 py-3 bg-slate-50 dark:bg-slate-900 border border-slate-200 dark:border-slate-800 rounded-xl focus:ring-2 focus:ring-primary"
                            required
                        >
                        <p class="text-xs text-slate-500 mt-1">Enter the new brand name to use throughout the system</p>
                    </div>

                    <div class="bg-amber-50 dark:bg-amber-900/20 border border-amber-200 dark:border-amber-800 rounded-lg p-4">
                        <div class="flex gap-3">
                            <span class="material-symbols-outlined text-amber-600 dark:text-amber-400">warning</span>
                            <div>
                                <p class="text-sm text-amber-800 dark:text-amber-300 font-medium mb-1">Important Notes:</p>
                                <ul class="text-xs text-amber-700 dark:text-amber-400 list-disc list-inside space-y-1">
                                    <li>This will modify multiple files across your system</li>
                                    <li>Make sure to backup your files before proceeding</li>
                                    <li>The change is case-sensitive</li>
                                    <li>Files that can't be updated will be listed in the errors section</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    
                    <button 
                        type="submit" 
                        name="rebrand" 
                        class="w-full px-6 py-4 bg-primary text-white rounded-xl font-bold shadow-lg shadow-primary/30 hover:bg-primary/90 transition-colors flex items-center justify-center gap-2"
                    >
                        <span class="material-symbols-outlined">auto_fix_high</span>
                        <span>Rebrand System</span>
                    </button>
                </form>
            </div>

            <!-- Files That Will Be Updated -->
            <div class="mt-6 bg-white dark:bg-surface-dark rounded-2xl border border-slate-200 dark:border-slate-800 p-6 shadow-sm max-w-2xl">
                <h3 class="text-clamp-lg font-bold mb-4">Files That Will Be Updated</h3>
                <ul class="text-sm text-slate-600 dark:text-slate-400 space-y-2">
                    <li class="flex items-center gap-2">
                        <span class="material-symbols-outlined text-primary text-sm">description</span>
                        <span>user/index.html</span>
                    </li>
                    <li class="flex items-center gap-2">
                        <span class="material-symbols-outlined text-primary text-sm">description</span>
                        <span>user/booking_details.html</span>
                    </li>
                    <li class="flex items-center gap-2">
                        <span class="material-symbols-outlined text-primary text-sm">description</span>
                        <span>user/user_authentication.html</span>
                    </li>
                    <li class="flex items-center gap-2">
                        <span class="material-symbols-outlined text-primary text-sm">code</span>
                        <span>user/browse_cars.php</span>
                    </li>
                    <li class="flex items-center gap-2">
                        <span class="material-symbols-outlined text-primary text-sm">code</span>
                        <span>user/favorites.php</span>
                    </li>
                    <li class="flex items-center gap-2">
                        <span class="material-symbols-outlined text-primary text-sm">code</span>
                        <span>user/vehicle_details.php</span>
                    </li>
                    <li class="flex items-center gap-2">
                        <span class="material-symbols-outlined text-primary text-sm">code</span>
                        <span>admin/install.php</span>
                    </li>
                </ul>
            </div>
        </main>
    </div>
</div>

<?php include 'includes/footer.php'; ?>
