<?php 

if ( ! defined( 'ABSPATH' ) ) exit;
/**
 * Shortcode: [afdp_auth_info]
 * Features: Dynamic Page Links, Custom SVGs, Live Cart Badge, and Responsive Design.
 */
add_shortcode('afdp_auth_info', 'afdp_auth_info_masterpiece');

function afdp_auth_info_masterpiece() {
    // 1. Fetch URLs dynamically using stored IDs from activation
    $login_url    = get_permalink(get_option('afdp_login_page'));
    $register_url = get_permalink(get_option('afdp_register_page'));
    $account_url  = get_permalink(get_option('afdp_account_page'));
    $checkout_url = get_permalink(get_option('afdp_checkout_page'));

    ob_start(); 
    ?>
    <div class="afdp-auth-bar">
        <?php if ( is_user_logged_in() ) : 
            $current_user = wp_get_current_user();
        ?>
            <div class="afdp-auth-logged-in">
                <a href="<?php echo esc_url($checkout_url); ?>" class="afdp-icon-btn afdp-cart-anchor" title="View Cart">
                    <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="9" cy="21" r="1"></circle><circle cx="20" cy="21" r="1"></circle><path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"></path></svg>
                    <span id="afdp-cart-count-badge">0</span>
                </a>

                <a href="<?php echo esc_url($account_url); ?>" class="afdp-user-pill">
                    <span class="afdp-user-name"><?php echo esc_html($current_user->display_name); ?></span>
                    <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path><circle cx="12" cy="7" r="4"></circle></svg>
                </a>

                <a href="<?php echo esc_url(wp_logout_url(home_url())); ?>" class="afdp-logout-icon" title="Logout">
                    <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"></path><polyline points="16 17 21 12 16 7"></polyline><line x1="21" y1="12" x2="9" y2="12"></line></svg>
                </a>
            </div>

        <?php else : ?>
            <div class="afdp-auth-guest">
                <a href="<?php echo esc_url($login_url); ?>" class="afdp-btn-outline">Login</a>
                <a href="<?php echo esc_url($register_url); ?>" class="afdp-btn-solid">Registration</a>
            </div>
        <?php endif; ?>
    </div>

    <style>
        .afdp-auth-bar { display: inline-block; vertical-align: middle; font-family: 'Plus Jakarta Sans', sans-serif; }
        .afdp-auth-logged-in, .afdp-auth-guest { display: flex; align-items: center; gap: 18px; }

        /* Icon Buttons */
        .afdp-icon-btn, .afdp-logout-icon { color: #0F172A; transition: 0.3s; display: flex; align-items: center; text-decoration: none; }
        .afdp-icon-btn:hover, .afdp-logout-icon:hover { color: #FF385C; }

        /* Cart Badge */
        .afdp-cart-anchor { position: relative; }
        #afdp-cart-count-badge {
            position: absolute; top: -7px; right: -9px;
            background: #FF385C; color: #fff;
            font-size: 10px; font-weight: 800;
            min-width: 18px; height: 18px;
            display: none; align-items: center; justify-content: center;
            border-radius: 50%; border: 2px solid #fff;
        }

        /* User Pill */
        .afdp-user-pill { 
            display: flex; align-items: center; gap: 8px; 
            background: #F1F5F9; padding: 6px 14px; 
            border-radius: 50px; text-decoration: none; color: #0F172A;
            font-weight: 700; font-size: 14px; transition: 0.3s;
        }
        .afdp-user-pill:hover { background: #E2E8F0; }

        /* Guest Buttons */
        .afdp-btn-outline { color: #64748B; text-decoration: none; font-weight: 700; font-size: 14px; }
        .afdp-btn-solid { 
            background: #0F172A; color: #fff; padding: 10px 20px; 
            border-radius: 12px; text-decoration: none; font-weight: 700; font-size: 14px; transition: 0.3s;
        }
        .afdp-btn-solid:hover { background: #000; transform: translateY(-1px); }

        @media (max-width: 600px) { .afdp-user-name { display: none; } }
    </style>

    <script>
    jQuery(document).ready(function($){
        // Function to update badge from LocalStorage (persists across pages)
        function refreshCartBadge() {
            const count = localStorage.getItem('afdp_cart_count') || 0;
            const badge = $('#afdp-cart-count-badge');
            if(parseInt(count) > 0) {
                badge.text(count).css('display', 'flex');
            } else {
                badge.hide();
            }
        }
        
        refreshCartBadge();
        // Listen for changes in localStorage from other tabs/pages
        window.addEventListener('storage', refreshCartBadge);
    });
    </script>
    <?php
    return ob_get_clean();
}