<?php
/**
 * @license GPL-2.0-or-later
 */
defined( 'ABSPATH' ) || exit;
?>
<div class="volade-wrap volade-dashboard-page">
    <?php if ( ! $user ) : ?>
        <?php
        $hero_kicker  = __( 'Écosystème Volade', 'php-compatibility-checker-by-volade' );
        $hero_title   = __( 'Tableau de bord', 'php-compatibility-checker-by-volade' );
        $hero_lead    = __( 'Vos plugins Volade fonctionnent sans compte. Gérez vos extensions installées ou connectez-vous pour l\'XP, les badges et les formules V+.', 'php-compatibility-checker-by-volade' );
        $hero_variant = 'indigo';
        $hero_orbs    = array( '⚡', '🔌', '⚙️' );
        ob_start();
        ?>
        <?php if ( ! empty( $this->get_registered_plugins() ) ) : ?>
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=' . Volade_SDK::get_plugins_overview_slug() ) ); ?>" class="volade-btn volade-btn-primary">
                <?php $this->_e( 'Mes plugins installés' ); ?>
            </a>
        <?php endif; ?>
        <?php if ( ! $this->use_discreet_guest_upsell() ) : ?>
        <a href="<?php echo esc_url( admin_url( 'admin.php?page=volade-auth' ) ); ?>" class="volade-btn volade-btn-secondary">
            <?php $this->_e( 'Connexion Volade' ); ?>
        </a>
        <?php endif; ?>
        <?php
        $hero_actions_html = ob_get_clean();
        include dirname( __FILE__ ) . '/partials/page-hero.php';
        ?>

        <div class="volade-card volade-empty-state">
            <div class="volade-empty-icon">⚡</div>
            <h2><?php $this->_e( 'Vos plugins fonctionnent déjà' ); ?></h2>
            <p class="volade-muted volade-empty-lead">
                <?php $this->_e( 'Aucun compte requis pour utiliser les réglages de vos extensions Volade. Un compte gratuit débloque le catalogue, les missions, le parrainage et des limites plus élevées.' ); ?>
            </p>
            <?php if ( ! empty( $this->get_registered_plugins() ) ) : ?>
                <a href="<?php echo esc_url( admin_url( 'admin.php?page=' . Volade_SDK::get_plugins_overview_slug() ) ); ?>" class="volade-btn volade-btn-secondary">
                    <?php $this->_e( 'Voir mes plugins' ); ?>
                </a>
            <?php endif; ?>
            <?php if ( ! $this->use_discreet_guest_upsell() ) : ?>
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=volade-auth' ) ); ?>" class="volade-btn volade-btn-primary">
                <?php $this->_e( 'Créer un compte gratuit' ); ?>
            </a>
            <?php else : ?>
            <p class="volade-muted" style="margin-top:12px;font-size:13px;">
                <?php $this->_e( 'Compte Volade optionnel — réglages Heartbeat déjà disponibles sans inscription.' ); ?>
                <a href="<?php echo esc_url( admin_url( 'admin.php?page=volade-auth' ) ); ?>"><?php $this->_e( 'Se connecter' ); ?></a>
            </p>
            <?php endif; ?>
        </div>
    <?php else : ?>
        <?php
        $tier       = $this->get_tier( $user['id'] );
        $site_limit = $this->get_site_limit( $user['id'] );
        $sites_used = $this->get_active_sites_count( $user['id'] );
        $member_since = ! empty( $user['created_at'] )
            ? mysql2date( get_option( 'date_format' ), $user['created_at'] )
            : '';

        $hero_kicker  = __( 'Mon espace Volade', 'php-compatibility-checker-by-volade' );
        $hero_title   = $user['pseudo'];
        $hero_lead    = sprintf(
            /* translators: 1: email */
            __( 'Connecté en tant que %s — suivez votre progression, badges et accès premium.', 'php-compatibility-checker-by-volade' ),
            $user['email']
        );
        $hero_variant = 'indigo';
        $hero_orbs    = array();

        ob_start();
        ?>
        <div class="volade-hero-user-chip">
            <div class="volade-avatar volade-avatar--sm">
                <?php echo esc_html( strtoupper( substr( $user['pseudo'], 0, 2 ) ) ); ?>
            </div>
            <div class="volade-hero-user-chip-body">
                <strong><?php
                /* translators: %d: user level number */
                printf( esc_html__( 'Niveau %d', 'php-compatibility-checker-by-volade' ), (int) $user['level'] );
                ?></strong>
                <span><?php echo esc_html( $this->get_tier_label( $tier ) ); ?></span>
            </div>
        </div>
        <?php
        $hero_aside_html = ob_get_clean();

        ob_start();
        ?>
        <a href="<?php echo esc_url( admin_url( 'admin.php?page=volade-activity' ) ); ?>" class="volade-btn volade-btn-secondary"><?php $this->_e( 'Activité' ); ?></a>
        <a href="<?php echo esc_url( admin_url( 'admin.php?page=volade-discover' ) ); ?>" class="volade-btn volade-btn-secondary"><?php $this->_e( 'Découvrir' ); ?></a>
        <?php if ( 'free' === $tier ) : ?>
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=volade-premium' ) ); ?>" class="volade-btn volade-btn-primary"><?php $this->_e( 'V+ Premium' ); ?></a>
        <?php endif; ?>
        <button type="button" id="volade-logout-btn" class="volade-btn volade-btn-secondary volade-btn-sm"><?php $this->_e( 'Déconnexion' ); ?></button>
        <?php
        $hero_actions_html = ob_get_clean();
        include dirname( __FILE__ ) . '/partials/page-hero.php';
        ?>

        <?php if ( isset( $_GET['volade_welcome'] ) ) : // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>
            <div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Bienvenue ! Votre compte Volade est actif.', 'php-compatibility-checker-by-volade' ); ?></p></div>
        <?php endif; ?>

        <div class="volade-dash-grid">
            <div class="volade-stat-card">
                <div class="volade-stat-icon volade-stat-icon--blue">📦</div>
                <span class="volade-stat-number"><?php echo (int) $user['xp']; ?></span>
                <span class="volade-stat-label"><?php $this->_e( 'XP' ); ?></span>
            </div>
            <div class="volade-stat-card">
                <div class="volade-stat-icon volade-stat-icon--amber">⭐</div>
                <span class="volade-stat-number"><?php echo (int) $user['level']; ?></span>
                <span class="volade-stat-label"><?php $this->_e( 'Niveau' ); ?></span>
            </div>
            <div class="volade-stat-card">
                <div class="volade-stat-icon volade-stat-icon--green">🔥</div>
                <span class="volade-stat-number"><?php echo (int) ( $user['streak'] ?? 0 ); ?></span>
                <span class="volade-stat-label"><?php $this->_e( 'Série (jours)' ); ?></span>
            </div>
            <div class="volade-stat-card">
                <div class="volade-stat-icon volade-stat-icon--indigo">🏆</div>
                <span class="volade-stat-number"><?php echo count( $user['badges'] ?? array() ); ?></span>
                <span class="volade-stat-label"><?php $this->_e( 'Badges' ); ?></span>
            </div>
        </div>

        <div class="volade-card volade-account-card">
            <div class="volade-card-header">
                <span class="volade-card-icon-md">👤</span>
                <h2><?php $this->_e( 'Informations du compte' ); ?></h2>
            </div>
            <dl class="volade-account-meta">
                <div>
                    <dt><?php $this->_e( 'Pseudo' ); ?></dt>
                    <dd><?php echo esc_html( $user['pseudo'] ); ?></dd>
                </div>
                <div>
                    <dt><?php $this->_e( 'E-mail' ); ?></dt>
                    <dd><?php echo esc_html( $user['email'] ); ?></dd>
                </div>
                <?php if ( $member_since ) : ?>
                    <div>
                        <dt><?php $this->_e( 'Membre depuis' ); ?></dt>
                        <dd><?php echo esc_html( $member_since ); ?></dd>
                    </div>
                <?php endif; ?>
                <div>
                    <dt><?php $this->_e( 'Formule' ); ?></dt>
                    <dd><?php echo esc_html( $this->get_tier_label( $tier ) ); ?></dd>
                </div>
                <div>
                    <dt><?php $this->_e( 'Sites V+' ); ?></dt>
                    <dd>
                        <?php echo (int) $sites_used; ?> /
                        <?php echo -1 === $site_limit ? '∞' : (int) $site_limit; ?>
                    </dd>
                </div>
            </dl>
            <div class="volade-quick-links">
                <a href="<?php echo esc_url( admin_url( 'admin.php?page=volade-activity' ) ); ?>" class="volade-btn volade-btn-secondary volade-btn-sm"><?php $this->_e( 'Activité' ); ?></a>
                <a href="<?php echo esc_url( admin_url( 'admin.php?page=volade-referrals' ) ); ?>" class="volade-btn volade-btn-secondary volade-btn-sm"><?php $this->_e( 'Parrainage' ); ?></a>
                <a href="<?php echo esc_url( admin_url( 'admin.php?page=volade-discover' ) ); ?>" class="volade-btn volade-btn-secondary volade-btn-sm"><?php $this->_e( 'Découvrir' ); ?></a>
                <a href="<?php echo esc_url( admin_url( 'admin.php?page=volade-promote' ) ); ?>" class="volade-btn volade-btn-secondary volade-btn-sm"><?php $this->_e( 'Promouvoir' ); ?></a>
                <?php if ( 'free' === $tier ) : ?>
                    <a href="<?php echo esc_url( admin_url( 'admin.php?page=volade-premium' ) ); ?>" class="volade-btn volade-btn-primary volade-btn-sm"><?php $this->_e( 'V+ Premium' ); ?></a>
                <?php endif; ?>
            </div>
        </div>

        <?php
        $next_level = $user['level'] + 1;
        $xp_req = $this->get_level_xp_requirements( $next_level );
        $current_xp_in_level = $user['xp'] - $xp_req['current'];
        $xp_needed = $xp_req['next'] - $xp_req['current'];
        $progress = $xp_needed > 0 ? min( 100, round( ( $current_xp_in_level / $xp_needed ) * 100 ) ) : 0;
        ?>

        <div class="volade-card volade-dash-card-lg">
            <div class="volade-card-header">
                <span class="volade-card-icon-md">⭐</span>
                <h2><?php $this->_e( 'Progression du niveau' ); ?></h2>
            </div>
            <p class="volade-muted">
                <?php
                printf(
                    /* translators: 1: current XP in level, 2: XP needed, 3: next level number */
                    esc_html__( '%1$d / %2$d XP pour atteindre le niveau %3$d', 'php-compatibility-checker-by-volade' ),
                    (int) $current_xp_in_level,
                    (int) $xp_needed,
                    (int) $next_level
                );
                ?>
            </p>
            <div class="volade-progress">
                <div class="volade-progress-bar" style="width:<?php echo (int) $progress; ?>%"></div>
            </div>
        </div>

        <div class="volade-card">
            <div class="volade-card-header">
                <span class="volade-card-icon-md">🏆</span>
                <h2><?php $this->_e( 'Vos badges' ); ?></h2>
            </div>
            <?php if ( ! empty( $user['badges'] ) ) : ?>
                <div class="volade-badges-grid">
                <?php foreach ( $user['badges'] as $badge ) : ?>
                    <div class="volade-badge-item">
                        <span class="volade-badge-icon-lg"><?php echo esc_html( $badge['icon'] ?? '🏅' ); ?></span>
                        <strong><?php echo esc_html( $badge['name'] ?? $this->get_badge_name( $badge['badge_key'] ?? $badge['key'] ?? '' ) ); ?></strong>
                        <small><?php echo esc_html( $badge['desc'] ?? '' ); ?></small>
                    </div>
                <?php endforeach; ?>
                </div>
            <?php else : ?>
                <p class="volade-muted"><?php $this->_e( 'Effectuez des actions pour gagner votre premier badge !' ); ?></p>
            <?php endif; ?>
        </div>

        <?php if ( 'free' === $tier ) : ?>
            <?php $this->render_premium_upsell_badge(); ?>
        <?php endif; ?>
    <?php endif; ?>
</div>
