<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<div class="edocman-lite-categories">
    <?php if (!empty($categories)): ?>
        <div class="categories-grid">
            <?php foreach ($categories as $edocman_lite_cat): ?>
                <div class="category-item">
                    <?php
                    // Build image path from correct directory
                    $edocman_lite_image_url = '';
                    if (!empty($edocman_lite_cat->image)) {
                        $edocman_lite_upload_dir = wp_upload_dir();
                        $edocman_lite_image_path = $edocman_lite_upload_dir['basedir'] . '/edocman/categories/thumb/' . $edocman_lite_cat->image;
                        
                        if (file_exists($edocman_lite_image_path)) {
                            $edocman_lite_image_url = $edocman_lite_upload_dir['baseurl'] . '/edocman/categories/thumb/' . $edocman_lite_cat->image;
                        }
                    }
                    ?>
                    
                    <?php if ($atts['show_image']): ?>
                        <?php if ($edocman_lite_image_url): ?>
                            <div class="category-image">
                                <a href="<?php echo esc_url( add_query_arg('cat_id', $edocman_lite_cat->id) ); ?>">
                                    <img src="<?php echo esc_url($edocman_lite_image_url); ?>" 
                                         alt="<?php echo esc_attr($edocman_lite_cat->title); ?>"
                                         loading="lazy">
                                </a>
                            </div>
                        <?php else: ?>
                            <!-- Placeholder image if no image -->
                            <div class="category-image category-no-image">
                            <a href="<?php echo esc_url( add_query_arg('cat_id', $edocman_lite_cat->id) ); ?>">
                                    <div class="image-placeholder">
                                        <i class="dashicons dashicons-category"></i>
                                    </div>
                                </a>
                            </div>
                        <?php endif; ?>
                    <?php endif; ?>
                    
                    <div class="category-content">
                        <h3 class="category-title">
                            <a href="<?php echo esc_url( add_query_arg('cat_id', $edocman_lite_cat->id) ); ?>">
                                <?php echo esc_html($edocman_lite_cat->title); ?>
                            </a>
                        </h3>
                        
                        <?php if ($atts['show_description'] && !empty($edocman_lite_cat->description)): ?>
                            <div class="category-description">
                                <?php 
                                // Limit description length
                                $edocman_lite_description = wp_strip_all_tags($edocman_lite_cat->description);
                                if (strlen($edocman_lite_description) > 150) {
                                    $edocman_lite_description = substr($edocman_lite_description, 0, 150) . '...';
                                }
                                echo esc_html($edocman_lite_description);
                                ?>
                            </div>
                        <?php endif; ?>
                        
                        <div class="category-meta">
                            <?php
                            global $wpdb;
                            $edocman_lite_table_rel  = esc_sql( $wpdb->prefix . 'edocman_document_category' );
                            $edocman_lite_table_docs = esc_sql( $wpdb->prefix . 'edocman_documents' );
                            // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Count documents from plugin-owned custom tables for each category card.
                            // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
                            $edocman_lite_count = $wpdb->get_var( $wpdb->prepare(
                                "SELECT COUNT(*) FROM {$edocman_lite_table_rel} r INNER JOIN {$edocman_lite_table_docs} d ON r.document_id = d.id WHERE r.category_id = %d AND d.published = 1",
                                $edocman_lite_cat->id
                            ) );
                            // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
                            ?>
                            <span class="document-count">
                                <i class="dashicons dashicons-media-document"></i>
                                <?php
                                /* translators: %d: number of documents */
                                echo esc_html( sprintf( _n('%d document', '%d documents', $edocman_lite_count, 'edocman-lite'), absint( $edocman_lite_count ) ) ); ?>
                            </span>
                            
                            <?php if (!empty($edocman_lite_cat->created_time)): ?>
                                <span class="category-date">
                                    <i class="dashicons dashicons-calendar-alt"></i>
                                    <?php echo esc_html( date_i18n(get_option('date_format'), strtotime($edocman_lite_cat->created_time)) ); ?>
                                </span>
                            <?php endif; ?>
                        </div>
                        
                        <div class="category-actions">
                            <a href="<?php echo esc_url( add_query_arg('cat_id', $edocman_lite_cat->id) ); ?>" 
                               class="view-category-btn">
                                <?php esc_html_e('View Documents', 'edocman-lite'); ?>
                                <i class="dashicons dashicons-arrow-right-alt"></i>
                            </a>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php else: ?>
        <div class="edocman-no-items">
            <div class="no-items-icon">
                <i class="dashicons dashicons-category"></i>
            </div>
            <h3><?php esc_html_e('No categories found.', 'edocman-lite'); ?></h3>
            <p><?php esc_html_e('There are no categories to display at this time.', 'edocman-lite'); ?></p>
        </div>
    <?php endif; ?>
</div>
