<?php

/**
 * My Account Download Usage Template
 *
 * Displays download usage statistics in My Account downloads page.
 *
 * @package ArraySubs\Features\MembersAccess\views
 * @since 1.0.0
 *
 * @var array $arraysubs_usage_info Array of usage data for display.
 */

defined('ABSPATH') || exit;

if (empty($arraysubs_usage_info)) {
    return;
}
?>

<div class="arraysubs-download-usage">
    <h3 class="arraysubs-download-usage__title">
        <?php esc_html_e('Your Download Limits', 'arraysubs'); ?>
    </h3>

    <div class="arraysubs-download-usage__items">
        <?php foreach ($arraysubs_usage_info as $arraysubs_usage) : ?>
            <div class="arraysubs-download-usage__item">
                <div class="arraysubs-download-usage__item-header">
                    <span class="arraysubs-download-usage__name">
                        <?php echo esc_html($arraysubs_usage['name']); ?>
                    </span>
                    <?php if (!empty($arraysubs_usage['period'])) : ?>
                        <span class="arraysubs-download-usage__period">
                            (<?php echo esc_html($arraysubs_usage['period']); ?>)
                        </span>
                    <?php endif; ?>
                </div>

                <?php if ($arraysubs_usage['unlimited']) : ?>
                    <div class="arraysubs-download-usage__unlimited">
                        <span class="arraysubs-download-usage__badge arraysubs-download-usage__badge--unlimited">
                            <?php esc_html_e('Unlimited', 'arraysubs'); ?>
                        </span>
                    </div>
                <?php else : ?>
                    <div class="arraysubs-download-usage__stats">
                        <div class="arraysubs-download-usage__progress">
                            <?php
                            $arraysubs_percentage = $arraysubs_usage['limit'] > 0
                                ? min(100, ($arraysubs_usage['used'] / $arraysubs_usage['limit']) * 100)
                                : 0;
                            ?>
                            <div class="arraysubs-download-usage__progress-bar">
                                <div
                                    class="arraysubs-download-usage__progress-fill <?php echo $arraysubs_percentage >= 90 ? 'arraysubs-download-usage__progress-fill--warning' : ''; ?>"
                                    style="width: <?php echo esc_attr($arraysubs_percentage); ?>%;"></div>
                            </div>
                        </div>
                        <div class="arraysubs-download-usage__counts">
                            <span class="arraysubs-download-usage__used">
                                <?php
                                printf(
                                    /* translators: %1$d: used count, %2$d: total limit */
                                    esc_html__('%1$d of %2$d used', 'arraysubs'),
                                    intval($arraysubs_usage['used']),
                                    intval($arraysubs_usage['limit'])
                                );
                                ?>
                            </span>
                            <span class="arraysubs-download-usage__remaining">
                                <?php
                                printf(
                                    /* translators: %d: remaining count */
                                    esc_html__('%d remaining', 'arraysubs'),
                                    intval($arraysubs_usage['remaining'])
                                );
                                ?>
                            </span>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
    </div>
</div>