import { useState } from 'react'
import { X } from 'lucide-react'
import { cn } from '@/lib/utils'

interface CreateCategoryModalProps {
  isOpen: boolean
  onClose: () => void
  onSubmit: (data: { name: string; description: string }) => Promise<void>
  isSaving?: boolean
}

export function CreateCategoryModal({
  isOpen,
  onClose,
  onSubmit,
  isSaving = false
}: CreateCategoryModalProps) {
  const [name, setName] = useState('')
  const [description, setDescription] = useState('')
  const [error, setError] = useState('')

  if (!isOpen) return null

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault()
    setError('')

    if (!name.trim()) {
      setError('Category name is required')
      return
    }

    if (name.length > 100) {
      setError('Category name must be 100 characters or less')
      return
    }

    try {
      await onSubmit({
        name: name.trim(),
        description: description.trim()
      })
      // Reset form on success
      setName('')
      setDescription('')
    } catch {
      setError('Failed to create category')
    }
  }

  const handleClose = () => {
    if (!isSaving) {
      setName('')
      setDescription('')
      setError('')
      onClose()
    }
  }

  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/80 backdrop-blur-sm">
      <div className="w-full max-w-md glass-card rounded-2xl p-6 shadow-lg">
        {/* Header */}
        <div className="mb-4 flex items-center justify-between">
          <h2 className="text-lg font-semibold text-white">Create Category</h2>
          <button
            onClick={handleClose}
            disabled={isSaving}
            className="rounded-full p-1 text-white/50 hover:bg-white/10 hover:text-white disabled:opacity-50"
          >
            <X className="h-5 w-5" />
          </button>
        </div>

        {/* Form */}
        <form onSubmit={handleSubmit} className="space-y-4">
          {/* Error Message */}
          {error && (
            <div className="rounded-md bg-red-400/10 p-3 text-sm text-red-400">
              {error}
            </div>
          )}

          {/* Name Field */}
          <div>
            <label htmlFor="name" className="mb-1 block text-sm font-medium text-white">
              Category Name <span className="text-red-400">*</span>
            </label>
            <input
              id="name"
              type="text"
              value={name}
              onChange={(e) => setName(e.target.value)}
              disabled={isSaving}
              maxLength={100}
              placeholder="e.g., Network Troubleshooting"
              required
              className={cn(
                'w-full rounded-md border border-white/10 bg-black/50 px-3 py-2 text-sm text-white',
                'placeholder:text-white/40',
                'focus:border-white/30 focus:outline-none focus:ring-1 focus:ring-white/20',
                'disabled:opacity-50'
              )}
            />
            <p className="mt-1 text-xs text-white/40">
              {name.length}/100 characters
            </p>
          </div>

          {/* Description Field */}
          <div>
            <label htmlFor="description" className="mb-1 block text-sm font-medium text-white">
              Description <span className="text-white/40">(optional)</span>
            </label>
            <textarea
              id="description"
              value={description}
              onChange={(e) => setDescription(e.target.value)}
              disabled={isSaving}
              rows={3}
              placeholder="Brief description of this category..."
              className={cn(
                'w-full rounded-md border border-white/10 bg-black/50 px-3 py-2 text-sm text-white',
                'placeholder:text-white/40',
                'focus:border-white/30 focus:outline-none focus:ring-1 focus:ring-white/20',
                'disabled:opacity-50'
              )}
            />
          </div>

          {/* Actions */}
          <div className="flex justify-end gap-2 pt-2">
            <button
              type="button"
              onClick={handleClose}
              disabled={isSaving}
              className={cn(
                'rounded-md border border-white/10 px-4 py-2 text-sm font-medium text-white/60',
                'hover:bg-white/10 hover:text-white disabled:opacity-50'
              )}
            >
              Cancel
            </button>
            <button
              type="submit"
              disabled={isSaving || !name.trim()}
              className={cn(
                'rounded-md bg-white px-4 py-2 text-sm font-medium text-black',
                'hover:bg-white/90 disabled:opacity-50'
              )}
            >
              {isSaving ? 'Creating...' : 'Create Category'}
            </button>
          </div>
        </form>
      </div>
    </div>
  )
}
