<script setup lang="ts">
defineProps<{
  modelValue: string
  loading: boolean
}>()

const emit = defineEmits<{
  'update:modelValue': [value: string]
  execute: []
}>()

function onInput(e: Event) {
  emit('update:modelValue', (e.target as HTMLTextAreaElement).value)
}

function onKeydown(e: KeyboardEvent) {
  if (e.ctrlKey && e.key === 'Enter') {
    emit('execute')
  }
}
</script>

<template>
  <div class="query-editor">
    <textarea
      :value="modelValue"
      @input="onInput"
      @keydown="onKeydown"
      placeholder="SELECT * FROM themes LIMIT 10;"
    ></textarea>
    <div class="query-actions">
      <span class="hint">Ctrl+Enter to execute. Only SELECT queries allowed.</span>
      <button class="btn-primary" @click="emit('execute')" :disabled="loading">
        {{ loading ? 'Running...' : 'Execute' }}
      </button>
    </div>
  </div>
</template>

<style scoped>
.query-editor {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.query-editor textarea {
  width: 100%;
  min-height: 120px;
  padding: 1rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 0.9rem;
  resize: vertical;
}

.query-editor textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.query-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.hint {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.btn-primary {
  padding: 0.5rem 1.25rem;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
</style>
