{% extends "admin/dj_redis_panel/base.html" %}
{% load i18n admin_urls static %}


{% block breadcrumbs %}
<div class="breadcrumbs">
    <a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
    &rsaquo; <a href="{% url 'dj_redis_panel:index' %}">{% trans 'Redis Instances' %}</a>
    &rsaquo; <a href="{% url 'dj_redis_panel:instance_overview' instance_alias %}">{{ instance_alias }}</a>
    &rsaquo; {% trans 'Search Keys' %} (db:{{ selected_db }})
</div>
{% endblock %}


{% block content %}
        <!-- Pagination Mode Indicator -->
        <div class="pagination-mode-indicator">
            {% if use_cursor_pagination %}
                <span class="badge cursor-mode">
                    📄 {% trans 'CURSOR PAGINATION' %}
                </span>
            {% else %}
                <span class="badge page-mode">
                    🔢 {% trans 'PAGE PAGINATION' %}
                </span>
            {% endif %}
        </div>



<div class="module key-search">
    {% if success_message %}
    <div class="messagelist">
        <div class="success">{{ success_message }}</div>
    </div>
    {% endif %}
    
    {% if error_message %}
    <div class="messagelist">
        <div class="error">{{ error_message }}</div>
    </div>
    {% endif %}
    
    <!-- Search Form -->
    <div class="module">
        <h2>{% trans 'Search Redis Keys' %}</h2>
        <br>
        <form method="get" class="search-form">
            <div class="form-row search-form-row">
                <div class="search-form-fields">
                    <div class="search-field-group">
                        <label for="q">{% trans 'Key Pattern:' %}</label>
                        <input type="text" id="q" name="q" value="{{ search_query }}" 
                               placeholder="*" class="vTextField" style="width: 300px;">
                    </div>
                    <div class="search-field-group">
                        <label for="per_page">{% trans 'Per page:' %}</label>
                        <select id="per_page" name="per_page" class="vTextField">
                            <option value="10" {% if per_page == 10 %}selected{% endif %}>10</option>
                            <option value="25" {% if per_page == 25 %}selected{% endif %}>25</option>
                            <option value="50" {% if per_page == 50 %}selected{% endif %}>50</option>
                            <option value="100" {% if per_page == 100 %}selected{% endif %}>100</option>
                        </select>
                    </div>
                    <div class="search-field-group">
                        <label>&nbsp;</label> <!-- Empty label for alignment -->
                        <input type="submit" value="{% trans 'Search' %}" class="default">
                    </div>
                </div>
                <div class="search-help">
                    <div class="help">{% trans 'Use * for wildcards, e.g., user:* or *:cache' %}</div>
                </div>
            </div>
        </form>
    </div>

    <!-- Error Display -->
    {% if error_message %}
    <div class="module">
        <div class="errors">
            <p><strong>{% trans 'Error:' %}</strong> {{ error_message }}</p>
        </div>
    </div>
    {% endif %}

    <!-- Results Summary -->
    {% if not error_message %}
    <div class="module">
        <h2>{% trans 'Search Results' %}</h2>
        <br>
        <p class="help">
            {% if use_cursor_pagination %}
                {% if showing_keys > 0 %}
                    {% blocktrans with count=showing_keys %}Showing {{ count }} keys{% endblocktrans %}
                    {% if has_next %} - <span class="quiet">{% trans 'more available' %}</span>{% endif %}
                    {% if current_cursor > 0 %}<br><em class="quiet">{% trans 'Cursor position' %}: {{ current_cursor }}</em>{% endif %}
                {% else %}
                    {% trans 'No keys found matching your search pattern.' %}
                {% endif %}
            {% else %}
                {% if total_keys > 0 %}
                    {% blocktrans with total=total_keys start=start_index end=end_index %}Found {{ total }} keys, showing {{ start }} to {{ end }}{% endblocktrans %}
                    {% if total_keys >= 100000 %}
                        <br><em>{% trans 'Search limited to first 100,000 matching keys. Refine your search pattern for more specific results.' %}</em>
                    {% endif %}
                {% else %}
                    {% trans 'No keys found matching your search pattern.' %}
                {% endif %}
            {% endif %}
        </p>
    </div>
    {% endif %}

    <!-- Results Table -->
    {% if keys_data and not error_message %}
    <div class="results">
        <table id="result_list">
            <thead>
                <tr>
                    <th scope="col">{% trans 'Key' %}</th>
                    <th scope="col">{% trans 'Type' %}</th>
                    <th scope="col">{% trans 'Size/Length' %}</th>
                    <th scope="col">{% trans 'TTL' %}</th>
                    <th scope="col">{% trans 'Actions' %}</th>
                </tr>
            </thead>
            <tbody>
                {% for key_info in keys_data %}
                <tr class="{% cycle 'row1' 'row2' %}">
                    <td>
                        <strong>{{ key_info.key }}</strong>
                    </td>
                    <td>
                        <span class="key-type key-type-{{ key_info.type }}">
                            {{ key_info.type|upper }}
                        </span>
                    </td>
                    <td>
                        {% if key_info.type == 'string' %}
                            {{ key_info.size }} {% trans 'bytes' %}
                        {% else %}
                            {{ key_info.size }} {% trans 'items' %}
                        {% endif %}
                    </td>
                    <td>
                        {% if key_info.ttl %}
                            {{ key_info.ttl }}s
                        {% else %}
                            <span class="quiet">{% trans 'No expiry' %}</span>
                        {% endif %}
                    </td>
                    <td>
                        <a href="{% url 'dj_redis_panel:key_detail' instance_alias selected_db key_info.key %}" class="viewlink">
                            {% trans 'View' %}
                        </a>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        
        <!-- Pagination using exact Django admin structure -->
        <p class="paginator" align="right">
        {% if use_cursor_pagination %}
            <!-- Cursor-based pagination: only previous/next navigation -->
            {% if current_cursor > 0 %}
                <a href="?{% if search_query != '*' %}q={{ search_query|urlencode }}&{% endif %}per_page={{ per_page }}&cursor=0" class="prev">{% trans 'first' %}</a>
            {% endif %}
            
            {% if has_next %}
                <a href="?{% if search_query != '*' %}q={{ search_query|urlencode }}&{% endif %}per_page={{ per_page }}&cursor={{ next_cursor }}" class="next">{% trans 'next' %}</a>
            {% endif %}
            
            {% if total_keys > 0 %}
                {% trans 'Cursor-based pagination' %} - {{ showing_keys }} {% trans 'keys shown' %}
                {% if has_next %} ({% trans 'more available' %}){% endif %}
            {% endif %}
        {% else %}
            <!-- Traditional page-based pagination -->
            {% if total_pages > 1 %}
                {% if has_previous %}
                    <a href="?{% if search_query != '*' %}q={{ search_query|urlencode }}&{% endif %}per_page={{ per_page }}&page={{ previous_page }}" class="prev">{% trans 'previous' %}</a>
                {% endif %}
                
                {% for num in page_range %}
                    {% if num == "..." %}
                        …
                    {% elif num == current_page %}
                        <span class="this-page">{{ num }}</span>
                    {% else %}
                        <a href="?{% if search_query != '*' %}q={{ search_query|urlencode }}&{% endif %}per_page={{ per_page }}&page={{ num }}">{{ num }}</a>
                    {% endif %}
                {% endfor %}
                
                {% if has_next %}
                    <a href="?{% if search_query != '*' %}q={{ search_query|urlencode }}&{% endif %}per_page={{ per_page }}&page={{ next_page }}" class="next">{% trans 'next' %}</a>
                {% endif %}
            {% endif %}
            {{ total_keys }} {% blocktrans count counter=total_keys %}key{% plural %}keys{% endblocktrans %}
        {% endif %}
        </p>
    </div>
    {% endif %}
</div>
{% endblock %}
