'use client'

import { useState } from 'react'
import { X, MessageSquare, Bug, Lightbulb, Send, CheckCircle } from 'lucide-react'
import { FeedbackType } from '@/lib/companion/types'

interface FeedbackModalProps {
  isOpen: boolean
  onClose: () => void
  onSubmit: (type: FeedbackType, title: string, description: string) => Promise<void>
}

const feedbackTypes: { id: FeedbackType; label: string; icon: typeof Bug; color: string }[] = [
  { id: 'bug', label: 'Bug melden', icon: Bug, color: 'text-red-600 bg-red-50' },
  { id: 'feature', label: 'Feature-Wunsch', icon: Lightbulb, color: 'text-amber-600 bg-amber-50' },
  { id: 'feedback', label: 'Allgemeines Feedback', icon: MessageSquare, color: 'text-blue-600 bg-blue-50' },
]

export function FeedbackModal({ isOpen, onClose, onSubmit }: FeedbackModalProps) {
  const [type, setType] = useState<FeedbackType>('feedback')
  const [title, setTitle] = useState('')
  const [description, setDescription] = useState('')
  const [isSubmitting, setIsSubmitting] = useState(false)
  const [isSuccess, setIsSuccess] = useState(false)

  if (!isOpen) return null

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault()
    if (!title.trim() || !description.trim()) return

    setIsSubmitting(true)
    try {
      await onSubmit(type, title.trim(), description.trim())
      setIsSuccess(true)
      setTimeout(() => {
        setIsSuccess(false)
        setTitle('')
        setDescription('')
        setType('feedback')
        onClose()
      }, 2000)
    } catch (error) {
      console.error('Failed to submit feedback:', error)
    } finally {
      setIsSubmitting(false)
    }
  }

  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center">
      {/* Backdrop */}
      <div
        className="absolute inset-0 bg-black/50 backdrop-blur-sm"
        onClick={onClose}
      />

      {/* Modal */}
      <div className="relative bg-white rounded-2xl shadow-xl w-full max-w-lg mx-4">
        {/* Header */}
        <div className="flex items-center justify-between p-6 border-b border-slate-200">
          <div className="flex items-center gap-3">
            <div className="p-2 bg-blue-100 rounded-xl">
              <MessageSquare className="w-5 h-5 text-blue-600" />
            </div>
            <h2 className="text-xl font-semibold text-slate-900">Feedback senden</h2>
          </div>
          <button
            onClick={onClose}
            className="p-2 hover:bg-slate-100 rounded-lg transition-colors"
          >
            <X className="w-5 h-5 text-slate-500" />
          </button>
        </div>

        {/* Success State */}
        {isSuccess ? (
          <div className="p-12 text-center">
            <div className="w-16 h-16 bg-green-100 rounded-full flex items-center justify-center mx-auto mb-4">
              <CheckCircle className="w-8 h-8 text-green-600" />
            </div>
            <h3 className="text-xl font-semibold text-slate-900 mb-2">Vielen Dank!</h3>
            <p className="text-slate-600">Ihr Feedback wurde erfolgreich gesendet.</p>
          </div>
        ) : (
          <form onSubmit={handleSubmit}>
            {/* Content */}
            <div className="p-6 space-y-6">
              {/* Feedback Type */}
              <div>
                <label className="block text-sm font-medium text-slate-700 mb-3">
                  Art des Feedbacks
                </label>
                <div className="grid grid-cols-3 gap-3">
                  {feedbackTypes.map((ft) => (
                    <button
                      key={ft.id}
                      type="button"
                      onClick={() => setType(ft.id)}
                      className={`
                        p-4 rounded-xl border-2 text-center transition-all
                        ${type === ft.id
                          ? 'border-blue-500 bg-blue-50'
                          : 'border-slate-200 hover:border-slate-300'
                        }
                      `}
                    >
                      <div className={`w-10 h-10 rounded-lg ${ft.color} flex items-center justify-center mx-auto mb-2`}>
                        <ft.icon className="w-5 h-5" />
                      </div>
                      <span className={`text-sm font-medium ${type === ft.id ? 'text-blue-700' : 'text-slate-700'}`}>
                        {ft.label}
                      </span>
                    </button>
                  ))}
                </div>
              </div>

              {/* Title */}
              <div>
                <label className="block text-sm font-medium text-slate-700 mb-2">
                  Titel *
                </label>
                <input
                  type="text"
                  value={title}
                  onChange={(e) => setTitle(e.target.value)}
                  placeholder={
                    type === 'bug'
                      ? 'z.B. Timer stoppt nach Pause nicht mehr'
                      : type === 'feature'
                        ? 'z.B. Materialien an Stunde anhaengen'
                        : 'z.B. Super nuetzliches Tool!'
                  }
                  className="w-full px-4 py-3 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                  required
                />
              </div>

              {/* Description */}
              <div>
                <label className="block text-sm font-medium text-slate-700 mb-2">
                  Beschreibung *
                </label>
                <textarea
                  value={description}
                  onChange={(e) => setDescription(e.target.value)}
                  placeholder={
                    type === 'bug'
                      ? 'Bitte beschreiben Sie den Fehler moeglichst genau. Was haben Sie gemacht? Was ist passiert? Was haetten Sie erwartet?'
                      : type === 'feature'
                        ? 'Beschreiben Sie die gewuenschte Funktion. Warum waere sie hilfreich?'
                        : 'Teilen Sie uns Ihre Gedanken mit...'
                  }
                  rows={5}
                  className="w-full px-4 py-3 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-blue-500 resize-none"
                  required
                />
              </div>
            </div>

            {/* Footer */}
            <div className="flex items-center justify-end gap-3 p-6 border-t border-slate-200 bg-slate-50">
              <button
                type="button"
                onClick={onClose}
                className="px-4 py-2 text-slate-600 hover:bg-slate-200 rounded-lg transition-colors"
              >
                Abbrechen
              </button>
              <button
                type="submit"
                disabled={!title.trim() || !description.trim() || isSubmitting}
                className={`
                  flex items-center gap-2 px-6 py-2 rounded-lg font-medium
                  transition-all duration-200
                  ${!title.trim() || !description.trim() || isSubmitting
                    ? 'bg-slate-200 text-slate-500 cursor-not-allowed'
                    : 'bg-blue-600 text-white hover:bg-blue-700'
                  }
                `}
              >
                {isSubmitting ? (
                  <>
                    <div className="w-4 h-4 border-2 border-white/30 border-t-white rounded-full animate-spin" />
                    Senden...
                  </>
                ) : (
                  <>
                    <Send className="w-4 h-4" />
                    Absenden
                  </>
                )}
              </button>
            </div>
          </form>
        )}
      </div>
    </div>
  )
}
