<div class="wrap wclp-dashboard">
    <h1>{{ __('License Dashboard') }}</h1>

    <div class="wclp-dashboard-stats">
        <!-- Overview Cards -->
        <div class="wclp-stat-cards">
            <div class="wclp-stat-card wclp-stat-total">
                <div class="wclp-stat-icon"><span class="dashicons dashicons-admin-network"></span></div>
                <div class="wclp-stat-content">
                    <span class="wclp-stat-number">{{ stats.total }}</span>
                    <span class="wclp-stat-label">{{ __('Total Licenses') }}</span>
                </div>
            </div>
            <div class="wclp-stat-card wclp-stat-active">
                <div class="wclp-stat-icon"><span class="dashicons dashicons-yes-alt"></span></div>
                <div class="wclp-stat-content">
                    <span class="wclp-stat-number">{{ stats.by_status.active }}</span>
                    <span class="wclp-stat-label">{{ __('Active') }}</span>
                </div>
            </div>
            <div class="wclp-stat-card wclp-stat-inactive">
                <div class="wclp-stat-icon"><span class="dashicons dashicons-marker"></span></div>
                <div class="wclp-stat-content">
                    <span class="wclp-stat-number">{{ stats.by_status.inactive }}</span>
                    <span class="wclp-stat-label">{{ __('Inactive') }}</span>
                </div>
            </div>
            <div class="wclp-stat-card wclp-stat-expired">
                <div class="wclp-stat-icon"><span class="dashicons dashicons-calendar-alt"></span></div>
                <div class="wclp-stat-content">
                    <span class="wclp-stat-number">{{ stats.by_status.expired }}</span>
                    <span class="wclp-stat-label">{{ __('Expired') }}</span>
                </div>
            </div>
            <div class="wclp-stat-card wclp-stat-revoked">
                <div class="wclp-stat-icon"><span class="dashicons dashicons-dismiss"></span></div>
                <div class="wclp-stat-content">
                    <span class="wclp-stat-number">{{ stats.by_status.revoked }}</span>
                    <span class="wclp-stat-label">{{ __('Revoked') }}</span>
                </div>
            </div>
        </div>

        <!-- Alert: Expiring Soon -->
        {% if stats.expiring_soon > 0 %}
        <div class="notice notice-warning">
            <p>
                <span class="dashicons dashicons-warning"></span>
                <strong>{{ __('Attention:') }}</strong>
                {{ stats.expiring_soon }} {{ stats.expiring_soon == 1 ? __('license is') : __('licenses are') }}
                {{ __('expiring within the next 30 days.') }}
                <a href="{{ admin_url }}?page=wc-licenses">{{ __('View Licenses') }}</a>
            </p>
        </div>
        {% endif %}

        <!-- Secondary Stats -->
        <div class="wclp-stat-row">
            <div class="wclp-stat-box">
                <h3>{{ __('License Types') }}</h3>
                <table class="widefat striped">
                    <tbody>
                        <tr>
                            <td>{{ __('Lifetime Licenses') }}</td>
                            <td class="wclp-stat-value">{{ stats.lifetime }}</td>
                        </tr>
                        <tr>
                            <td>{{ __('Time-limited Licenses') }}</td>
                            <td class="wclp-stat-value">{{ stats.expiring }}</td>
                        </tr>
                        <tr>
                            <td>{{ __('Expiring Soon (30 days)') }}</td>
                            <td class="wclp-stat-value {% if stats.expiring_soon > 0 %}wclp-warning{% endif %}">
                                {{ stats.expiring_soon }}
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>

            <div class="wclp-stat-box">
                <h3>{{ __('Top Products by Licenses') }}</h3>
                {% if stats.by_product is empty %}
                    <p class="description">{{ __('No license data available yet.') }}</p>
                {% else %}
                    <table class="widefat striped">
                        <thead>
                            <tr>
                                <th>{{ __('Product') }}</th>
                                <th class="wclp-stat-value">{{ __('Licenses') }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for product in stats.by_product %}
                            <tr>
                                <td>{{ esc_html(product.product_name) }}</td>
                                <td class="wclp-stat-value">{{ product.count }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                {% endif %}
            </div>

            <div class="wclp-stat-box">
                <h3>{{ __('Top Domains') }}</h3>
                {% if stats.top_domains is empty %}
                    <p class="description">{{ __('No license data available yet.') }}</p>
                {% else %}
                    <table class="widefat striped">
                        <thead>
                            <tr>
                                <th>{{ __('Domain') }}</th>
                                <th class="wclp-stat-value">{{ __('Licenses') }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for domain in stats.top_domains %}
                            <tr>
                                <td><code>{{ esc_html(domain.domain) }}</code></td>
                                <td class="wclp-stat-value">{{ domain.count }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                {% endif %}
            </div>
        </div>

        <!-- Monthly Chart -->
        <div class="wclp-stat-box wclp-stat-full">
            <h3>{{ __('Licenses Created (Last 12 Months)') }}</h3>
            {% if stats.monthly is empty %}
                <p class="description">{{ __('No license data available yet.') }}</p>
            {% else %}
                <div class="wclp-chart-container">
                    <div class="wclp-bar-chart" id="wclp-monthly-chart">
                        {% set max_value = 1 %}
                        {% for count in stats.monthly %}
                            {% if count > max_value %}
                                {% set max_value = count %}
                            {% endif %}
                        {% endfor %}
                        {% for month, count in stats.monthly %}
                            <div class="wclp-bar-wrapper">
                                <div class="wclp-bar" style="height: {{ (count / max_value * 100)|round }}%;" title="{{ count }} {{ __('licenses') }}">
                                    <span class="wclp-bar-value">{{ count }}</span>
                                </div>
                                <span class="wclp-bar-label">{{ month|date('M Y') }}</span>
                            </div>
                        {% endfor %}
                    </div>
                </div>
            {% endif %}
        </div>
    </div>

    <!-- Quick Actions -->
    <div class="wclp-dashboard-actions">
        <h2>{{ __('Quick Actions') }}</h2>
        <div class="wclp-action-buttons">
            <a href="{{ admin_url }}?page=wc-licenses" class="button button-primary">
                <span class="dashicons dashicons-admin-network"></span>
                {{ __('Manage Licenses') }}
            </a>
            <a href="{{ admin_url }}?page=wc-licenses&action=export_csv" class="button">
                <span class="dashicons dashicons-download"></span>
                {{ __('Export to CSV') }}
            </a>
            <a href="{{ admin_url }}?page=wc-settings&tab=licensed_product" class="button">
                <span class="dashicons dashicons-admin-generic"></span>
                {{ __('Settings') }}
            </a>
        </div>
    </div>
</div>
