<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useFruitStore } from '../stores/fruitStore'
import { FRUIT_TYPES } from '../api/fruits'

const router = useRouter()
const store = useFruitStore()

const name = ref('')
const osdbNumber = ref('')
const comment = ref('')
const fruitType = ref('')
const synonymInput = ref('')
const synonyms = ref<string[]>([])
const submitting = ref(false)
const serverError = ref<string | null>(null)

function addSynonym() {
  const s = synonymInput.value.trim()
  if (s && !synonyms.value.includes(s)) {
    synonyms.value.push(s)
  }
  synonymInput.value = ''
}

function removeSynonym(idx: number) {
  synonyms.value.splice(idx, 1)
}

async function submit() {
  serverError.value = null
  submitting.value = true
  try {
    const fruit = await store.create({
      name: name.value,
      osdb_number: osdbNumber.value,
      comment: comment.value || null,
      fruit_type: fruitType.value,
      synonyms: synonyms.value,
    })
    router.push(`/fruits/${fruit.id}`)
  } catch (e) {
    serverError.value = e instanceof Error ? e.message : 'Fehler beim Speichern'
  } finally {
    submitting.value = false
  }
}
</script>

<template>
  <div class="p-6 max-w-2xl mx-auto">
    <h1 class="text-2xl font-bold text-gray-900 mb-6">Neue Frucht anlegen</h1>

    <div v-if="serverError" class="mb-4 p-3 bg-red-50 border border-red-300 rounded text-red-700 text-sm">
      {{ serverError }}
    </div>

    <form @submit.prevent="submit" class="space-y-4">
      <div>
        <label class="block text-sm font-medium text-gray-700 mb-1">Name *</label>
        <input v-model="name" type="text" required class="w-full border border-gray-300 rounded px-3 py-2 focus:outline-none focus:ring-2 focus:ring-green-500" />
      </div>

      <div>
        <label class="block text-sm font-medium text-gray-700 mb-1">OSDB-Kürzel *</label>
        <input v-model="osdbNumber" type="text" required class="w-full border border-gray-300 rounded px-3 py-2 focus:outline-none focus:ring-2 focus:ring-green-500" />
      </div>

      <div>
        <label class="block text-sm font-medium text-gray-700 mb-1">Typ *</label>
        <select v-model="fruitType" required class="w-full border border-gray-300 rounded px-3 py-2 focus:outline-none focus:ring-2 focus:ring-green-500">
          <option value="" disabled>Bitte wählen...</option>
          <option v-for="t in FRUIT_TYPES" :key="t" :value="t">{{ t }}</option>
        </select>
      </div>

      <div>
        <label class="block text-sm font-medium text-gray-700 mb-1">Kommentar</label>
        <textarea v-model="comment" rows="3" class="w-full border border-gray-300 rounded px-3 py-2 focus:outline-none focus:ring-2 focus:ring-green-500" />
      </div>

      <div>
        <label class="block text-sm font-medium text-gray-700 mb-1">Synonyme</label>
        <div class="flex gap-2 mb-2">
          <input
            v-model="synonymInput"
            type="text"
            @keydown.enter.prevent="addSynonym"
            placeholder="Synonym eingeben und Enter drücken"
            class="flex-1 border border-gray-300 rounded px-3 py-2 focus:outline-none focus:ring-2 focus:ring-green-500"
          />
          <button type="button" @click="addSynonym" class="bg-gray-200 px-3 py-2 rounded hover:bg-gray-300">
            +
          </button>
        </div>
        <ul class="space-y-1">
          <li v-for="(s, i) in synonyms" :key="i" class="flex items-center gap-2 text-sm">
            <span class="flex-1 bg-gray-100 rounded px-2 py-1">{{ s }}</span>
            <button type="button" @click="removeSynonym(i)" class="text-red-500 hover:text-red-700 text-xs">✕</button>
          </li>
        </ul>
      </div>

      <div class="flex gap-3 pt-2">
        <button
          type="submit"
          :disabled="submitting"
          class="bg-green-600 text-white px-6 py-2 rounded hover:bg-green-700 disabled:opacity-50 transition-colors"
        >
          {{ submitting ? 'Speichern...' : 'Anlegen' }}
        </button>
        <RouterLink to="/fruits" class="px-6 py-2 border rounded hover:bg-gray-50 transition-colors">
          Abbrechen
        </RouterLink>
      </div>
    </form>
  </div>
</template>
