<?php
$recommended_plugin = $recommended_plugin ?? \NeurogenesisTheme\Helpers\RecommendedPlugin::get_card_data();
$quick_setup        = $quick_setup ?? \NeurogenesisTheme\QuickSetup\QuickSetup::get_progress();
$resources          = $resources ?? \NeurogenesisTheme\Helpers\DashboardResources::all();
$rating             = $rating ?? \NeurogenesisTheme\Helpers\RatingNotice::view_context();
$welcome_hero_url   = $welcome_hero_url ?? neurogenesis_theme_theme_url( 'assets/admin/images/dashboard-welcome-hero.png' );
$review_url         = $rating['review_url'] ?? 'https://wordpress.org/themes/';
$plugin_status      = $plugin_status ?? ( $recommended_plugin['status'] ?? 'not_installed' );
$start_building_url = $start_building_url ?? neurogenesis_theme_generate_page_url( 'dashboard' );
?>

<div class="space-y-6">
    <section class="neurogenesis-theme-dashboard-hero">
        <div class="neurogenesis-theme-dashboard-hero__content">
            <div class="neurogenesis-theme-dashboard-hero__badge">
                <?php esc_html_e( 'Welcome to Thinko', 'thinko' ); ?>
            </div>
            <h2 class="neurogenesis-theme-dashboard-hero__title">
                <?php esc_html_e( 'Build your online courses faster, without the friction', 'thinko' ); ?>
            </h2>
            <p class="neurogenesis-theme-dashboard-hero__description">
                <?php esc_html_e( 'Thinko is built for online courses. Fast, easy to customize, and ready to use from day one, no coding needed.', 'thinko' ); ?>
            </p>
            <div class="neurogenesis-theme-dashboard-hero__actions">
                <a
                    href="<?php echo esc_url( $start_building_url ); ?>"
                    class="neurogenesis-theme-dashboard-hero__cta"
                    data-start-building-cta
                    data-plugin-status="<?php echo esc_attr( $plugin_status ); ?>">
                    <?php esc_html_e( 'Start Building Now', 'thinko' ); ?>
                </a>
                <p class="neurogenesis-theme-dashboard-hero__legal">
                    <?php
                    if ( 'not_installed' === $plugin_status ) {
                        esc_html_e( 'By clicking "Start Building Now," you agree to install and activate the Neurogenesis Styler plugin. It adds additional controls for the theme. Thinko works just fine without it — the plugin is optional.', 'thinko' );
                    } else {
                        esc_html_e( "By clicking 'Start Building Now', you'll begin the simple onboarding process.", 'thinko' );
                    }
                    ?>
                </p>
            </div>
        </div>
        <div class="neurogenesis-theme-dashboard-hero__visual">
            <img
                src="<?php echo esc_url( $welcome_hero_url ); ?>"
                alt=""
                class="neurogenesis-theme-dashboard-hero__image"
                loading="lazy"
                decoding="async" />
        </div>
    </section>

    <div class="neurogenesis-theme-dashboard-cards">
        <section
            id="neurogenesis-theme-recommended-plugin-card"
            class="neurogenesis-theme-dashboard-card neurogenesis-theme-dashboard-card--recommended">
            <div class="neurogenesis-theme-dashboard-card__header">
                <img
                    src="<?php echo esc_url( neurogenesis_theme_admin_icon_url( 'neurogenesis-styler-logo.svg' ) ); ?>"
                    alt=""
                    class="neurogenesis-theme-dashboard-card__icon"
                    width="50"
                    height="50"
                    loading="lazy"
                    decoding="async" />
                <div class="neurogenesis-theme-dashboard-card__heading">
                    <p class="neurogenesis-theme-dashboard-card__eyebrow">
                        <?php esc_html_e( 'Recommended for you', 'thinko' ); ?>
                    </p>
                    <div class="neurogenesis-theme-dashboard-card__title-row">
                        <h3 class="neurogenesis-theme-dashboard-card__title">
                            <?php echo esc_html( $recommended_plugin['name'] ); ?>
                        </h3>
                        <span
                            id="neurogenesis-theme-recommended-plugin-status"
                            class="neurogenesis-theme-dashboard-card__status hidden">
                            <?php echo esc_html( $recommended_plugin['status_label'] ); ?>
                        </span>
                    </div>
                </div>
            </div>
            <p class="neurogenesis-theme-dashboard-card__description">
                <?php esc_html_e( 'The ultimate styling companion for your WordPress site. Effortlessly customize fonts, colors, and layouts without touching a single line of code.', 'thinko' ); ?>
            </p>
            <?php if ( 'active' === $recommended_plugin['status'] ) : ?>
                <span class="neurogenesis-theme-dashboard-card__active"><?php esc_html_e( 'Active', 'thinko' ); ?></span>
            <?php else : ?>
                <button
                    type="button"
                    id="neurogenesis-theme-install-recommended-plugin"
                    class="neurogenesis-theme-dashboard-card__action">
                    <?php echo esc_html( $recommended_plugin['action_label'] ); ?>
                    <?php echo neurogenesis_theme_svg_icon( 'arrow-right' ); ?>
                </button>
            <?php endif; ?>
        </section>

        <section class="neurogenesis-theme-dashboard-card neurogenesis-theme-dashboard-card--feedback">
            <div class="neurogenesis-theme-dashboard-card__header">
                <span class="neurogenesis-theme-dashboard-card__icon-badge" aria-hidden="true">
                    <?php echo neurogenesis_theme_svg_icon( 'star-outline' ); ?>
                </span>
                <h3 class="neurogenesis-theme-dashboard-card__title">
                    <?php esc_html_e( 'Share Your Feedback', 'thinko' ); ?>
                </h3>
            </div>
            <p class="neurogenesis-theme-dashboard-card__description">
                <?php esc_html_e( 'Your reviews help us grow and improve Thinko Theme. Share your experience with the community and help us build a better theme for everyone.', 'thinko' ); ?>
            </p>
            <div class="neurogenesis-theme-dashboard-card__stars" aria-hidden="true">
                <?php for ( $i = 0; $i < 5; $i++ ) : ?>
                    <?php echo neurogenesis_theme_svg_icon( 'star-filled' ); ?>
                <?php endfor; ?>
            </div>
            <a
                href="<?php echo esc_url( $review_url ); ?>"
                target="_blank"
                rel="noopener noreferrer"
                class="neurogenesis-theme-dashboard-card__cta-button">
                <?php echo neurogenesis_theme_svg_icon( 'external-link' ); ?>
                <?php esc_html_e( 'Rate on WordPress.org', 'thinko' ); ?>
            </a>
        </section>
    </div>

    <section class="neurogenesis-theme-dashboard-quick-setup">
        <div class="neurogenesis-theme-dashboard-quick-setup__header">
            <div class="neurogenesis-theme-dashboard-quick-setup__heading">
                <h3 class="neurogenesis-theme-dashboard-quick-setup__title">
                    <?php esc_html_e( 'Quick Setup', 'thinko' ); ?>
                </h3>
                <p class="neurogenesis-theme-dashboard-quick-setup__subtitle">
                    <?php
                    printf(
                        /* translators: 1: completed steps, 2: total steps */
                        esc_html__( '%1$d of %2$d steps completed', 'thinko' ),
                        (int) $quick_setup['completed'],
                        (int) $quick_setup['total']
                    );
                    ?>
                </p>
            </div>
            <span class="neurogenesis-theme-dashboard-quick-setup__percent">
                <?php echo esc_html( (string) $quick_setup['percent'] ); ?>%
            </span>
        </div>

        <div
            class="neurogenesis-theme-dashboard-quick-setup__progress"
            role="progressbar"
            aria-valuemin="0"
            aria-valuemax="100"
            aria-valuenow="<?php echo esc_attr( (string) $quick_setup['percent'] ); ?>">
            <div
                class="neurogenesis-theme-dashboard-quick-setup__progress-bar"
                style="width: <?php echo esc_attr( (string) $quick_setup['percent'] ); ?>%;"></div>
        </div>

        <ul class="neurogenesis-theme-dashboard-quick-setup__steps">
            <?php foreach ( $quick_setup['steps'] as $step ) : ?>
                <?php
                $step_completed = ! empty( $step['completed'] );
                $step_clickable = ! empty( $step['clickable'] );
                $step_action    = isset( $step['action'] ) ? (string) $step['action'] : '';
                $step_link_class = 'neurogenesis-theme-dashboard-quick-setup__step-link';
                if ( $step_completed ) {
                    $step_link_class .= ' neurogenesis-theme-dashboard-quick-setup__step-link--completed';
                }
                ?>
                <li class="neurogenesis-theme-dashboard-quick-setup__step">
                    <?php if ( $step_clickable && '' !== $step_action ) : ?>
                        <button
                            type="button"
                            class="<?php echo esc_attr( $step_link_class ); ?>"
                            data-neurogenesis-theme-quick-setup-action="<?php echo esc_attr( $step_action ); ?>">
                    <?php elseif ( $step_clickable ) : ?>
                        <a
                            href="<?php echo esc_url( $step['url'] ); ?>"
                            class="<?php echo esc_attr( $step_link_class ); ?>">
                    <?php else : ?>
                        <span class="<?php echo esc_attr( $step_link_class ); ?>">
                    <?php endif; ?>
                        <?php if ( $step_completed ) : ?>
                            <span class="neurogenesis-theme-dashboard-quick-setup__step-icon neurogenesis-theme-dashboard-quick-setup__step-icon--completed" aria-hidden="true">
                                <?php echo neurogenesis_theme_svg_icon( 'check' ); ?>
                            </span>
                        <?php else : ?>
                            <span class="neurogenesis-theme-dashboard-quick-setup__step-icon" aria-hidden="true"></span>
                        <?php endif; ?>
                        <span class="neurogenesis-theme-dashboard-quick-setup__step-label">
                            <?php echo esc_html( $step['label'] ); ?>
                        </span>
                    <?php if ( $step_clickable && '' !== $step_action ) : ?>
                        </button>
                    <?php elseif ( $step_clickable ) : ?>
                        </a>
                    <?php else : ?>
                        </span>
                    <?php endif; ?>
                </li>
            <?php endforeach; ?>
        </ul>
    </section>

    <section class="neurogenesis-theme-dashboard-resources" aria-label="<?php esc_attr_e( 'Resources', 'thinko' ); ?>">
        <?php foreach ( $resources as $resource ) : ?>
            <?php
            $is_disabled = ! empty( $resource['disabled'] );
            $classes     = 'neurogenesis-theme-dashboard-resource' . ( $is_disabled ? ' neurogenesis-theme-dashboard-resource--disabled' : '' );
            ?>
            <?php if ( $is_disabled ) : ?>
                <div class="<?php echo esc_attr( $classes ); ?>" aria-disabled="true">
            <?php else : ?>
                <a
                    href="<?php echo esc_url( $resource['url'] ); ?>"
                    target="_blank"
                    rel="noopener noreferrer"
                    class="<?php echo esc_attr( $classes ); ?>">
            <?php endif; ?>
                    <div class="neurogenesis-theme-dashboard-resource__header">
                        <img
                            src="<?php echo esc_url( neurogenesis_theme_admin_icon_url( $resource['icon'] . '.svg' ) ); ?>"
                            alt=""
                            class="neurogenesis-theme-dashboard-resource__icon"
                            width="32"
                            height="32"
                            loading="lazy"
                            decoding="async" />
                        <?php if ( $is_disabled ) : ?>
                            <span class="neurogenesis-theme-dashboard-resource__badge">
                                <?php esc_html_e( 'Soon', 'thinko' ); ?>
                            </span>
                        <?php endif; ?>
                    </div>
                    <div class="neurogenesis-theme-dashboard-resource__body">
                        <div class="neurogenesis-theme-dashboard-resource__title-row">
                            <span class="neurogenesis-theme-dashboard-resource__title">
                                <?php echo esc_html( $resource['title'] ); ?>
                            </span>
                            <svg xmlns="http://www.w3.org/2000/svg" width="11" height="11" viewBox="0 0 11 11" fill="none" class="neurogenesis-theme-dashboard-resource__chevron" aria-hidden="true">
                                <path d="M4 2.75L7.25 5.5L4 8.25" stroke="currentColor" stroke-width="1.14583" stroke-linecap="round" stroke-linejoin="round" />
                            </svg>
                        </div>
                        <p class="neurogenesis-theme-dashboard-resource__description">
                            <?php echo esc_html( $resource['description'] ); ?>
                        </p>
                    </div>
            <?php if ( $is_disabled ) : ?>
                </div>
            <?php else : ?>
                </a>
            <?php endif; ?>
        <?php endforeach; ?>
    </section>
</div>
