import { useEffect, useRef, useState } from 'react'

// ToneSelect lets the writer set the document's target tone, which steers the
// grammar-checkpoint LLM toward the right register (an academic essay vs a casual
// journal). A small custom dropdown (not a native <select>) so it can carry the
// bilingual zh·en labels and emoji that match Petal's chrome — the writer uses
// Mandarin and English. The `value` strings mirror the backend's tone keys.

export interface ToneOption {
  value: string
  emoji: string
  zh: string
  en: string
}

// Keep these `value`s in sync with llm.toneGuidance on the server. 'general'
// means no steering (Petal's default friendly ESL advice).
export const TONES: ToneOption[] = [
  { value: 'general', emoji: '🌸', zh: '通用', en: 'General' },
  { value: 'academic', emoji: '🎓', zh: '学术', en: 'Academic' },
  { value: 'professional', emoji: '💼', zh: '专业', en: 'Professional' },
  { value: 'casual', emoji: '☕', zh: '轻松', en: 'Casual' },
  { value: 'humorous', emoji: '😄', zh: '幽默', en: 'Humorous' },
  { value: 'creative', emoji: '🎨', zh: '创意', en: 'Creative' },
  { value: 'persuasive', emoji: '📣', zh: '说服', en: 'Persuasive' },
]

interface Props {
  value: string
  onChange: (value: string) => void
}

export function ToneSelect({ value, onChange }: Props) {
  const [open, setOpen] = useState(false)
  const ref = useRef<HTMLDivElement>(null)
  const current = TONES.find((t) => t.value === value) ?? TONES[0]

  // Click outside closes the menu.
  useEffect(() => {
    if (!open) return
    const onDown = (e: MouseEvent) => {
      if (!ref.current?.contains(e.target as Node)) setOpen(false)
    }
    document.addEventListener('mousedown', onDown)
    return () => document.removeEventListener('mousedown', onDown)
  }, [open])

  return (
    <div ref={ref} className="relative shrink-0">
      <button
        type="button"
        aria-label="Document tone"
        aria-haspopup="listbox"
        aria-expanded={open}
        onClick={() => setOpen((o) => !o)}
        className="inline-flex h-9 items-center gap-1.5 whitespace-nowrap px-3 text-sm font-bold"
        style={{
          borderRadius: 'var(--radius-pill)',
          background: 'var(--color-surface)',
          color: 'var(--color-plum)',
          boxShadow: 'var(--shadow-soft)',
        }}
        title="Set the tone — Petal tailors its advice to match"
      >
        <span aria-hidden>{current.emoji}</span>
        <span>{current.zh}</span>
        <span style={{ color: 'var(--color-muted)' }}>· {current.en}</span>
        <span aria-hidden style={{ color: 'var(--color-muted)' }}>
          ⌄
        </span>
      </button>

      {open && (
        <div
          role="listbox"
          className="petal-word-card absolute right-0 z-30 mt-1.5 p-1.5"
          style={{
            width: 200,
            background: 'var(--color-surface)',
            border: '1px solid var(--color-border)',
            borderRadius: 'var(--radius-card)',
            boxShadow: 'var(--shadow-soft)',
          }}
        >
          {TONES.map((t) => {
            const active = t.value === value
            return (
              <button
                key={t.value}
                type="button"
                role="option"
                aria-selected={active}
                onClick={() => {
                  onChange(t.value)
                  setOpen(false)
                }}
                className="flex w-full items-center gap-2 rounded-xl px-2.5 py-1.5 text-left text-sm font-semibold"
                style={{
                  background: active ? 'var(--color-surface-alt)' : 'transparent',
                  color: 'var(--color-plum)',
                }}
                onMouseEnter={(e) => (e.currentTarget.style.background = 'var(--color-surface-alt)')}
                onMouseLeave={(e) =>
                  (e.currentTarget.style.background = active ? 'var(--color-surface-alt)' : 'transparent')
                }
              >
                <span aria-hidden>{t.emoji}</span>
                <span>{t.zh}</span>
                <span className="font-normal" style={{ color: 'var(--color-muted)' }}>
                  {t.en}
                </span>
              </button>
            )
          })}
        </div>
      )}
    </div>
  )
}
