<?php
// upgrade_to_seller.php - Self-service Buyer to Seller role upgrade
require_once 'db_connect.php';

// Must be logged in
if (!isLoggedIn()) {
    header("Location: login.php?redirect=upgrade_to_seller.php&error=" . urlencode("Please log in to upgrade your account."));
    exit();
}

$user = getLoggedInUser();

// If already seller or admin, redirect to sell.php
if (canSell()) {
    header("Location: sell.php?success=" . urlencode("You already have seller access!"));
    exit();
}

// Handle Upgrade POST
if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'upgrade') {
    $stmt = $pdo->prepare("UPDATE users SET role = 'seller' WHERE id = ? AND role = 'buyer'");
    $stmt->execute([$user['id']]);

    if ($stmt->rowCount() > 0) {
        // Update session role immediately so navbar updates without re-login
        $_SESSION['role'] = 'seller';
        header("Location: sell.php?success=" . urlencode("Congratulations! Your account has been upgraded to Seller. Welcome aboard!"));
        exit();
    } else {
        header("Location: upgrade_to_seller.php?error=" . urlencode("Upgrade failed. Please try again."));
        exit();
    }
}

renderHeader(__('upgrade_page_title'));
?>

<div class="max-w-3xl mx-auto my-10 px-4">

    <div class="text-center mb-10">
        <span class="inline-flex items-center gap-2 px-3.5 py-1 bg-orange-50 text-orange-600 text-xs font-black uppercase tracking-widest rounded-full mb-4 border border-orange-100"><?php echo __('upgrade_badge'); ?></span>
        <h1 class="text-4xl font-extrabold text-gray-900 tracking-tight"><?php echo __('upgrade_heading'); ?></h1>
        <p class="text-gray-500 text-sm mt-3 max-w-md mx-auto"><?php echo __('upgrade_desc'); ?></p>
    </div>

    <div class="bg-amber-50/80 border border-amber-200 rounded-2xl p-4 flex items-center gap-4 mb-8">
        <div class="w-10 h-10 bg-amber-100 text-amber-700 rounded-xl flex items-center justify-center flex-shrink-0">
            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"/></svg>
        </div>
        <div>
            <p class="text-sm font-bold text-amber-900"><?php echo __('upgrade_current'); ?> <strong><?php echo sanitize($user['username']); ?></strong></p>
            <p class="text-xs text-amber-700 mt-0.5"><?php echo __('upgrade_role_from'); ?> <span class="font-black uppercase"><?php echo __('upgrade_role_buyer'); ?></span> → Will be upgraded to <span class="font-black uppercase text-orange-600"><?php echo __('upgrade_role_seller'); ?></span></p>
        </div>
    </div>

    <div class="grid grid-cols-1 sm:grid-cols-2 gap-5 mb-10">
        <div class="bg-white rounded-2xl border border-gray-100 shadow-sm p-6 flex gap-4 hover:shadow-md transition-shadow">
            <div class="w-12 h-12 bg-orange-50 text-orange-600 rounded-xl flex items-center justify-center flex-shrink-0">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"/></svg>
            </div>
            <div>
                <h3 class="font-bold text-gray-900 text-sm mb-1"><?php echo __('upgrade_feat1_title'); ?></h3>
                <p class="text-xs text-gray-500 leading-relaxed"><?php echo __('upgrade_feat1_desc'); ?></p>
            </div>
        </div>
        <div class="bg-white rounded-2xl border border-gray-100 shadow-sm p-6 flex gap-4 hover:shadow-md transition-shadow">
            <div class="w-12 h-12 bg-emerald-50 text-emerald-600 rounded-xl flex items-center justify-center flex-shrink-0">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
            </div>
            <div>
                <h3 class="font-bold text-gray-900 text-sm mb-1"><?php echo __('upgrade_feat2_title'); ?></h3>
                <p class="text-xs text-gray-500 leading-relaxed"><?php echo __('upgrade_feat2_desc'); ?></p>
            </div>
        </div>
        <div class="bg-white rounded-2xl border border-gray-100 shadow-sm p-6 flex gap-4 hover:shadow-md transition-shadow">
            <div class="w-12 h-12 bg-blue-50 text-blue-600 rounded-xl flex items-center justify-center flex-shrink-0">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z"/></svg>
            </div>
            <div>
                <h3 class="font-bold text-gray-900 text-sm mb-1"><?php echo __('upgrade_feat3_title'); ?></h3>
                <p class="text-xs text-gray-500 leading-relaxed"><?php echo __('upgrade_feat3_desc'); ?></p>
            </div>
        </div>
        <div class="bg-white rounded-2xl border border-gray-100 shadow-sm p-6 flex gap-4 hover:shadow-md transition-shadow">
            <div class="w-12 h-12 bg-purple-50 text-purple-600 rounded-xl flex items-center justify-center flex-shrink-0">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 3h2l.4 2M7 13h10l4-8H5.4M7 13L5.4 5M7 13l-2.293 2.293c-.63.63-.184 1.707.707 1.707H17m0 0a2 2 0 100 4 2 2 0 000-4zm-8 2a2 2 0 11-4 0 2 2 0 014 0z"/></svg>
            </div>
            <div>
                <h3 class="font-bold text-gray-900 text-sm mb-1"><?php echo __('upgrade_feat4_title'); ?></h3>
                <p class="text-xs text-gray-500 leading-relaxed"><?php echo __('upgrade_feat4_desc'); ?></p>
            </div>
        </div>
    </div>

    <div class="bg-gradient-to-br from-orange-500 to-rose-500 rounded-3xl p-8 sm:p-10 text-white text-center relative overflow-hidden shadow-xl">
        <div class="relative z-10">
            <div class="w-16 h-16 bg-white/20 text-white rounded-2xl flex items-center justify-center mx-auto mb-4 backdrop-blur-sm">
                <svg class="w-8 h-8" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"/></svg>
            </div>
            <h2 class="text-2xl font-extrabold mb-2"><?php echo __('upgrade_cta_title'); ?></h2>
            <p class="text-sm text-orange-100 mb-8"><?php echo __('upgrade_cta_desc'); ?></p>
            <form action="upgrade_to_seller.php" method="POST">
                <input type="hidden" name="action" value="upgrade">
                <button type="submit" id="btn-upgrade-seller"
                    onclick="this.disabled=true; this.textContent='<?php echo addslashes(__('upgrade_btn_loading')); ?>'; this.form.submit();"
                    class="inline-flex items-center gap-2 px-10 py-4 bg-white text-orange-600 font-black rounded-2xl shadow-lg hover:shadow-xl text-sm transition-all duration-200 hover:-translate-y-1 focus:outline-none">
                    <?php echo __('upgrade_btn'); ?>
                </button>
            </form>
            <p class="text-xs text-orange-200 mt-5"><?php echo __('upgrade_note'); ?></p>
        </div>
    </div>

    <div class="text-center mt-6">
        <a href="index.php" class="text-sm text-gray-400 hover:text-gray-600 transition-colors"><?php echo __('upgrade_back'); ?></a>
    </div>

</div>

<?php
renderFooter();
?>
