<?php
/**
 * Post summary renderer for Sifency Addons dashboard.
 * Provides a single function `sifency_addons_render_themebuilder_summary()` which
 * computes counts and renders the post-type grid used on the welcome page.
 *
 * This version is styled to match the modern dashboard UI, includes icons,
 * and has a "View More" feature.
 */

if ( ! function_exists( 'sifency_addons_render_themebuilder_summary' ) ) {
    function sifency_addons_render_themebuilder_summary() {
        $post_type_breakdown = [];
        $total_posts = 0;
        $total_drafts = 0;
        
        $cpt_slug      = 'themebuilder';
        $taxonomy_slug = 'sifency_builder_type';

        $theme_builder_terms = get_terms( [
            'taxonomy'   => $taxonomy_slug,
            'hide_empty' => false,
        ] );

        $card_icons = [
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
            'dashicons-align-wide',
        ];
        $icon_index = 0;

        if ( ! is_wp_error( $theme_builder_terms ) && ! empty( $theme_builder_terms ) ) {

            foreach ( $theme_builder_terms as $term ) {
                $query_args = [
                    'post_type'      => $cpt_slug,
                    'post_status'    => 'any',
                    'posts_per_page' => -1,
                    'fields'         => 'ids',
                    'tax_query'      => [
                        [ 'taxonomy' => $taxonomy_slug, 'field' => 'slug', 'terms' => $term->slug, ],
                    ],
                ];
                $term_query = new WP_Query( $query_args );
                $term_count = $term_query->found_posts;

                $draft_query_args = $query_args;
                $draft_query_args['post_status'] = 'draft';
                $draft_query = new WP_Query( $draft_query_args );
                $draft_count = $draft_query->found_posts;

                $total_posts += $term_count;
                $total_drafts += $draft_count;

                $view_link = add_query_arg(
                    [
                        'page' => 'sifency-theme-builder',
                        'tab'  => $term->slug,
                    ],
                    admin_url( 'admin.php' )
                );

                $post_type_breakdown[ 'themebuilder_' . $term->slug ] = [
                    'label'     => $term->name,
                    'count'     => $term_count,
                    'view_link' => $view_link,
                    'icon'      => $card_icons[$icon_index % count($card_icons)],
                ];

                $icon_index++;
            }
        }


        //======================================================================
        // RENDER THE MARKUP
        //======================================================================
        ?>
        <div class="sifency-themebuilder-summary-wrapper">
            <div id="sifency-themebuilder-grid" class="sifency-themebuilder-type-grid">
                <!-- Main Summary Box (styled like the other cards) -->
                <div class="sifency-themebuilder-type-item">
                    <div class="sifency-card-icon-wrapper">
                                <span class="dashicons dashicons-admin-plugins"></span>
                        </div>

                    <h5><?php esc_html_e( 'ThemBuilder', 'sifency-addons' ); ?></h5>
                    <div class="sifency-themebuilder-count"><?php echo esc_html( $total_posts ); ?></div>
                    <small><?php echo /* translators: %s: drafts */ sprintf( esc_html__( '%d drafts', 'sifency-addons' ), esc_html( $total_drafts ) ); ?></small>
                </div>

                <!-- Individual Category Cards -->
                <?php foreach ( $post_type_breakdown as $post_type => $data ) : ?>
                <a href="<?php echo esc_url( $data['view_link'] ); ?>" class="sifency-themebuilder-type-item">
                    <div class="sifency-card-icon-wrapper">
                                <span class="dashicons <?php echo esc_attr($data['icon']); ?>"></span>
                            </div>
                    <div class="sifency-themebuilder-count"><?php echo esc_html( $data['count'] ); ?></div>
                    <h5><?php echo esc_html( $data['label'] ); ?></h5>
                </a>
                <?php endforeach; ?>
            </div>

            <?php if ( count($post_type_breakdown) + 1 > 6 ) : ?>
            <div class="sifency-view-more-container">
                <button id="sifency-view-more-btn" class="sifency-view-more-btn sifency-admin-btn">
                    <?php esc_html_e( 'View More', 'sifency-addons' ); ?>
                </button>
            </div>
            <?php endif; ?>
        </div>

        <script>
        document.addEventListener('DOMContentLoaded', function() {
            const viewMoreBtn = document.getElementById('sifency-view-more-btn');
            const grid = document.getElementById('sifency-themebuilder-grid');

            if (viewMoreBtn && grid) {
                viewMoreBtn.addEventListener('click', function() {
                    grid.classList.toggle('sifency-grid-expanded');

                    // Change button text
                    if (grid.classList.contains('sifency-grid-expanded')) {
                        this.textContent = '<?php esc_html_e( 'View Less', 'sifency-addons' ); ?>';
                    } else {
                        this.textContent = '<?php esc_html_e( 'View More', 'sifency-addons' ); ?>';
                    }
                });
            }
        });
        </script>
        <?php
    }
}