'use client'

import { useState, useEffect } from 'react'
import {
  teacherMaxHoursDayApi, teacherMaxHoursWeekApi, teachersApi,
} from '@/lib/stundenplan/api'
import type {
  TeacherMaxHoursDay, TeacherMaxHoursWeek, TimetableTeacher,
} from '@/app/stundenplan/types'
import { useConstraintCrud, ConstraintShell, useShellStyles } from './_shell'

function useTeachers() {
  const [teachers, setTeachers] = useState<TimetableTeacher[]>([])
  useEffect(() => { teachersApi.list().then(setTeachers).catch(() => setTeachers([])) }, [])
  return teachers
}

function tLabel(teachers: TimetableTeacher[], id: string): string {
  const t = teachers.find(x => x.id === id)
  return t ? `${t.last_name}, ${t.first_name}` : id.slice(0, 8) + '…'
}

// ---------- Max Hours / Day ----------

type DayForm = Omit<TeacherMaxHoursDay, 'id' | 'created_by_user_id' | 'created_at'>
const initialDay: DayForm = { teacher_id: '', max_hours: 6, is_hard: false, weight: 50, active: true, note: '' }

export function TeacherMaxHoursDayEditor() {
  const styles = useShellStyles()
  const teachers = useTeachers()
  const crud = useConstraintCrud<TeacherMaxHoursDay, DayForm>(teacherMaxHoursDayApi, initialDay)

  return (
    <ConstraintShell
      testId="teacher-max-hours-day-editor"
      title="Lehrer: Max. Stunden / Tag"
      description="Beispiel: „Anna unterrichtet hoechstens 6 Stunden pro Tag" (oft Soft-Regel)."
      newLabel="+ Neue Regel"
      newDisabled={teachers.length === 0}
      prereqWarning={teachers.length === 0 ? 'Zuerst Lehrer anlegen.' : null}
      emptyText="Keine Regeln vorhanden."
      tableHeaders={['Lehrer', 'Max. Std/Tag', 'Hart', 'Weight']}
      state={crud}
      formBody={
        <div className="grid grid-cols-1 md:grid-cols-4 gap-3">
          <div>
            <label className="block text-sm mb-1 opacity-70">Lehrer</label>
            <select required value={crud.form.teacher_id} onChange={e => crud.setForm({ ...crud.form, teacher_id: e.target.value })} className={`w-full px-3 py-2 rounded-lg border ${styles.inputClass}`}>
              <option value="">— bitte waehlen —</option>
              {teachers.map(t => <option key={t.id} value={t.id}>{t.last_name}, {t.first_name}</option>)}
            </select>
          </div>
          <div>
            <label className="block text-sm mb-1 opacity-70">Max. Stunden (1-12)</label>
            <input type="number" min={1} max={12} required value={crud.form.max_hours} onChange={e => crud.setForm({ ...crud.form, max_hours: parseInt(e.target.value) || 1 })} className={`w-full px-3 py-2 rounded-lg border ${styles.inputClass}`} />
          </div>
          <div>
            <label className="block text-sm mb-1 opacity-70">Weight (0-100)</label>
            <input type="number" min={0} max={100} value={crud.form.weight} onChange={e => crud.setForm({ ...crud.form, weight: parseInt(e.target.value) || 0 })} className={`w-full px-3 py-2 rounded-lg border ${styles.inputClass}`} />
          </div>
          <div className="flex items-center gap-2">
            <input type="checkbox" id="is_hard_mhd" checked={crud.form.is_hard} onChange={e => crud.setForm({ ...crud.form, is_hard: e.target.checked })} className="w-5 h-5" />
            <label htmlFor="is_hard_mhd" className="text-sm">Harte Regel</label>
          </div>
          <div className="md:col-span-4 flex items-end">
            <button type="submit" disabled={crud.submitting} className={styles.submitBtn}>{crud.submitting ? 'Speichert...' : 'Anlegen'}</button>
          </div>
        </div>
      }
      renderRow={(item) => {
        const c = item as TeacherMaxHoursDay
        return (
          <tr key={c.id} className={styles.rowClass}>
            <td className="px-4 py-3 font-medium">{tLabel(teachers, c.teacher_id)}</td>
            <td className="px-4 py-3">{c.max_hours}</td>
            <td className="px-4 py-3">{c.is_hard ? '✓' : '—'}</td>
            <td className="px-4 py-3">{c.weight}</td>
            <td className="px-4 py-3 text-right"><button onClick={() => crud.remove(c.id)} className={styles.deleteBtn}>Loeschen</button></td>
          </tr>
        )
      }}
    />
  )
}

// ---------- Max Hours / Week ----------

type WeekForm = Omit<TeacherMaxHoursWeek, 'id' | 'created_by_user_id' | 'created_at'>
const initialWeek: WeekForm = { teacher_id: '', max_hours: 28, is_hard: true, weight: 100, active: true, note: '' }

export function TeacherMaxHoursWeekEditor() {
  const styles = useShellStyles()
  const teachers = useTeachers()
  const crud = useConstraintCrud<TeacherMaxHoursWeek, WeekForm>(teacherMaxHoursWeekApi, initialWeek)

  return (
    <ConstraintShell
      testId="teacher-max-hours-week-editor"
      title="Lehrer: Max. Stunden / Woche"
      description="Beispiel: Teilzeit 50% → max. 14 h/Woche (harte Regel aus Vertrag)."
      newLabel="+ Neue Regel"
      newDisabled={teachers.length === 0}
      prereqWarning={teachers.length === 0 ? 'Zuerst Lehrer anlegen.' : null}
      emptyText="Keine Regeln vorhanden."
      tableHeaders={['Lehrer', 'Max. Std/Woche', 'Hart', 'Weight']}
      state={crud}
      formBody={
        <div className="grid grid-cols-1 md:grid-cols-4 gap-3">
          <div>
            <label className="block text-sm mb-1 opacity-70">Lehrer</label>
            <select required value={crud.form.teacher_id} onChange={e => crud.setForm({ ...crud.form, teacher_id: e.target.value })} className={`w-full px-3 py-2 rounded-lg border ${styles.inputClass}`}>
              <option value="">— bitte waehlen —</option>
              {teachers.map(t => <option key={t.id} value={t.id}>{t.last_name}, {t.first_name}</option>)}
            </select>
          </div>
          <div>
            <label className="block text-sm mb-1 opacity-70">Max. Stunden (1-40)</label>
            <input type="number" min={1} max={40} required value={crud.form.max_hours} onChange={e => crud.setForm({ ...crud.form, max_hours: parseInt(e.target.value) || 1 })} className={`w-full px-3 py-2 rounded-lg border ${styles.inputClass}`} />
          </div>
          <div>
            <label className="block text-sm mb-1 opacity-70">Weight (0-100)</label>
            <input type="number" min={0} max={100} value={crud.form.weight} onChange={e => crud.setForm({ ...crud.form, weight: parseInt(e.target.value) || 0 })} className={`w-full px-3 py-2 rounded-lg border ${styles.inputClass}`} />
          </div>
          <div className="flex items-center gap-2">
            <input type="checkbox" id="is_hard_mhw" checked={crud.form.is_hard} onChange={e => crud.setForm({ ...crud.form, is_hard: e.target.checked })} className="w-5 h-5" />
            <label htmlFor="is_hard_mhw" className="text-sm">Harte Regel</label>
          </div>
          <div className="md:col-span-4 flex items-end">
            <button type="submit" disabled={crud.submitting} className={styles.submitBtn}>{crud.submitting ? 'Speichert...' : 'Anlegen'}</button>
          </div>
        </div>
      }
      renderRow={(item) => {
        const c = item as TeacherMaxHoursWeek
        return (
          <tr key={c.id} className={styles.rowClass}>
            <td className="px-4 py-3 font-medium">{tLabel(teachers, c.teacher_id)}</td>
            <td className="px-4 py-3">{c.max_hours}</td>
            <td className="px-4 py-3">{c.is_hard ? '✓' : '—'}</td>
            <td className="px-4 py-3">{c.weight}</td>
            <td className="px-4 py-3 text-right"><button onClick={() => crud.remove(c.id)} className={styles.deleteBtn}>Loeschen</button></td>
          </tr>
        )
      }}
    />
  )
}
