<script setup lang="ts">
import { Loader2, Save, Check, AlertCircle } from '@lucide/vue'

defineProps<{
  fileName: string | null
  fileSize: string
  fileModified: string
  content: string
  dirty: boolean
  saving: boolean
  saveStatus: 'idle' | 'saved' | 'error'
  saveMessage: string
  backupStatus: string
  reloadStatus: string
  reloadMessage: string
  contentHash: string
  verified: boolean
  readOnly: boolean
}>()

defineEmits<{
  updateContent: [value: string]
  save: []
}>()

</script>

<template>
  <div class="editor-panel">
    <!-- File info header -->
    <div class="editor-header">
      <div class="editor-file-info">
        <span class="editor-filename">{{ fileName || '—' }}</span>
        <span v-if="fileName" class="editor-file-meta">
          {{ fileSize }}
          <span class="meta-sep">·</span>
          {{ fileModified }}
        </span>
        <code v-if="contentHash" class="editor-hash" :title="contentHash">
          sha256:{{ contentHash.slice(0, 12) }}
        </code>
      </div>

      <!-- Save button & status -->
      <div class="editor-actions">
        <span v-if="saveStatus === 'saved'" class="save-indicator success">
          <Check :size="14" />
          {{ saveMessage }}
        </span>
        <span v-if="saveStatus === 'error'" class="save-indicator error">
          <AlertCircle :size="14" />
          {{ saveMessage }}
        </span>
        <button
          type="button"
          class="save-btn"
          :class="{ dirty, saving }"
          :disabled="readOnly || !dirty || saving"
          @click="$emit('save')"
        >
          <Loader2 v-if="saving" :size="14" class="spin" />
          <Save v-else :size="14" />
          Speichern
        </button>
      </div>
    </div>

    <div v-if="reloadMessage" class="editor-health">
      <span class="health-pill" :class="verified ? 'verified' : reloadStatus">
        {{ verified ? 'Read-back verifiziert' : 'Runtime-Aktivierung unbestätigt' }}
      </span>
      <span class="health-note">{{ reloadMessage }}</span>
    </div>

    <!-- Text editor -->
    <textarea
      class="config-editor"
      :aria-label="fileName ? `Edit ${fileName}` : 'Edit agent configuration'"
      :value="content"
      :readonly="readOnly"
      @input="$emit('updateContent', ($event.target as HTMLTextAreaElement).value)"
      spellcheck="false"
      wrap="off"
    ></textarea>
  </div>
</template>

<style scoped>
.editor-panel {
  border: 1px solid var(--line);
  border-top: none;
  border-radius: 0 0 10px 10px;
  overflow: hidden;
  background: var(--glass);
}

.editor-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: color-mix(in srgb, var(--tx) 2%, transparent);
  border-bottom: 1px solid var(--line);
  gap: 12px;
}
.editor-health {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--tx) 1.5%, transparent);
  color: var(--tx-3);
  font-size: 10.5px;
  flex-wrap: wrap;
}
.editor-file-info {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.editor-filename {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--tx);
  white-space: nowrap;
}
.editor-file-meta {
  font-size: 10px;
  color: var(--tx-3);
  white-space: nowrap;
}
.editor-hash {
  max-width: 180px;
  overflow: hidden;
  color: var(--tx-3);
  font-family: var(--font-mono-v2);
  font-size: 10px;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.meta-sep {
  margin: 0 4px;
  color: var(--line-3);
}
.editor-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.save-indicator {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 10.5px;
  white-space: nowrap;
}
.save-indicator.success {
  color: var(--st-work);
}
.save-indicator.error {
  color: var(--st-block);
  max-width: 240px;
}

.save-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: color-mix(in srgb, var(--a-mid) 8%, transparent);
  color: var(--a-mid);
  font-size: 10.5px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
  font-family: inherit;
  line-height: 1;
}
.save-btn:hover:not(:disabled) {
  background: color-mix(in srgb, var(--a-mid) 14%, transparent);
  border-color: var(--line-3);
}
.save-btn.dirty {
  background: color-mix(in srgb, var(--a-mid) 18%, transparent);
  border-color: var(--a-mid);
  color: var(--tx);
}
.save-btn.saving {
  opacity: 0.7;
  cursor: wait;
}
.save-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.health-pill {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 8px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.health-pill.created {
  color: var(--st-work);
  border-color: color-mix(in srgb, var(--st-work) 30%, transparent);
}
.health-pill.not_applicable {
  color: var(--st-queue);
  border-color: color-mix(in srgb, var(--st-queue) 25%, transparent);
}
.health-pill.not_supported {
  color: var(--tx-2);
  border-color: color-mix(in srgb, var(--tx-2) 25%, transparent);
}
.health-pill.verified {
  color: var(--st-work);
  border-color: color-mix(in srgb, var(--st-work) 30%, transparent);
  background: var(--status-work-bg);
}
.health-note {
  color: var(--tx-3);
}

.config-editor {
  width: 100%;
  min-height: 400px;
  padding: 16px;
  border: none;
  background: var(--field-surface);
  color: var(--tx);
  font-family: var(--font-mono-v2);
  font-size: 12px;
  line-height: 1.6;
  resize: vertical;
  outline: none;
  tab-size: 2;
  box-sizing: border-box;
}
.config-editor:focus {
  background: var(--field-surface-focus);
}
.config-editor:read-only {
  cursor: default;
  color: var(--tx-2);
}

.spin {
  animation: spin 1s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

@media (max-width: 640px) {
  .editor-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  .editor-actions {
    width: 100%;
    justify-content: flex-end;
  }
}
</style>
