<?php
/**
 * Page Promouvoir — missions et calendrier de série.
 *
 * @package Volade\SDK
 * @license GPL-2.0-or-later
 * @var object|null $user
 * @var bool $guest_mode
 * @var array $missions
 * @var array $calendar
 * @var bool $today_checked
 * @var int $completed_cnt
 * @var int $total_xp
 */

defined( 'ABSPATH' ) || exit;

$guest_mode = ! empty( $guest_mode );
$auth_url   = admin_url( 'admin.php?page=volade-auth' );
?>
<div class="volade-wrap volade-promote-wrap" <?php echo $guest_mode ? 'data-guest-mode="1"' : ''; ?>>
    <?php
    $hero_kicker  = __( 'Engagement', 'php-compatibility-checker-by-volade' );
    $hero_title   = __( 'Promouvoir', 'php-compatibility-checker-by-volade' );
    $hero_lead    = $guest_mode
        ? __( 'Découvrez les missions Volade gratuitement — partagez, explorez et préparez votre progression. Connectez-vous pour valider et cumuler XP, séries et badges.', 'php-compatibility-checker-by-volade' )
        : __( 'Complétez des missions, partagez Volade et validez chaque jour pour gagner de l\'XP et des bonus.', 'php-compatibility-checker-by-volade' );
    $hero_variant = 'indigo';
    $hero_orbs    = array( '🎯', '📅', '⭐' );

    ob_start();
    if ( $guest_mode ) :
        ?>
        <a href="<?php echo esc_url( $auth_url ); ?>" class="volade-btn volade-btn-primary">
            <?php esc_html_e( 'Se connecter pour valider', 'php-compatibility-checker-by-volade' ); ?>
        </a>
        <?php
    else :
        ?>
        <a href="<?php echo esc_url( admin_url( 'admin.php?page=volade-activity' ) ); ?>" class="volade-btn volade-btn-secondary">
            <?php esc_html_e( 'Mon activité', 'php-compatibility-checker-by-volade' ); ?>
        </a>
        <?php
    endif;
    $hero_actions_html = ob_get_clean();
    include dirname( __FILE__ ) . '/partials/page-hero.php';
    ?>

    <?php if ( $guest_mode ) : ?>
        <?php $context = 'promote'; include dirname( __FILE__ ) . '/partials/guest-xp-banner.php'; ?>
    <?php endif; ?>

    <div class="volade-dash-grid volade-promote-stats">
        <div class="volade-stat-card">
            <div class="volade-stat-icon volade-stat-icon--indigo">🔥</div>
            <span class="volade-stat-number volade-promote-streak"><?php echo $guest_mode ? '—' : (int) $user['streak']; ?></span>
            <span class="volade-stat-label"><?php esc_html_e( 'Série (jours)', 'php-compatibility-checker-by-volade' ); ?></span>
        </div>
        <div class="volade-stat-card">
            <div class="volade-stat-icon volade-stat-icon--dark">✅</div>
            <span class="volade-stat-number volade-promote-completed-count">
                <?php echo $guest_mode ? '0/' . count( $missions ) : (int) $completed_cnt . '/' . count( $missions ); ?>
            </span>
            <span class="volade-stat-label"><?php esc_html_e( 'Missions faites', 'php-compatibility-checker-by-volade' ); ?></span>
        </div>
        <div class="volade-stat-card">
            <div class="volade-stat-icon volade-stat-icon--indigo">⭐</div>
            <span class="volade-stat-number"><?php echo $guest_mode ? '+' . (int) $total_xp : '+' . (int) $total_xp; ?></span>
            <span class="volade-stat-label"><?php echo $guest_mode ? esc_html__( 'XP à gagner', 'php-compatibility-checker-by-volade' ) : esc_html__( 'XP missions', 'php-compatibility-checker-by-volade' ); ?></span>
        </div>
        <div class="volade-stat-card">
            <div class="volade-stat-icon volade-stat-icon--dark">📦</div>
            <span class="volade-stat-number volade-promote-user-xp"><?php echo $guest_mode ? '—' : (int) $user['xp']; ?></span>
            <span class="volade-stat-label"><?php esc_html_e( 'XP total', 'php-compatibility-checker-by-volade' ); ?></span>
        </div>
    </div>

    <div class="volade-promote-tabs-wrap">
        <div class="volade-promote-tabs" role="tablist" aria-label="<?php esc_attr_e( 'Promouvoir', 'php-compatibility-checker-by-volade' ); ?>">
            <button type="button"
                    class="volade-promote-tab is-active"
                    role="tab"
                    id="volade-promote-tab-btn-missions"
                    aria-selected="true"
                    aria-controls="volade-promote-tab-missions"
                    data-tab="missions">
                <span class="volade-promote-tab-icon" aria-hidden="true">🎯</span>
                <?php esc_html_e( 'Missions', 'php-compatibility-checker-by-volade' ); ?>
                <span class="volade-promote-tab-badge"><?php echo $guest_mode ? '0/' . count( $missions ) : (int) $completed_cnt . '/' . count( $missions ); ?></span>
            </button>
            <button type="button"
                    class="volade-promote-tab"
                    role="tab"
                    id="volade-promote-tab-btn-calendar"
                    aria-selected="false"
                    aria-controls="volade-promote-tab-calendar"
                    data-tab="calendar"
                    tabindex="-1">
                <span class="volade-promote-tab-icon" aria-hidden="true">📅</span>
                <?php esc_html_e( 'Calendrier', 'php-compatibility-checker-by-volade' ); ?>
                <span class="volade-promote-tab-badge volade-promote-tab-badge--streak">🔥 <?php echo $guest_mode ? '0' : (int) $user['streak']; ?></span>
            </button>
        </div>

        <div class="volade-promote-tab-panels">
            <div class="volade-promote-tab-panel is-active"
                 id="volade-promote-tab-missions"
                 role="tabpanel"
                 aria-labelledby="volade-promote-tab-btn-missions">
                <div class="volade-card volade-promote-panel-card">
                    <div class="volade-card-header">
                        <span class="volade-card-icon-md">🎯</span>
                        <div>
                            <h2><?php esc_html_e( 'Missions', 'php-compatibility-checker-by-volade' ); ?></h2>
                            <p class="volade-muted volade-promote-panel-lead">
                                <?php if ( $guest_mode ) : ?>
                                    <?php esc_html_e( 'Lancez les actions librement. Connectez-vous ensuite pour récupérer l\'XP et suivre votre progression.', 'php-compatibility-checker-by-volade' ); ?>
                                <?php else : ?>
                                    <?php esc_html_e( 'Partagez, explorez et gagnez de l\'XP — certaines missions sont quotidiennes ou hebdomadaires.', 'php-compatibility-checker-by-volade' ); ?>
                                <?php endif; ?>
                            </p>
                        </div>
                    </div>

                    <div class="volade-missions-grid">
            <?php foreach ( $missions as $mission ) : ?>
                <?php
                $type      = $mission['type'] ?? 'manual';
                $completed = ! empty( $mission['completed'] );
                $available = ! empty( $mission['available'] );
                $card_cls  = 'volade-mission-card';
                if ( $completed ) {
                    $card_cls .= ' is-done';
                } elseif ( ! $available ) {
                    $card_cls .= ' is-locked';
                }
                if ( $guest_mode ) {
                    $card_cls .= ' is-guest-preview';
                }
                ?>
                <div class="<?php echo esc_attr( $card_cls ); ?>"
                     data-mission="<?php echo esc_attr( $mission['key'] ); ?>"
                     data-type="<?php echo esc_attr( $type ); ?>"
                     data-url="<?php echo esc_url( $mission['url'] ?? '' ); ?>"
                     data-timer="<?php echo in_array( $type, array( 'visit_url', 'watch_video', 'share_social', 'internal_link' ), true ) ? ( 'watch_video' === $type ? '15' : '8' ) : '0'; ?>">
                    <div class="volade-mission-icon"><?php echo esc_html( $mission['icon'] ?? '🎯' ); ?></div>
                    <div class="volade-mission-body">
                        <h3><?php echo esc_html( $mission['title'] ); ?></h3>
                        <p><?php echo esc_html( $mission['desc'] ); ?></p>
                        <span class="volade-mission-xp"><?php echo esc_html( $mission['xp_label'] ); ?></span>
                        <?php if ( ! empty( $mission['repeat'] ) && 'once' !== $mission['repeat'] ) : ?>
                            <span class="volade-mission-repeat">
                                <?php
                                echo 'daily' === $mission['repeat']
                                    ? esc_html__( 'Quotidien', 'php-compatibility-checker-by-volade' )
                                    : esc_html__( 'Hebdomadaire', 'php-compatibility-checker-by-volade' );
                                ?>
                            </span>
                        <?php endif; ?>
                    </div>
                    <div class="volade-mission-action">
                        <?php if ( $guest_mode ) : ?>
                            <?php if ( in_array( $type, array( 'visit_url', 'watch_video', 'share_social', 'internal_link' ), true ) && ! empty( $mission['url'] ) ) : ?>
                                <button type="button" class="volade-btn volade-btn-sm volade-btn-secondary volade-mission-start-btn">
                                    <?php esc_html_e( 'Commencer', 'php-compatibility-checker-by-volade' ); ?>
                                </button>
                                <a href="<?php echo esc_url( $auth_url ); ?>" class="volade-mission-guest-xp-hint">
                                    <?php esc_html_e( 'Se connecter pour l\'XP', 'php-compatibility-checker-by-volade' ); ?>
                                </a>
                            <?php elseif ( 'daily_checkin' === $mission['key'] ) : ?>
                                <a href="<?php echo esc_url( $auth_url ); ?>" class="volade-btn volade-btn-sm volade-btn-primary volade-mission-guest-auth">
                                    <?php esc_html_e( 'Connectez-vous (+10 XP)', 'php-compatibility-checker-by-volade' ); ?>
                                </a>
                            <?php else : ?>
                                <a href="<?php echo esc_url( $auth_url ); ?>" class="volade-btn volade-btn-sm volade-btn-primary volade-mission-guest-auth">
                                    <?php esc_html_e( 'Se connecter pour valider', 'php-compatibility-checker-by-volade' ); ?>
                                </a>
                            <?php endif; ?>
                        <?php elseif ( $completed ) : ?>
                            <span class="volade-mission-status done">
                                <span class="volade-mission-check" aria-hidden="true">✓</span>
                                <?php echo esc_html( $mission['status_label'] ?? __( 'Validé', 'php-compatibility-checker-by-volade' ) ); ?>
                            </span>
                        <?php elseif ( ! $available ) : ?>
                            <span class="volade-mission-status locked"><?php esc_html_e( 'À débloquer', 'php-compatibility-checker-by-volade' ); ?></span>
                        <?php elseif ( 'daily_checkin' === $mission['key'] ) : ?>
                            <span class="volade-mission-status hint"><?php esc_html_e( 'Via le calendrier', 'php-compatibility-checker-by-volade' ); ?></span>
                        <?php elseif ( 'copy_referral' === $type ) : ?>
                            <button type="button" class="volade-btn volade-btn-sm volade-btn-secondary volade-mission-copy-btn"
                                    data-link="<?php echo esc_url( $this->get_referral_link( $user['id'] ) ); ?>">
                                <?php esc_html_e( 'Copier & valider', 'php-compatibility-checker-by-volade' ); ?>
                            </button>
                        <?php elseif ( in_array( $type, array( 'visit_url', 'watch_video', 'share_social', 'internal_link' ), true ) ) : ?>
                            <button type="button" class="volade-btn volade-btn-sm volade-btn-primary volade-mission-start-btn">
                                <?php esc_html_e( 'Commencer', 'php-compatibility-checker-by-volade' ); ?>
                            </button>
                        <?php else : ?>
                            <button type="button" class="volade-btn volade-btn-sm volade-btn-primary volade-mission-claim-btn">
                                <?php esc_html_e( 'Récupérer', 'php-compatibility-checker-by-volade' ); ?>
                            </button>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endforeach; ?>
                    </div>
                </div>
            </div>

            <div class="volade-promote-tab-panel"
                 id="volade-promote-tab-calendar"
                 role="tabpanel"
                 aria-labelledby="volade-promote-tab-btn-calendar"
                 hidden>
                <div class="volade-card volade-promote-panel-card volade-checkin-card">
                    <div class="volade-card-header">
                        <span class="volade-card-icon-md">📅</span>
                        <div>
                            <h2><?php esc_html_e( 'Série quotidienne', 'php-compatibility-checker-by-volade' ); ?></h2>
                            <p class="volade-muted volade-checkin-lead">
                                <?php if ( $guest_mode ) : ?>
                                    <?php esc_html_e( 'Validez chaque jour pour maintenir une série et débloquer des bonus XP — disponible après connexion.', 'php-compatibility-checker-by-volade' ); ?>
                                <?php else : ?>
                                    <?php esc_html_e( 'Validez chaque jour pour maintenir votre série et débloquer des bonus.', 'php-compatibility-checker-by-volade' ); ?>
                                <?php endif; ?>
                            </p>
                        </div>
                    </div>

                    <div class="volade-calendar-grid" aria-label="<?php esc_attr_e( 'Calendrier des 28 derniers jours', 'php-compatibility-checker-by-volade' ); ?>">
                        <?php foreach ( $calendar as $day ) : ?>
                            <div class="volade-calendar-day <?php echo $day['checked'] ? 'is-checked' : ''; ?> <?php echo $day['today'] ? 'is-today' : ''; ?>"
                                 title="<?php echo esc_attr( $day['date'] ); ?>">
                                <span class="volade-calendar-weekday"><?php echo esc_html( $day['weekday'] ); ?></span>
                                <span class="volade-calendar-num"><?php echo esc_html( $day['label'] ); ?></span>
                                <?php if ( $day['checked'] ) : ?>
                                    <span class="volade-calendar-check" aria-hidden="true">✓</span>
                                <?php endif; ?>
                            </div>
                        <?php endforeach; ?>
                    </div>

                    <div class="volade-checkin-action">
                        <?php if ( $guest_mode ) : ?>
                            <a href="<?php echo esc_url( $auth_url ); ?>" class="volade-btn volade-btn-primary volade-mission-guest-auth">
                                <?php esc_html_e( 'Connectez-vous pour valider (+10 XP/jour)', 'php-compatibility-checker-by-volade' ); ?>
                            </a>
                        <?php elseif ( $today_checked ) : ?>
                            <span class="volade-checkin-done">✅ <?php esc_html_e( 'Aujourd\'hui validé — revenez demain !', 'php-compatibility-checker-by-volade' ); ?></span>
                        <?php else : ?>
                            <button type="button" class="volade-btn volade-btn-primary volade-daily-checkin-btn" data-mission="daily_checkin">
                                <?php esc_html_e( 'Valider aujourd\'hui', 'php-compatibility-checker-by-volade' ); ?> (+10 XP)
                            </button>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div id="volade-mission-toast" class="volade-mission-toast" role="status" aria-live="polite"></div>
</div>
