<script setup lang="ts">
import { watch, onBeforeUnmount } from 'vue'
import { useEditor, EditorContent } from '@tiptap/vue-3'
import StarterKit from '@tiptap/starter-kit'
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight'
import { common, createLowlight } from 'lowlight'

const lowlight = createLowlight(common)

const props = defineProps<{
  modelValue: Record<string, unknown> | null
  placeholder?: string
}>()

const emit = defineEmits<{
  'update:modelValue': [value: Record<string, unknown>]
  'ready': []
}>()

const editor = useEditor({
  extensions: [
    StarterKit.configure({
      codeBlock: false,
    }),
    CodeBlockLowlight.configure({
      lowlight,
    }),
  ],
  content: props.modelValue || '',
  editorProps: {
    attributes: {
      class: 'tiptap-editor',
    },
  },
  onUpdate: ({ editor }) => {
    emit('update:modelValue', editor.getJSON())
  },
  onCreate: () => {
    emit('ready')
  },
})

watch(() => props.modelValue, (newValue) => {
  if (!editor.value) return
  if (JSON.stringify(editor.value.getJSON()) !== JSON.stringify(newValue)) {
    editor.value.commands.setContent(newValue || '', { emitUpdate: false })
  }
})

onBeforeUnmount(() => {
  editor.value?.destroy()
})
</script>

<template>
  <div class="tiptap-wrapper">
    <div v-if="editor" class="tiptap-toolbar">
      <button
        type="button"
        class="tiptap-btn"
        :class="{ active: editor.isActive('bold') }"
        @click="editor.chain().focus().toggleBold().run()"
        title="Bold"
      >
        <strong>B</strong>
      </button>
      <button
        type="button"
        class="tiptap-btn"
        :class="{ active: editor.isActive('italic') }"
        @click="editor.chain().focus().toggleItalic().run()"
        title="Italic"
      >
        <em>I</em>
      </button>
      <span class="tiptap-separator"></span>
      <button
        type="button"
        class="tiptap-btn"
        :class="{ active: editor.isActive('heading', { level: 1 }) }"
        @click="editor.chain().focus().toggleHeading({ level: 1 }).run()"
        title="Heading 1"
      >
        H1
      </button>
      <button
        type="button"
        class="tiptap-btn"
        :class="{ active: editor.isActive('heading', { level: 2 }) }"
        @click="editor.chain().focus().toggleHeading({ level: 2 }).run()"
        title="Heading 2"
      >
        H2
      </button>
      <button
        type="button"
        class="tiptap-btn"
        :class="{ active: editor.isActive('heading', { level: 3 }) }"
        @click="editor.chain().focus().toggleHeading({ level: 3 }).run()"
        title="Heading 3"
      >
        H3
      </button>
      <span class="tiptap-separator"></span>
      <button
        type="button"
        class="tiptap-btn"
        :class="{ active: editor.isActive('bulletList') }"
        @click="editor.chain().focus().toggleBulletList().run()"
        title="Bullet List"
      >
        •
      </button>
      <button
        type="button"
        class="tiptap-btn"
        :class="{ active: editor.isActive('orderedList') }"
        @click="editor.chain().focus().toggleOrderedList().run()"
        title="Ordered List"
      >
        1.
      </button>
      <button
        type="button"
        class="tiptap-btn"
        :class="{ active: editor.isActive('codeBlock') }"
        @click="editor.chain().focus().toggleCodeBlock().run()"
        title="Code Block"
      >
        &lt;/&gt;
      </button>
      <button
        type="button"
        class="tiptap-btn"
        :class="{ active: editor.isActive('blockquote') }"
        @click="editor.chain().focus().toggleBlockquote().run()"
        title="Blockquote"
      >
        &ldquo;
      </button>
    </div>
    <EditorContent :editor="editor" />
  </div>
</template>

<style>
.tiptap-wrapper {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.tiptap-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.25rem;
  padding: 0.5rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: 8px 8px 0 0;
}

.tiptap-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 32px;
  height: 32px;
  padding: 0 0.5rem;
  background: none;
  border: 1px solid transparent;
  border-radius: 4px;
  font-size: 0.875rem;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s;
}

.tiptap-btn:hover {
  background: var(--bg-primary);
  color: var(--text-primary);
}

.tiptap-btn.active {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.tiptap-separator {
  width: 1px;
  height: 20px;
  background: var(--border);
  margin: 0 0.25rem;
}

.tiptap-editor {
  flex: 1;
  padding: 1rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 0 0 8px 8px;
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
  font-size: 0.9375rem;
  line-height: 1.7;
  color: var(--text-primary);
  outline: none;
  overflow-y: auto;
  min-height: 400px;
}

.tiptap-editor:focus {
  border-color: var(--accent);
}

.tiptap-editor p {
  margin: 0 0 1rem 0;
}

.tiptap-editor h1,
.tiptap-editor h2,
.tiptap-editor h3 {
  font-weight: 600;
  margin: 1.5rem 0 0.75rem 0;
}

.tiptap-editor h1 {
  font-size: 1.75rem;
}

.tiptap-editor h2 {
  font-size: 1.5rem;
}

.tiptap-editor h3 {
  font-size: 1.25rem;
}

.tiptap-editor ul,
.tiptap-editor ol {
  padding-left: 1.5rem;
  margin: 0 0 1rem 0;
}

.tiptap-editor li {
  margin: 0.25rem 0;
}

.tiptap-editor code {
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.125rem 0.375rem;
  font-family: inherit;
  font-size: 0.875em;
}

.tiptap-editor pre {
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1rem;
  margin: 0 0 1rem 0;
  overflow-x: auto;
}

.tiptap-editor pre code {
  background: none;
  border: none;
  padding: 0;
}

.tiptap-editor blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 1rem;
  margin: 0 0 1rem 0;
  color: var(--text-secondary);
}

.tiptap-editor p.is-editor-empty:first-child::before {
  content: attr(data-placeholder);
  float: left;
  color: var(--text-secondary);
  pointer-events: none;
  height: 0;
}

/* Syntax highlighting */
.tiptap-editor .hljs-comment,
.tiptap-editor .hljs-quote {
  color: #6a737d;
}

.tiptap-editor .hljs-variable,
.tiptap-editor .hljs-template-variable,
.tiptap-editor .hljs-tag,
.tiptap-editor .hljs-name,
.tiptap-editor .hljs-selector-id,
.tiptap-editor .hljs-selector-class,
.tiptap-editor .hljs-regexp,
.tiptap-editor .hljs-deletion {
  color: #d73a49;
}

.tiptap-editor .hljs-number,
.tiptap-editor .hljs-built_in,
.tiptap-editor .hljs-literal,
.tiptap-editor .hljs-type,
.tiptap-editor .hljs-params,
.tiptap-editor .hljs-meta,
.tiptap-editor .hljs-link {
  color: #005cc5;
}

.tiptap-editor .hljs-attribute {
  color: #e36209;
}

.tiptap-editor .hljs-string,
.tiptap-editor .hljs-symbol,
.tiptap-editor .hljs-bullet,
.tiptap-editor .hljs-addition {
  color: #22863a;
}

.tiptap-editor .hljs-title,
.tiptap-editor .hljs-section {
  color: #6f42c1;
}

.tiptap-editor .hljs-keyword,
.tiptap-editor .hljs-selector-tag {
  color: #d73a49;
}
</style>
