/* Hallmark · pre-emit critique: P5 H5 E5 S5 R5 V5 */
/* Hallmark · component: TemplatesPage · genre: editorial · theme: Atelier (Dark & Light) · studied-DNA: https://inihaus.de */
import React, { useState, useEffect } from 'react';
import { ArrowLeft, Layers, Play, CheckCircle2, AlertCircle, Plus, Trash2, Clock, Users, Award } from 'lucide-react';
import { apiFetch } from '../api/client';

export default function TemplatesPage({ onBack, onInstantiateTemplate, user, skills }) {
  const [templates, setTemplates] = useState([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState('');
  const [selectedTemplate, setSelectedTemplate] = useState(null);

  // Form for instantiating template
  const [title, setTitle] = useState('');
  const [startDate, setStartDate] = useState(new Date().toISOString().split('T')[0]);
  const [endDate, setEndDate] = useState(new Date().toISOString().split('T')[0]);
  const [location, setLocation] = useState('');
  const [submitting, setSubmitting] = useState(false);

  // State for creating a new template with preset shifts
  const [showCreateTemplate, setShowCreateTemplate] = useState(false);
  const [newTplName, setNewTplName] = useState('');
  const [newTplDesc, setNewTplDesc] = useState('');
  const [newTplTaskAreas, setNewTplTaskAreas] = useState([
    {
      name: 'Tresendienst',
      description: 'Getränkeausschank und Bar',
      shifts: [
        { title: 'Frühschicht', start_time: '18:00', end_time: '22:00', max_participants: 2, required_skill_ids: [] },
        { title: 'Spätschicht', start_time: '22:00', end_time: '02:00', max_participants: 3, required_skill_ids: [] }
      ]
    }
  ]);

  useEffect(() => {
    fetchTemplates();
  }, []);

  const fetchTemplates = async () => {
    setLoading(true);
    try {
      const data = await apiFetch('/templates/');
      setTemplates(data.results || data);
    } catch (err) {
      setError(err.message || 'Laden der Vorlagen fehlgeschlagen.');
    } finally {
      setLoading(false);
    }
  };

  const handleSelectTemplate = (template) => {
    setSelectedTemplate(template);
    setTitle(template.name);
  };

  const handleInstantiate = async (e) => {
    e.preventDefault();
    if (!selectedTemplate) return;

    setSubmitting(true);
    try {
      await onInstantiateTemplate(selectedTemplate.id, {
        title,
        start_date: startDate,
        end_date: endDate,
        location
      });
    } catch (err) {
      setError(err.message || 'Erstellen der Veranstaltung aus Vorlage fehlgeschlagen.');
    } finally {
      setSubmitting(false);
    }
  };

  const handleCreateTemplate = async (e) => {
    e.preventDefault();
    if (!newTplName.trim()) {
      setError('Bitte gib einen Vorlagen-Namen an.');
      return;
    }

    setSubmitting(true);
    try {
      const payload = {
        name: newTplName,
        description: newTplDesc,
        template_data: {
          task_areas: newTplTaskAreas.map(ta => ({
            name: ta.name,
            description: ta.description,
            shifts: (ta.shifts || []).map(s => ({
              title: s.title,
              start_time: s.start_time,
              end_time: s.end_time,
              max_participants: s.max_participants,
              required_skill_ids: s.required_skill_ids
            }))
          }))
        }
      };

      await apiFetch('/templates/', {
        method: 'POST',
        body: JSON.stringify(payload)
      });

      setShowCreateTemplate(false);
      setNewTplName('');
      setNewTplDesc('');
      fetchTemplates();
    } catch (err) {
      setError(err.message || 'Erstellen der Vorlage fehlgeschlagen.');
    } finally {
      setSubmitting(false);
    }
  };

  const addTplTaskArea = () => {
    setNewTplTaskAreas([
      ...newTplTaskAreas,
      {
        name: 'Neuer Bereich',
        description: '',
        shifts: [{ title: 'Schicht 1', start_time: '14:00', end_time: '18:00', max_participants: 2, required_skill_ids: [] }]
      }
    ]);
  };

  const addTplShift = (taIdx) => {
    const updated = [...newTplTaskAreas];
    updated[taIdx].shifts.push({
      title: `Schicht ${updated[taIdx].shifts.length + 1}`,
      start_time: '18:00',
      end_time: '22:00',
      max_participants: 2,
      required_skill_ids: []
    });
    setNewTplTaskAreas(updated);
  };

  return (
    <div className="space-y-6 max-w-4xl mx-auto animate-in fade-in duration-200 font-sans">
      {/* Navigation Header */}
      <div className="flex items-center justify-between font-mono text-xs">
        <button
          onClick={onBack}
          className="px-3.5 py-1.5 rounded-sm bg-subtle hover:bg-surface-hover text-main border border-grid transition flex items-center gap-2 font-bold"
        >
          <ArrowLeft className="w-4 h-4" /> [ ZURÜCK ZUR ÜBERSICHT ]
        </button>

        <span className="text-muted border border-grid px-2.5 py-0.5 rounded-sm uppercase">
          SYSTEM VORLAGEN ({templates.length})
        </span>
      </div>

      {/* Page Title Panel */}
      <div className="hallmark-panel rounded-sm p-6 sm:p-8 border border-grid space-y-4">
        <div className="flex items-center justify-between gap-3">
          <div className="flex items-center gap-3">
            <div className="w-10 h-10 rounded-sm bg-indigo-500/10 text-indigo-400 border border-indigo-500/20 flex items-center justify-center font-bold">
              <Layers className="w-5 h-5" />
            </div>
            <div>
              <h2 className="font-serif text-3xl font-bold uppercase tracking-tight text-main">
                Veranstaltungs-Vorlagen Zentrale
              </h2>
              <p className="text-xs text-muted font-mono mt-0.5">
                Erstelle neue Veranstaltungen mit voreingestellten Schichten und Aufgabenbereichen
              </p>
            </div>
          </div>

          {user && (
            <button
              onClick={() => setShowCreateTemplate(!showCreateTemplate)}
              className="px-3.5 py-2 rounded-sm text-xs font-mono font-bold bg-indigo-500/10 text-indigo-400 border border-indigo-500/20 hover:bg-indigo-500/20 transition flex items-center gap-1.5 shrink-0"
            >
              <Plus className="w-4 h-4" />
              {showCreateTemplate ? 'Schließen' : 'Neue Vorlage Erstellen'}
            </button>
          )}
        </div>
      </div>

      {error && (
        <div className="p-4 rounded-sm bg-red-500/10 border border-red-500/20 text-red-500 text-xs font-mono flex items-center gap-2">
          <AlertCircle className="w-4 h-4 shrink-0" />
          <span>{error}</span>
        </div>
      )}

      {/* Creator Form for new Template with Preset Shifts */}
      {showCreateTemplate && (
        <form onSubmit={handleCreateTemplate} className="hallmark-panel rounded-sm p-6 border border-grid space-y-4 font-mono text-xs animate-in fade-in duration-200">
          <h3 className="font-serif text-lg font-bold uppercase text-indigo-400 border-b border-grid pb-2 flex items-center justify-between">
            <span>Neue Vorlage mit voreingestellten Schichten anlegen</span>
            <button
              type="button"
              onClick={addTplTaskArea}
              className="text-xs font-mono font-bold bg-indigo-500/20 text-indigo-300 px-3 py-1 rounded-sm border border-indigo-500/30 flex items-center gap-1"
            >
              <Plus className="w-3.5 h-3.5" /> Bereich Hinzufügen
            </button>
          </h3>

          <div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
            <div className="sm:col-span-2">
              <label className="block text-main mb-1 font-bold">Vorlagen Name</label>
              <input
                type="text"
                required
                placeholder="z. B. Standard Kneipenabend / Bar-Event"
                value={newTplName}
                onChange={(e) => setNewTplName(e.target.value)}
                className="w-full px-3.5 py-2 rounded-sm input-field text-sm font-bold"
              />
            </div>
            <div className="sm:col-span-2">
              <label className="block text-main mb-1 font-bold">Beschreibung (optional)</label>
              <input
                type="text"
                placeholder="z. B. Inklusive 2 Barschichten und Aufbau"
                value={newTplDesc}
                onChange={(e) => setNewTplDesc(e.target.value)}
                className="w-full px-3.5 py-2 rounded-sm input-field text-xs"
              />
            </div>
          </div>

          {/* Task Areas & Preset Shifts Builder */}
          <div className="space-y-4 pt-2">
            {newTplTaskAreas.map((ta, taIdx) => (
              <div key={taIdx} className="p-4 rounded-sm bg-subtle border border-grid space-y-3">
                <div className="flex items-center justify-between gap-2">
                  <input
                    type="text"
                    required
                    placeholder="Bereichs-Name (z. B. Bar)"
                    value={ta.name}
                    onChange={(e) => {
                      const updated = [...newTplTaskAreas];
                      updated[taIdx].name = e.target.value;
                      setNewTplTaskAreas(updated);
                    }}
                    className="flex-1 px-3 py-1 rounded-sm input-field text-xs font-bold"
                  />
                  <button
                    type="button"
                    onClick={() => {
                      setNewTplTaskAreas(newTplTaskAreas.filter((_, i) => i !== taIdx));
                    }}
                    className="text-muted hover:text-red-400 p-1"
                  >
                    <Trash2 className="w-4 h-4" />
                  </button>
                </div>

                <div className="pl-3 border-l-2 border-grid space-y-2">
                  <div className="flex items-center justify-between text-muted text-[11px] font-bold">
                    <span>Voreingestellte Schichten</span>
                    <button
                      type="button"
                      onClick={() => addTplShift(taIdx)}
                      className="text-indigo-400 hover:underline flex items-center gap-1"
                    >
                      <Plus className="w-3 h-3" /> Schicht hinzufügen
                    </button>
                  </div>

                  {ta.shifts.map((sh, sIdx) => (
                    <div key={sIdx} className="p-2.5 rounded-sm bg-surface border border-grid grid grid-cols-1 sm:grid-cols-12 gap-2 items-center text-xs">
                      <input
                        type="text"
                        placeholder="Schichtname"
                        value={sh.title}
                        onChange={(e) => {
                          const updated = [...newTplTaskAreas];
                          updated[taIdx].shifts[sIdx].title = e.target.value;
                          setNewTplTaskAreas(updated);
                        }}
                        className="sm:col-span-4 px-2 py-1 rounded-sm input-field text-xs"
                      />
                      <input
                        type="text"
                        placeholder="Start (18:00)"
                        value={sh.start_time}
                        onChange={(e) => {
                          const updated = [...newTplTaskAreas];
                          updated[taIdx].shifts[sIdx].start_time = e.target.value;
                          setNewTplTaskAreas(updated);
                        }}
                        className="sm:col-span-2 px-2 py-1 rounded-sm input-field text-xs"
                      />
                      <input
                        type="text"
                        placeholder="Ende (22:00)"
                        value={sh.end_time}
                        onChange={(e) => {
                          const updated = [...newTplTaskAreas];
                          updated[taIdx].shifts[sIdx].end_time = e.target.value;
                          setNewTplTaskAreas(updated);
                        }}
                        className="sm:col-span-2 px-2 py-1 rounded-sm input-field text-xs"
                      />
                      <div className="sm:col-span-3 flex items-center gap-1">
                        <span className="text-[10px] text-muted">Plätze:</span>
                        <input
                          type="number"
                          min="1"
                          value={sh.max_participants}
                          onChange={(e) => {
                            const updated = [...newTplTaskAreas];
                            updated[taIdx].shifts[sIdx].max_participants = parseInt(e.target.value) || 1;
                            setNewTplTaskAreas(updated);
                          }}
                          className="w-full px-2 py-1 rounded-sm input-field text-xs font-bold"
                        />
                      </div>
                      <button
                        type="button"
                        onClick={() => {
                          const updated = [...newTplTaskAreas];
                          updated[taIdx].shifts = updated[taIdx].shifts.filter((_, i) => i !== sIdx);
                          setNewTplTaskAreas(updated);
                        }}
                        className="sm:col-span-1 p-1 text-muted hover:text-red-400 text-center"
                      >
                        <Trash2 className="w-3.5 h-3.5 mx-auto" />
                      </button>
                    </div>
                  ))}
                </div>
              </div>
            ))}
          </div>

          <div className="pt-3 flex justify-end gap-2 border-t border-grid">
            <button
              type="button"
              onClick={() => setShowCreateTemplate(false)}
              className="px-4 py-2 rounded-sm text-xs font-semibold text-muted hover:bg-surface-hover transition border border-grid"
            >
              Abbrechen
            </button>
            <button
              type="submit"
              disabled={submitting}
              style={{ backgroundColor: 'var(--brand-primary)' }}
              className="px-5 py-2 rounded-sm text-xs font-mono font-bold text-white transition hover:brightness-110 shadow-sm flex items-center gap-1.5"
            >
              <CheckCircle2 className="w-4 h-4" /> Vorlage Speichern
            </button>
          </div>
        </form>
      )}

      {/* Templates Grid */}
      <div className="space-y-4 font-mono text-xs">
        <h3 className="font-serif text-lg font-bold uppercase text-main">Verfügbare Vorlagen</h3>

        {loading ? (
          <div className="hallmark-panel p-8 text-center text-muted">Lade Vorlagen...</div>
        ) : templates.length === 0 ? (
          <div className="hallmark-panel p-8 text-center text-muted italic">
            [ Noch keine Vorlagen gespeichert. Klicke oben auf "Neue Vorlage Erstellen" um Vorlagen mit voreingestellten Schichten anzulegen. ]
          </div>
        ) : (
          <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
            {templates.map((tpl) => (
              <div
                key={tpl.id}
                onClick={() => handleSelectTemplate(tpl)}
                className={`hallmark-panel p-5 rounded-sm border cursor-pointer transition space-y-3 ${
                  selectedTemplate?.id === tpl.id
                    ? 'bg-indigo-500/10 border-indigo-500 shadow-md'
                    : 'bg-surface border-grid hover:border-muted'
                }`}
              >
                <div className="flex items-center justify-between">
                  <h4 className="font-serif text-base font-bold text-main uppercase">{tpl.name}</h4>
                  {selectedTemplate?.id === tpl.id && (
                    <span className="text-[10px] font-mono text-indigo-400 bg-indigo-500/20 px-2 py-0.5 rounded-sm border border-indigo-500/30 font-bold">
                      [ AUSGEWÄHLT ]
                    </span>
                  )}
                </div>

                {tpl.description && <p className="text-xs font-sans text-muted">{tpl.description}</p>}

                {/* Render Preset Task Areas & Preset Shifts */}
                {tpl.template_data?.task_areas && tpl.template_data.task_areas.length > 0 && (
                  <div className="space-y-2 pt-2 border-t border-grid text-[11px]">
                    <div className="font-bold text-muted uppercase text-[10px]">📌 Voreingestellte Schichten:</div>
                    {tpl.template_data.task_areas.map((ta, idx) => (
                      <div key={idx} className="bg-subtle p-2 rounded-sm border border-grid space-y-1">
                        <div className="font-bold text-main flex items-center justify-between">
                          <span>{ta.name}</span>
                          <span className="text-[10px] text-muted font-normal">{ta.shifts?.length || 0} Schichten</span>
                        </div>
                        <div className="flex flex-wrap gap-1">
                          {(ta.shifts || []).map((sh, sIdx) => (
                            <span key={sIdx} className="px-2 py-0.5 rounded-sm bg-surface border border-grid text-[10px] flex items-center gap-1 font-mono">
                              <Clock className="w-3 h-3 text-muted shrink-0" />
                              <strong className="text-main">{sh.title}</strong>
                              <span className="text-muted">({sh.start_time}-{sh.end_time}, {sh.max_participants} Plätze)</span>
                            </span>
                          ))}
                        </div>
                      </div>
                    ))}
                  </div>
                )}

                <div className="text-[10px] text-muted pt-2 border-t border-grid flex items-center justify-between">
                  <span>Erstellt von: <strong>{tpl.created_by_name || 'Admin'}</strong></span>
                  <span className="text-indigo-400 font-bold">Klick zum Auswählen</span>
                </div>
              </div>
            ))}
          </div>
        )}
      </div>

      {/* Instantiation Form */}
      {selectedTemplate && (
        <form onSubmit={handleInstantiate} className="hallmark-panel rounded-sm p-6 border border-grid space-y-4 font-mono text-xs animate-in fade-in duration-200">
          <h3 className="font-serif text-lg font-bold uppercase text-indigo-400 border-b border-grid pb-2">
            Neue Veranstaltung aus Vorlage "{selectedTemplate.name}" Erstellen
          </h3>

          <div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
            <div className="sm:col-span-2">
              <label className="block text-main mb-1 font-bold">Titel der Veranstaltung</label>
              <input
                type="text"
                required
                value={title}
                onChange={(e) => setTitle(e.target.value)}
                className="w-full px-3.5 py-2 rounded-sm input-field text-sm font-bold"
              />
            </div>

            <div>
              <label className="block text-main mb-1 font-bold">Startdatum</label>
              <input
                type="date"
                required
                value={startDate}
                onChange={(e) => setStartDate(e.target.value)}
                className="w-full px-3.5 py-2 rounded-sm input-field text-xs"
              />
            </div>

            <div>
              <label className="block text-main mb-1 font-bold">Enddatum</label>
              <input
                type="date"
                required
                value={endDate}
                onChange={(e) => setEndDate(e.target.value)}
                className="w-full px-3.5 py-2 rounded-sm input-field text-xs"
              />
            </div>

            <div className="sm:col-span-2">
              <label className="block text-main mb-1 font-bold">Ort (optional)</label>
              <input
                type="text"
                value={location}
                onChange={(e) => setLocation(e.target.value)}
                placeholder="z. B. Großer Saal"
                className="w-full px-3.5 py-2 rounded-sm input-field text-xs"
              />
            </div>
          </div>

          <div className="pt-3 flex justify-end border-t border-grid">
            <button
              type="submit"
              disabled={submitting}
              style={{ backgroundColor: 'var(--brand-primary)' }}
              className="px-6 py-2.5 rounded-sm text-xs font-mono font-bold text-white transition hover:brightness-110 shadow-sm flex items-center gap-2"
            >
              <Play className="w-4 h-4 fill-white" />
              {submitting ? 'Erstellen...' : 'Veranstaltung Jetzt Aus Vorlage Erstellen'}
            </button>
          </div>
        </form>
      )}
    </div>
  );
}
