<template>
  <div class="bg-white dark:bg-white rounded-md border border-gray-200 dark:border-gray-200 overflow-hidden">
    <UEditor
      v-slot="{ editor }"
      v-model="editorContent"
      content-type="json"
      :editable="!props.disabled"
      :placeholder="t('applicationForms.formElements.richTextPlaceholder')"
      :ui="{
        content: 'bg-white dark:bg-white',
        base: 'min-h-[200px] p-3 bg-white dark:bg-white'
      }"
      class="w-full"
    >
      <UEditorToolbar
        :editor="editor"
        :items="toolbarItems"
        class="border-b border-muted sticky top-0 inset-x-0 px-3 py-2 z-50 bg-default overflow-x-auto"
      />
      <UEditorSuggestionMenu :editor="editor" :items="suggestionItems" />
      <UEditorMentionMenu :editor="editor" :items="mentionItems" />
      <UEditorDragHandle :editor="editor" />
    </UEditor>
  </div>
</template>

<script setup lang="ts">
import { EmployeeDataCategory, ProcessingPurpose, type FormOptionDto } from '~~/.api-client'
import type { JSONContent } from '@tiptap/vue-3'

const { t } = useI18n()

const props = defineProps<{
  formOptions: FormOptionDto[]
  disabled?: boolean
}>()

const emit = defineEmits<{
  (e: 'update:formOptions', value: FormOptionDto[]): void
}>()

const editorContent = computed({
  get: () => {
    const rawValue = props.formOptions[0]?.value ?? ''
    if (rawValue.trim().startsWith('{')) {
      try {
        return JSON.parse(rawValue) as JSONContent
      } catch {
        return rawValue // Fallback to HTML if JSON parse fails
      }
    }
    return rawValue // Treat as HTML
  },
  set: (newValue: string | JSONContent) => {
    const updatedOptions: FormOptionDto[] = [...props.formOptions]
    const stringifiedValue = typeof newValue === 'string' ? newValue : JSON.stringify(newValue)

    if (updatedOptions.length === 0) {
      const createdOption: FormOptionDto = {
        value: stringifiedValue,
        label: '',
        processingPurpose: ProcessingPurpose.None,
        employeeDataCategory: EmployeeDataCategory.None
      }
      updatedOptions.push(createdOption)
    } else {
      const firstOption = updatedOptions[0]!
      updatedOptions[0] = { ...firstOption, value: stringifiedValue }
    }
    emit('update:formOptions', updatedOptions)
  }
})

const toolbarItems = [
  [
    { kind: 'undo', icon: 'i-lucide-undo' },
    { kind: 'redo', icon: 'i-lucide-redo' }
  ],
  [
    { kind: 'heading', level: 1, icon: 'i-lucide-heading-1', label: 'H1' },
    { kind: 'heading', level: 2, icon: 'i-lucide-heading-2', label: 'H2' },
    { kind: 'heading', level: 3, icon: 'i-lucide-heading-3', label: 'H3' }
  ],
  [
    { kind: 'mark', mark: 'bold', icon: 'i-lucide-bold' },
    { kind: 'mark', mark: 'italic', icon: 'i-lucide-italic' },
    { kind: 'mark', mark: 'underline', icon: 'i-lucide-underline' },
    { kind: 'mark', mark: 'strike', icon: 'i-lucide-strikethrough' }
  ],
  [
    { kind: 'bulletList', icon: 'i-lucide-list' },
    { kind: 'orderedList', icon: 'i-lucide-list-ordered' }
  ],
  [
    { kind: 'blockquote', icon: 'i-lucide-quote' },
    { kind: 'codeBlock', icon: 'i-lucide-code' }
  ],
  [{ kind: 'link', icon: 'i-lucide-link' }]
]

const suggestionItems = [
  [
    { kind: 'heading', level: 1, label: 'Überschrift 1', icon: 'i-lucide-heading-1' },
    { kind: 'heading', level: 2, label: 'Überschrift 2', icon: 'i-lucide-heading-2' },
    { kind: 'heading', level: 3, label: 'Überschrift 3', icon: 'i-lucide-heading-3' }
  ],
  [
    { kind: 'bulletList', label: 'Aufzählung', icon: 'i-lucide-list' },
    { kind: 'orderedList', label: 'Nummerierung', icon: 'i-lucide-list-ordered' }
  ],
  [
    { kind: 'blockquote', label: 'Zitat', icon: 'i-lucide-quote' },
    { kind: 'codeBlock', label: 'Code Block', icon: 'i-lucide-code' },
    { kind: 'horizontalRule', label: 'Trennlinie', icon: 'i-lucide-separator-horizontal' }
  ]
]

const mentionItems: Array<{ label: string; avatar?: { src: string } }> = []
</script>
