'use client'

import { useState, useEffect, useCallback } from 'react'
import { useTheme } from '@/lib/ThemeContext'
import { teachersApi } from '@/lib/stundenplan/api'
import type { TimetableTeacher, CreateTimetableTeacher } from '@/app/stundenplan/types'

const initialForm: CreateTimetableTeacher = {
  first_name: '',
  last_name: '',
  short_code: '',
  employment_percentage: 100,
  max_hours_week: 28,
  notes: '',
}

export function LehrerManager() {
  const { isDark } = useTheme()
  const [items, setItems] = useState<TimetableTeacher[]>([])
  const [loading, setLoading] = useState(true)
  const [error, setError] = useState<string | null>(null)
  const [showForm, setShowForm] = useState(false)
  const [submitting, setSubmitting] = useState(false)
  const [form, setForm] = useState<CreateTimetableTeacher>(initialForm)

  const load = useCallback(async () => {
    setLoading(true)
    setError(null)
    try {
      const data = await teachersApi.list()
      setItems(data || [])
    } catch (e) {
      setError(e instanceof Error ? e.message : 'Laden fehlgeschlagen')
    } finally {
      setLoading(false)
    }
  }, [])

  useEffect(() => { load() }, [load])

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault()
    setSubmitting(true)
    setError(null)
    try {
      await teachersApi.create(form)
      setForm(initialForm)
      setShowForm(false)
      await load()
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Anlegen fehlgeschlagen')
    } finally {
      setSubmitting(false)
    }
  }

  const handleDelete = async (id: string) => {
    if (!confirm('Lehrer wirklich loeschen? Verbundene Constraints werden mitgeloescht.')) return
    try {
      await teachersApi.remove(id)
      await load()
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Loeschen fehlgeschlagen')
    }
  }

  const cardClass = isDark ? 'bg-white/10 border-white/20 text-white' : 'bg-white/80 border-black/10 text-slate-900'
  const inputClass = isDark ? 'bg-white/10 border-white/20 text-white placeholder-white/40' : 'bg-white border-slate-300 text-slate-900 placeholder-slate-400'

  return (
    <div className="space-y-4" data-testid="lehrer-manager">
      <div className="flex items-center justify-between">
        <h2 className={`text-xl font-semibold ${isDark ? 'text-white' : 'text-slate-900'}`}>
          Lehrer ({items.length})
        </h2>
        <button
          onClick={() => setShowForm(s => !s)}
          className={`px-4 py-2 rounded-xl font-medium transition-colors ${isDark ? 'bg-indigo-500 hover:bg-indigo-600 text-white' : 'bg-indigo-600 hover:bg-indigo-700 text-white'}`}
        >
          {showForm ? 'Abbrechen' : '+ Neuer Lehrer'}
        </button>
      </div>

      {error && (
        <div className="p-3 rounded-xl bg-red-500/20 border border-red-500/40 text-red-300">{error}</div>
      )}

      {showForm && (
        <form onSubmit={handleSubmit} className={`p-4 rounded-2xl border backdrop-blur-xl ${cardClass}`}>
          <div className="grid grid-cols-1 md:grid-cols-3 gap-3">
            <div>
              <label className="block text-sm mb-1 opacity-70">Vorname</label>
              <input required value={form.first_name} onChange={e => setForm({ ...form, first_name: e.target.value })} className={`w-full px-3 py-2 rounded-lg border ${inputClass}`} />
            </div>
            <div>
              <label className="block text-sm mb-1 opacity-70">Nachname</label>
              <input required value={form.last_name} onChange={e => setForm({ ...form, last_name: e.target.value })} className={`w-full px-3 py-2 rounded-lg border ${inputClass}`} />
            </div>
            <div>
              <label className="block text-sm mb-1 opacity-70">Kuerzel</label>
              <input required maxLength={10} value={form.short_code} onChange={e => setForm({ ...form, short_code: e.target.value.toUpperCase() })} placeholder="z.B. MUE" className={`w-full px-3 py-2 rounded-lg border ${inputClass}`} />
            </div>
            <div>
              <label className="block text-sm mb-1 opacity-70">Stellenanteil (%)</label>
              <input type="number" min={0} max={100} value={form.employment_percentage ?? 100} onChange={e => setForm({ ...form, employment_percentage: parseInt(e.target.value) || 0 })} className={`w-full px-3 py-2 rounded-lg border ${inputClass}`} />
            </div>
            <div>
              <label className="block text-sm mb-1 opacity-70">Max. Stunden/Woche</label>
              <input type="number" min={0} max={40} value={form.max_hours_week ?? 28} onChange={e => setForm({ ...form, max_hours_week: parseInt(e.target.value) || 0 })} className={`w-full px-3 py-2 rounded-lg border ${inputClass}`} />
            </div>
            <div className="flex items-end">
              <button type="submit" disabled={submitting} className="w-full px-4 py-2 rounded-lg bg-emerald-500 hover:bg-emerald-600 text-white font-medium disabled:opacity-50">
                {submitting ? 'Speichert...' : 'Anlegen'}
              </button>
            </div>
          </div>
        </form>
      )}

      {loading ? (
        <div className={`text-center py-8 opacity-60 ${isDark ? 'text-white' : 'text-slate-700'}`}>Laedt…</div>
      ) : items.length === 0 ? (
        <div className={`text-center py-8 opacity-60 ${isDark ? 'text-white' : 'text-slate-700'}`}>Noch keine Lehrer angelegt.</div>
      ) : (
        <div className={`rounded-2xl border backdrop-blur-xl overflow-hidden ${cardClass}`}>
          <table className="w-full">
            <thead className={isDark ? 'bg-white/5' : 'bg-slate-100'}>
              <tr>
                <th className="text-left px-4 py-3 text-sm font-medium opacity-70">Name</th>
                <th className="text-left px-4 py-3 text-sm font-medium opacity-70">Kuerzel</th>
                <th className="text-left px-4 py-3 text-sm font-medium opacity-70">Stellenanteil</th>
                <th className="text-left px-4 py-3 text-sm font-medium opacity-70">Max. h/Woche</th>
                <th className="px-4 py-3"></th>
              </tr>
            </thead>
            <tbody>
              {items.map(t => (
                <tr key={t.id} className={isDark ? 'border-t border-white/10' : 'border-t border-slate-200'}>
                  <td className="px-4 py-3 font-medium">{t.last_name}, {t.first_name}</td>
                  <td className="px-4 py-3">{t.short_code}</td>
                  <td className="px-4 py-3">{t.employment_percentage}%</td>
                  <td className="px-4 py-3">{t.max_hours_week}</td>
                  <td className="px-4 py-3 text-right">
                    <button onClick={() => handleDelete(t.id)} className="text-red-400 hover:text-red-300 text-sm">Loeschen</button>
                  </td>
                </tr>
              ))}
            </tbody>
          </table>
        </div>
      )}
    </div>
  )
}
