{% extends "base.html" %}

{% block content %}
<div class="space-y-6" x-data="{
    selectedJobId: '{{ selected_job_id or '' }}',
    sqlQuery: `{{ sql | e }}`,
    runningQuery: null,
    runQuery(name, form) {
        if (!this.selectedJobId) {
            alert('Please select a job first');
            return;
        }
        this.runningQuery = name;
        setTimeout(() => form.submit(), 100);
    }
}">
    <!-- Header -->
    <div class="flex items-center justify-between">
        <div>
            <h1 class="text-2xl font-bold text-white flex items-center gap-2">
                <svg class="w-6 h-6 text-redis-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 9l3 3-3 3m5 0h3M5 20h14a2 2 0 002-2V6a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" />
                </svg>
                SQL Query
            </h1>
            <p class="text-slate-400 text-sm mt-1">
                Run custom SQL queries on captured Redis commands
            </p>
        </div>
        
        <!-- Job Selector -->
        <div class="flex items-center gap-3">
            <label class="text-sm text-slate-400">Job:</label>
            <select x-model="selectedJobId" 
                    class="px-3 py-1.5 bg-slate-800/50 border border-slate-700/50 rounded-lg text-white text-sm focus:outline-none focus:border-redis-500/50 min-w-[280px]"
                    :class="!selectedJobId && 'border-amber-500/50'">
                <option value="">-- Select a Job --</option>
                {% for job in jobs %}
                <option value="{{ job.id }}" {% if selected_job_id == job.id %}selected{% endif %}>
                    {{ job.name or job.replication_group_id }} ({{ job.created_at.strftime('%m/%d %H:%M') }}) - {{ job.total_commands|format_number }} cmds
                </option>
                {% endfor %}
            </select>
        </div>
    </div>
    
    <!-- No Job Selected Warning -->
    <div x-show="!selectedJobId" class="glass-card rounded-xl p-4 border-l-4 border-amber-500 bg-amber-500/10">
        <div class="flex items-center gap-3">
            <svg class="w-5 h-5 text-amber-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
            </svg>
            <p class="text-amber-200 text-sm">Select a job from the dropdown above to run queries. Each job has its own database.</p>
        </div>
    </div>

    <!-- Quick Query Buttons -->
    <div class="glass-card rounded-xl p-4" x-show="selectedJobId" x-transition>
        <div class="flex items-center justify-between mb-3">
            <h3 class="text-sm font-medium text-slate-400">Quick Queries</h3>
            <span class="text-xs text-slate-500">Click to run instantly</span>
        </div>
        <div class="flex flex-wrap gap-2">
            <form method="GET" class="inline" x-ref="form1" @submit.prevent="runQuery('patterns', $refs.form1)">
                <input type="hidden" name="job_id" :value="selectedJobId">
                <input type="hidden" name="sql" value="SELECT key_pattern, COUNT(*) as cnt FROM redis_commands WHERE key_pattern IS NOT NULL GROUP BY key_pattern ORDER BY cnt DESC LIMIT 20">
                <button type="submit" class="px-3 py-1.5 rounded-lg bg-slate-700/50 text-slate-300 hover:bg-slate-700 hover:text-white transition-all text-sm flex items-center gap-1.5 min-w-[100px] justify-center" :class="runningQuery === 'patterns' && 'bg-emerald-500/20 text-emerald-400'">
                    <svg x-show="runningQuery !== 'patterns'" class="w-3.5 h-3.5 text-emerald-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z" />
                    </svg>
                    <svg x-show="runningQuery === 'patterns'" x-cloak class="w-3.5 h-3.5 text-emerald-400 animate-spin" fill="none" viewBox="0 0 24 24">
                        <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                        <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                    </svg>
                    Key Patterns
                </button>
            </form>
            
            <form method="GET" class="inline" x-ref="form2" @submit.prevent="runQuery('shard', $refs.form2)">
                <input type="hidden" name="job_id" :value="selectedJobId">
                <input type="hidden" name="sql" value="SELECT shard_name, COUNT(*) as cnt FROM redis_commands GROUP BY shard_name ORDER BY cnt DESC">
                <button type="submit" class="px-3 py-1.5 rounded-lg bg-slate-700/50 text-slate-300 hover:bg-slate-700 hover:text-white transition-all text-sm flex items-center gap-1.5 min-w-[100px] justify-center" :class="runningQuery === 'shard' && 'bg-emerald-500/20 text-emerald-400'">
                    <svg x-show="runningQuery !== 'shard'" class="w-3.5 h-3.5 text-emerald-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z" />
                    </svg>
                    <svg x-show="runningQuery === 'shard'" x-cloak class="w-3.5 h-3.5 text-emerald-400 animate-spin" fill="none" viewBox="0 0 24 24">
                        <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                        <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                    </svg>
                    Per Shard
                </button>
            </form>
            
            <form method="GET" class="inline" x-ref="form3" @submit.prevent="runQuery('topkeys', $refs.form3)">
                <input type="hidden" name="job_id" :value="selectedJobId">
                <input type="hidden" name="sql" value="SELECT key, COUNT(*) as cnt, MAX(key_size_bytes) as size FROM redis_commands WHERE key IS NOT NULL GROUP BY key ORDER BY cnt DESC LIMIT 30">
                <button type="submit" class="px-3 py-1.5 rounded-lg bg-slate-700/50 text-slate-300 hover:bg-slate-700 hover:text-white transition-all text-sm flex items-center gap-1.5 min-w-[100px] justify-center" :class="runningQuery === 'topkeys' && 'bg-emerald-500/20 text-emerald-400'">
                    <svg x-show="runningQuery !== 'topkeys'" class="w-3.5 h-3.5 text-emerald-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z" />
                    </svg>
                    <svg x-show="runningQuery === 'topkeys'" x-cloak class="w-3.5 h-3.5 text-emerald-400 animate-spin" fill="none" viewBox="0 0 24 24">
                        <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                        <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                    </svg>
                    Top Keys
                </button>
            </form>
            
            <form method="GET" class="inline" x-ref="form4" @submit.prevent="runQuery('large', $refs.form4)">
                <input type="hidden" name="job_id" :value="selectedJobId">
                <input type="hidden" name="sql" value="SELECT key, key_size_bytes, shard_name FROM redis_commands WHERE key_size_bytes IS NOT NULL ORDER BY key_size_bytes DESC LIMIT 20">
                <button type="submit" class="px-3 py-1.5 rounded-lg bg-slate-700/50 text-slate-300 hover:bg-slate-700 hover:text-white transition-all text-sm flex items-center gap-1.5 min-w-[100px] justify-center" :class="runningQuery === 'large' && 'bg-emerald-500/20 text-emerald-400'">
                    <svg x-show="runningQuery !== 'large'" class="w-3.5 h-3.5 text-emerald-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z" />
                    </svg>
                    <svg x-show="runningQuery === 'large'" x-cloak class="w-3.5 h-3.5 text-emerald-400 animate-spin" fill="none" viewBox="0 0 24 24">
                        <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                        <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                    </svg>
                    Large Keys
                </button>
            </form>
            
            <form method="GET" class="inline" x-ref="form5" @submit.prevent="runQuery('commands', $refs.form5)">
                <input type="hidden" name="job_id" :value="selectedJobId">
                <input type="hidden" name="sql" value="SELECT command, COUNT(*) as cnt FROM redis_commands GROUP BY command ORDER BY cnt DESC">
                <button type="submit" class="px-3 py-1.5 rounded-lg bg-slate-700/50 text-slate-300 hover:bg-slate-700 hover:text-white transition-all text-sm flex items-center gap-1.5 min-w-[100px] justify-center" :class="runningQuery === 'commands' && 'bg-emerald-500/20 text-emerald-400'">
                    <svg x-show="runningQuery !== 'commands'" class="w-3.5 h-3.5 text-emerald-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z" />
                    </svg>
                    <svg x-show="runningQuery === 'commands'" x-cloak class="w-3.5 h-3.5 text-emerald-400 animate-spin" fill="none" viewBox="0 0 24 24">
                        <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                        <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                    </svg>
                    Commands
                </button>
            </form>
            
            <form method="GET" class="inline" x-ref="form6" @submit.prevent="runQuery('clients', $refs.form6)">
                <input type="hidden" name="job_id" :value="selectedJobId">
                <input type="hidden" name="sql" value="SELECT client_ip, COUNT(*) as cnt FROM redis_commands WHERE client_ip IS NOT NULL GROUP BY client_ip ORDER BY cnt DESC LIMIT 15">
                <button type="submit" class="px-3 py-1.5 rounded-lg bg-slate-700/50 text-slate-300 hover:bg-slate-700 hover:text-white transition-all text-sm flex items-center gap-1.5 min-w-[100px] justify-center" :class="runningQuery === 'clients' && 'bg-emerald-500/20 text-emerald-400'">
                    <svg x-show="runningQuery !== 'clients'" class="w-3.5 h-3.5 text-emerald-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z" />
                    </svg>
                    <svg x-show="runningQuery === 'clients'" x-cloak class="w-3.5 h-3.5 text-emerald-400 animate-spin" fill="none" viewBox="0 0 24 24">
                        <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                        <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                    </svg>
                    Client IPs
                </button>
            </form>
            
            <form method="GET" class="inline" x-ref="form7" @submit.prevent="runQuery('recent', $refs.form7)">
                <input type="hidden" name="job_id" :value="selectedJobId">
                <input type="hidden" name="sql" value="SELECT datetime_utc, command, key, shard_name, client_ip FROM redis_commands ORDER BY timestamp DESC LIMIT 50">
                <button type="submit" class="px-3 py-1.5 rounded-lg bg-slate-700/50 text-slate-300 hover:bg-slate-700 hover:text-white transition-all text-sm flex items-center gap-1.5 min-w-[100px] justify-center" :class="runningQuery === 'recent' && 'bg-emerald-500/20 text-emerald-400'">
                    <svg x-show="runningQuery !== 'recent'" class="w-3.5 h-3.5 text-emerald-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z" />
                    </svg>
                    <svg x-show="runningQuery === 'recent'" x-cloak class="w-3.5 h-3.5 text-emerald-400 animate-spin" fill="none" viewBox="0 0 24 24">
                        <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                        <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                    </svg>
                    Recent
                </button>
            </form>
        </div>
    </div>

    <!-- SQL Editor -->
    <form method="GET" class="space-y-4" x-show="selectedJobId" x-transition>
        <input type="hidden" name="job_id" :value="selectedJobId">
        <div class="glass-card rounded-xl p-6">
            <div class="flex items-center justify-between mb-2">
                <label class="block text-sm font-medium text-slate-300">Custom SQL</label>
                <div class="text-xs text-slate-500">
                    Tables: <code class="text-slate-400">redis_commands</code>, <code class="text-slate-400">key_size_cache</code>
                </div>
            </div>
            <textarea name="sql" 
                      rows="4"
                      x-model="sqlQuery"
                      placeholder="SELECT * FROM redis_commands LIMIT 10"
                      class="w-full px-4 py-3 bg-slate-800/50 border border-slate-700/50 rounded-xl text-white font-mono text-sm placeholder-slate-500 focus:outline-none focus:border-redis-500/50 focus:ring-1 focus:ring-redis-500/50 resize-y">{{ sql }}</textarea>
            
            <div class="flex items-center justify-between mt-3">
                <p class="text-xs text-slate-500">Only SELECT queries allowed • Queries run against the selected job's database</p>
                <button type="submit" class="px-5 py-2 rounded-lg bg-redis-500 text-white font-medium hover:bg-redis-600 transition-colors flex items-center gap-2">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z" />
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                    </svg>
                    Run
                </button>
            </div>
        </div>
    </form>

    {% if error %}
    <div class="glass-card rounded-xl p-4 border-l-4 border-redis-500 bg-redis-500/10">
        <div class="flex items-start gap-3">
            <svg class="w-5 h-5 text-redis-400 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
            </svg>
            <div>
                <h4 class="text-redis-300 font-medium">Query Error</h4>
                <p class="text-redis-200 text-sm font-mono mt-1">{{ error }}</p>
            </div>
        </div>
    </div>
    {% endif %}

    {% if results is not none %}
    <div class="glass-card rounded-xl overflow-hidden">
        <div class="px-6 py-4 border-b border-slate-700/50 flex items-center justify-between">
            <h3 class="text-lg font-semibold text-white">Results</h3>
            <span class="text-sm text-slate-500">{{ results|length }} rows</span>
        </div>
        
        {% if results %}
        <div class="overflow-x-auto max-h-[500px] overflow-y-auto">
            <table class="w-full">
                <thead class="sticky top-0 bg-slate-900">
                    <tr class="border-b border-slate-700/50 text-left text-sm text-slate-400">
                        {% for col in columns %}
                        <th class="px-6 py-3 font-medium whitespace-nowrap">{{ col }}</th>
                        {% endfor %}
                    </tr>
                </thead>
                <tbody>
                    {% for row in results %}
                    <tr class="border-b border-slate-800/50 hover:bg-slate-800/30 text-sm">
                        {% for col in columns %}
                        <td class="px-6 py-3 font-mono text-slate-300 max-w-xs truncate" title="{{ row[col] }}">
                            {% if row[col] is none %}
                            <span class="text-slate-600">NULL</span>
                            {% else %}
                            {{ row[col] }}
                            {% endif %}
                        </td>
                        {% endfor %}
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% else %}
        <div class="p-8 text-center text-slate-400">Query returned no results</div>
        {% endif %}
    </div>
    {% endif %}
</div>
{% endblock %}
