{% if not has_packages %}
    <p>{{ __('You have no licenses yet.') }}</p>
{% else %}
    <div class="woocommerce-licenses">
        {% for package in packages %}
            <div class="license-package">
                <div class="package-header">
                    <div class="package-title">
                        <h3>
                            {% if package.product_url %}
                                <a href="{{ esc_url(package.product_url) }}">{{ esc_html(package.product_name) }}</a>
                            {% else %}
                                {{ esc_html(package.product_name) }}
                            {% endif %}
                        </h3>
                        <span class="package-order">
                            {{ __('Order') }}
                            {% if package.order_url %}
                                <a href="{{ esc_url(package.order_url) }}">#{{ esc_html(package.order_number) }}</a>
                            {% else %}
                                #{{ esc_html(package.order_number) }}
                            {% endif %}
                        </span>
                    </div>
                    <span class="package-license-count">
                        {{ package.licenses|length }} {{ package.licenses|length == 1 ? __('License') : __('Licenses') }}
                    </span>
                </div>

                <div class="package-licenses">
                    {% for license in package.licenses %}
                        <div class="license-entry license-entry-{{ esc_attr(license.status) }}">
                            <div class="license-row-primary">
                                <div class="license-key-group">
                                    <code class="license-key">{{ esc_html(license.license_key) }}</code>
                                    <span class="license-status license-status-{{ esc_attr(license.status) }}">
                                        {{ esc_html(license.status)|capitalize }}
                                    </span>
                                </div>
                                <div class="license-actions">
                                    <button type="button" class="copy-license-btn" data-license-key="{{ esc_attr(license.license_key) }}" title="{{ __('Copy to clipboard') }}">
                                        <span class="dashicons dashicons-clipboard"></span>
                                    </button>
                                    {% if license.is_transferable %}
                                        <button type="button" class="wclp-transfer-btn"
                                                data-license-id="{{ license.id }}"
                                                data-current-domain="{{ esc_attr(license.domain) }}"
                                                title="{{ __('Transfer to new domain') }}">
                                            <span class="dashicons dashicons-randomize"></span>
                                        </button>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="license-row-secondary">
                                <span class="license-meta-item license-domain">
                                    <span class="dashicons dashicons-admin-site-alt3"></span>
                                    {{ esc_html(license.domain) }}
                                </span>
                                <span class="license-meta-item license-expiry">
                                    <span class="dashicons dashicons-calendar-alt"></span>
                                    {% if license.expires_at %}
                                        {{ license.expires_at|date('Y-m-d') }}
                                    {% else %}
                                        <span class="lifetime">{{ __('Lifetime') }}</span>
                                    {% endif %}
                                </span>
                            </div>
                            {% if signing_enabled and license.customer_secret %}
                                <div class="license-row-secret">
                                    <button type="button" class="secret-toggle" aria-expanded="false">
                                        <span class="dashicons dashicons-lock"></span>
                                        {{ __('API Verification Secret') }}
                                        <span class="dashicons dashicons-arrow-down-alt2 toggle-arrow"></span>
                                    </button>
                                    <div class="secret-content" style="display: none;">
                                        <p class="secret-description">
                                            {{ __('Use this secret to verify signed API responses. Keep it secure.') }}
                                        </p>
                                        <div class="secret-value-wrapper">
                                            <code class="secret-value">{{ esc_html(license.customer_secret) }}</code>
                                            <button type="button" class="copy-secret-btn" data-secret="{{ esc_attr(license.customer_secret) }}" title="{{ __('Copy to clipboard') }}">
                                                <span class="dashicons dashicons-clipboard"></span>
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            {% endif %}
                        </div>
                    {% endfor %}
                </div>

                {% if package.downloads is defined and package.downloads is not empty %}
                    <div class="package-downloads">
                        <h4>{{ __('Available Downloads') }}</h4>
                        <ul class="download-list">
                            {# Show only the latest version (first item) #}
                            {% set latest = package.downloads|first %}
                            <li class="download-item download-item-latest">
                                <div class="download-row-file">
                                    <a href="{{ esc_url(latest.download_url) }}" class="download-link">
                                        <span class="dashicons dashicons-download"></span>
                                        {{ esc_html(latest.filename ?: 'Version ' ~ latest.version) }}
                                    </a>
                                    <span class="download-version-badge">{{ __('Latest') }}</span>
                                </div>
                                <div class="download-row-meta">
                                    <span class="download-date">{{ esc_html(latest.released_at) }}</span>
                                    {% if latest.file_hash %}
                                        <span class="download-hash" title="{{ esc_attr(latest.file_hash) }}">
                                            <span class="dashicons dashicons-shield"></span>
                                            <code>{{ latest.file_hash[:12] }}...</code>
                                        </span>
                                    {% endif %}
                                </div>
                            </li>
                        </ul>

                        {# Show older versions in collapsible if more than one version exists #}
                        {% if package.downloads|length > 1 %}
                            <div class="older-versions-section">
                                <button type="button" class="older-versions-toggle" aria-expanded="false">
                                    <span class="dashicons dashicons-arrow-down-alt2"></span>
                                    {{ __('Older versions') }} ({{ package.downloads|length - 1 }})
                                </button>
                                <ul class="download-list older-versions-list" style="display: none;">
                                    {% for download in package.downloads|slice(1) %}
                                        <li class="download-item">
                                            <div class="download-row-file">
                                                <a href="{{ esc_url(download.download_url) }}" class="download-link">
                                                    <span class="dashicons dashicons-download"></span>
                                                    {{ esc_html(download.filename ?: 'Version ' ~ download.version) }}
                                                </a>
                                            </div>
                                            <div class="download-row-meta">
                                                <span class="download-date">{{ esc_html(download.released_at) }}</span>
                                                {% if download.file_hash %}
                                                    <span class="download-hash" title="{{ esc_attr(download.file_hash) }}">
                                                        <span class="dashicons dashicons-shield"></span>
                                                        <code>{{ download.file_hash[:12] }}...</code>
                                                    </span>
                                                {% endif %}
                                            </div>
                                        </li>
                                    {% endfor %}
                                </ul>
                            </div>
                        {% endif %}
                    </div>
                {% endif %}
            </div>
        {% endfor %}
    </div>

    <!-- Transfer Modal -->
    <div id="wclp-transfer-modal" class="wclp-modal" style="display:none;">
        <div class="wclp-modal-overlay"></div>
        <div class="wclp-modal-content">
            <button type="button" class="wclp-modal-close" aria-label="{{ __('Close') }}">&times;</button>
            <h3>{{ __('Transfer License to New Domain') }}</h3>
            <form id="wclp-transfer-form">
                <input type="hidden" name="license_id" id="transfer-license-id" value="">

                <div class="wclp-form-row">
                    <label>{{ __('Current Domain') }}</label>
                    <p class="wclp-current-domain"><code id="transfer-current-domain"></code></p>
                </div>

                <div class="wclp-form-row">
                    <label for="transfer-new-domain">{{ __('New Domain') }}</label>
                    <input type="text" name="new_domain" id="transfer-new-domain"
                           placeholder="example.com" required
                           pattern="[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)+">
                    <p class="wclp-field-description">{{ __('Enter the new domain without http:// or www.') }}</p>
                </div>

                <div class="wclp-form-row wclp-form-actions">
                    <button type="submit" class="button wclp-btn-primary" id="wclp-transfer-submit">
                        {{ __('Transfer License') }}
                    </button>
                    <button type="button" class="button wclp-modal-cancel">{{ __('Cancel') }}</button>
                </div>

                <div id="wclp-transfer-message" class="wclp-message" style="display:none;"></div>
            </form>
        </div>
    </div>
{% endif %}
