<script setup lang="ts">
import { ref, computed, watch, nextTick } from 'vue'
import VoiceMicButton from './VoiceMicButton.vue'

const props = defineProps<{
  processing?: boolean
  terminalReady?: boolean | null  // null = no terminal, false = starting, true = ready
  voiceTranscript?: string
  isRecording?: boolean
  voiceMode?: 'web' | 'whisper'
  whisperStatus?: 'offline' | 'loading' | 'ready'
  maxLines?: number
}>()

const maxH = computed(() => {
  const lines = props.maxLines ?? 6
  return lines <= 1 ? '1.5em' : `${lines * 1.5}em`
})

const emit = defineEmits<{
  send: [message: string]
  startRecording: []
  stopRecording: []
}>()

const input = ref('')

// terminalReady: null = no terminal (show "no terminal"), false = starting, true = ready
const terminalStarting = computed(() => props.terminalReady === false) // explicitly false, not null
const noTerminal = computed(() => props.terminalReady === null)
const canSend = computed(() => props.terminalReady === true && !props.processing)
const isDisabled = computed(() => !input.value.trim() || !canSend.value)

function handleSend() {
  const msg = input.value.trim()
  if (!msg || !canSend.value) return
  emit('send', msg)
  input.value = ''
}

function handleKeydown(e: KeyboardEvent) {
  if (e.key === 'Enter' && e.ctrlKey) {
    e.preventDefault()
    const ta = e.target as HTMLTextAreaElement
    const start = ta.selectionStart
    const end = ta.selectionEnd
    input.value = input.value.slice(0, start) + '\n' + input.value.slice(end)
    nextTick(() => {
      ta.selectionStart = ta.selectionEnd = start + 1
    })
    return
  }
  if (e.key === 'Enter' && !e.shiftKey) {
    e.preventDefault()
    handleSend()
  }
}

// Fill textarea with voice transcript
watch(() => props.voiceTranscript, (newText) => {
  if (newText && newText.trim()) {
    input.value = newText
  }
})
</script>

<template>
  <div class="user-input">
    <div v-if="terminalStarting" class="processing-bar starting">
      <span class="processing-dots">
        <span class="dot"></span><span class="dot"></span><span class="dot"></span>
      </span>
      <span>Starting terminal...</span>
    </div>
    <div v-else-if="processing" class="processing-bar">
      <span class="processing-dots">
        <span class="dot"></span><span class="dot"></span><span class="dot"></span>
      </span>
      <span>Agent is processing...</span>
    </div>
    <div class="input-container" :class="{ disabled: !canSend }">
      <textarea
        v-model="input"
        class="input-field"
        :style="{ maxHeight: maxH }"
        :placeholder="terminalStarting ? 'Starting terminal...' : noTerminal ? 'No terminal — use + to create session' : processing ? 'Wait for agent to finish...' : 'Continue this conversation...'"
        rows="1"
        :disabled="!canSend"
        @keydown="handleKeydown"
      />
      <VoiceMicButton
        v-if="voiceMode"
        :is-recording="isRecording ?? false"
        :voice-mode="voiceMode"
        :whisper-status="whisperStatus ?? 'offline'"
        :disabled="!canSend"
        @start="emit('startRecording')"
        @stop="emit('stopRecording')"
      />
      <button
        class="send-btn"
        :disabled="isDisabled"
        @click="handleSend"
        title="Send prompt (resumes this session)"
      >
        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
          <line x1="22" y1="2" x2="11" y2="13" />
          <polygon points="22 2 15 22 11 13 2 9 22 2" />
        </svg>
      </button>
    </div>
  </div>
</template>

<style scoped>
.user-input {
  padding: 0.5rem 0.75rem 0.15rem;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border-color);
  flex-shrink: 0;
}

.processing-bar {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0 0.25rem 0.35rem;
  font-size: 10px;
  color: var(--accent);
}

.processing-bar.starting {
  color: var(--text-muted, #888);
}

.processing-dots {
  display: flex;
  gap: 3px;
}

.processing-dots .dot {
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse-dot 1.2s ease-in-out infinite;
}

.processing-dots .dot:nth-child(2) { animation-delay: 0.15s; }
.processing-dots .dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes pulse-dot {
  0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1); }
}

.input-container {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 0.4rem 0.5rem;
  transition: border-color 0.15s, opacity 0.15s;
}

.input-container:focus-within {
  border-color: var(--accent);
}

.input-container.disabled {
  opacity: 0.5;
}

.input-field {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-primary);
  font-size: 13px;
  line-height: 1.5;
  resize: none;
  field-sizing: content;
  min-height: 1lh;
  overflow-y: auto;
  padding: 0.15rem 0.25rem;
  font-family: inherit;
}

.input-field::placeholder {
  color: var(--text-muted);
}

.input-field:disabled {
  cursor: not-allowed;
}

.send-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  background: var(--accent);
  border: none;
  border-radius: 6px;
  color: white;
  cursor: pointer;
  flex-shrink: 0;
  transition: all 0.15s;
}

.send-btn:hover:not(:disabled) {
  filter: brightness(1.15);
}

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