import { useState, useEffect } from "react";
import { useNavigate, useParams } from "react-router-dom";
import api from "../../api/client";
import { useAuth } from "../../auth/AuthContext";

const CONTACT_TYPES = ["email", "phone", "whatsapp", "other"];
const LANGUAGES = [
  { value: "el", label: "Greek" },
  { value: "en", label: "English" },
  { value: "de", label: "German" },
  { value: "fr", label: "French" },
  { value: "it", label: "Italian" },
];
const TITLES = [
  { value: "", label: "—" },
  { value: "Fr.", label: "Father" },
  { value: "Rev.", label: "Reverend" },
  { value: "Archim.", label: "Archimandrite" },
  { value: "Bp.", label: "Bishop" },
  { value: "Abp.", label: "Archbishop" },
  { value: "Met.", label: "Metropolitan" },
  { value: "Mr.", label: "Mister" },
  { value: "Mrs.", label: "Missus" },
  { value: "Ms.", label: "Ms." },
  { value: "Dr.", label: "Doctor" },
  { value: "Prof.", label: "Professor" },
];
const PRESET_TAGS = ["church", "monastery", "municipality", "school", "repeat-customer", "vip", "pending", "inactive"];
const REL_STATUS_OPTIONS = [
  { value: "lead",     label: "Lead" },
  { value: "prospect", label: "Prospect" },
  { value: "active",   label: "Active" },
  { value: "inactive", label: "Inactive" },
  { value: "churned",  label: "Churned" },
];

const CONTACT_TYPE_ICONS = {
  email: "📧",
  phone: "📞",
  whatsapp: "💬",
  other: "🔗",
};

const inputClass = "w-full px-3 py-2 text-sm rounded-md border";
const inputStyle = {
  backgroundColor: "var(--bg-input)",
  borderColor: "var(--border-primary)",
  color: "var(--text-primary)",
};
const labelStyle = {
  display: "block",
  marginBottom: 4,
  fontSize: 12,
  color: "var(--text-secondary)",
  fontWeight: 500,
};

function Field({ label, children, style }) {
  return (
    <div style={style}>
      <label style={labelStyle}>{label}</label>
      {children}
    </div>
  );
}

function SectionCard({ title, children }) {
  return (
    <div
      className="rounded-lg border p-5 mb-4"
      style={{ backgroundColor: "var(--bg-card)", borderColor: "var(--border-primary)" }}
    >
      <h2 className="text-sm font-semibold mb-4" style={{ color: "var(--text-heading)" }}>{title}</h2>
      {children}
    </div>
  );
}

const emptyContact = () => ({ type: "email", label: "", value: "", primary: false });

export default function CustomerForm() {
  const { id } = useParams();
  const isEdit = Boolean(id);
  const navigate = useNavigate();
  const { user, hasPermission } = useAuth();
  const canEdit = hasPermission("crm", "edit");

  const [form, setForm] = useState({
    title: "",
    name: "",
    surname: "",
    organization: "",
    religion: "",
    language: "el",
    relationship_status: "lead",
    tags: [],
    folder_id: "",
    location: { address: "", city: "", postal_code: "", region: "", country: "" },
    contacts: [],
    notes: [],
  });
  const [loading, setLoading] = useState(isEdit);
  const [saving, setSaving] = useState(false);
  const [error, setError] = useState("");
  const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
  const [deleteOpts, setDeleteOpts] = useState({ wipe_comms: false, wipe_files: false, wipe_everything: false });
  const [tagInput, setTagInput] = useState("");
  const [allTags, setAllTags] = useState([]);
  const [newNoteText, setNewNoteText] = useState("");
  const [editingNoteIdx, setEditingNoteIdx] = useState(null);
  const [editingNoteText, setEditingNoteText] = useState("");

  useEffect(() => {
    api.get("/crm/customers/tags").then(setAllTags).catch(() => {});
  }, []);

  useEffect(() => {
    if (!isEdit) return;
    api.get(`/crm/customers/${id}`)
      .then((data) => {
        setForm({
          title: data.title || "",
          name: data.name || "",
          surname: data.surname || "",
          organization: data.organization || "",
          religion: data.religion || "",
          language: data.language || "el",
          relationship_status: data.relationship_status || "lead",
          tags: data.tags || [],
          folder_id: data.folder_id || "",
          location: {
            address: data.location?.address || "",
            city: data.location?.city || "",
            postal_code: data.location?.postal_code || "",
            region: data.location?.region || "",
            country: data.location?.country || "",
          },
          contacts: data.contacts || [],
          notes: data.notes || [],
        });
      })
      .catch((err) => setError(err.message))
      .finally(() => setLoading(false));
  }, [id, isEdit]);

  const set = (field, value) => setForm((f) => ({ ...f, [field]: value }));
  const setLoc = (field, value) => setForm((f) => ({ ...f, location: { ...f.location, [field]: value } }));

  // Tags
  const addTag = (raw) => {
    const tag = raw.trim();
    if (tag && !form.tags.includes(tag)) {
      set("tags", [...form.tags, tag]);
    }
    setTagInput("");
  };
  const removeTag = (tag) => set("tags", form.tags.filter((t) => t !== tag));

  // Contacts
  const addContact = () => set("contacts", [...form.contacts, emptyContact()]);
  const removeContact = (i) => set("contacts", form.contacts.filter((_, idx) => idx !== i));
  const setContact = (i, field, value) => {
    const updated = form.contacts.map((c, idx) => idx === i ? { ...c, [field]: value } : c);
    set("contacts", updated);
  };
  const setPrimaryContact = (i) => {
    const type = form.contacts[i].type;
    const updated = form.contacts.map((c, idx) => ({
      ...c,
      primary: c.type === type ? idx === i : c.primary,
    }));
    set("contacts", updated);
  };

  // Notes
  const addNote = () => {
    if (!newNoteText.trim()) return;
    const note = {
      text: newNoteText.trim(),
      by: user?.name || "unknown",
      at: new Date().toISOString(),
    };
    set("notes", [...form.notes, note]);
    setNewNoteText("");
  };

  const removeNote = (i) => {
    set("notes", form.notes.filter((_, idx) => idx !== i));
    if (editingNoteIdx === i) setEditingNoteIdx(null);
  };

  const startEditNote = (i) => {
    setEditingNoteIdx(i);
    setEditingNoteText(form.notes[i].text);
  };

  const saveEditNote = (i) => {
    if (!editingNoteText.trim()) return;
    const updated = form.notes.map((n, idx) =>
      idx === i ? { ...n, text: editingNoteText.trim(), at: new Date().toISOString() } : n
    );
    set("notes", updated);
    setEditingNoteIdx(null);
  };

  const buildPayload = () => ({
    title: form.title.trim() || null,
    name: form.name.trim(),
    surname: form.surname.trim() || null,
    organization: form.organization.trim() || null,
    religion: form.religion.trim() || null,
    language: form.language,
    relationship_status: form.relationship_status || "lead",
    tags: form.tags,
    ...(!isEdit && { folder_id: form.folder_id.trim().toLowerCase() }),
    location: {
      address: form.location.address.trim() || null,
      city: form.location.city.trim() || null,
      postal_code: form.location.postal_code.trim() || null,
      region: form.location.region.trim() || null,
      country: form.location.country.trim() || null,
    },
    contacts: form.contacts.filter((c) => c.value.trim()),
    notes: form.notes,
  });

  const handleSave = async () => {
    if (!form.name.trim()) { setError("Customer name is required."); return; }
    if (!isEdit && !form.folder_id.trim()) { setError("Internal Folder ID is required."); return; }
    if (!isEdit && !/^[a-z0-9][a-z0-9\-]*[a-z0-9]$/.test(form.folder_id.trim().toLowerCase())) {
      setError("Internal Folder ID must contain only lowercase letters, numbers, and hyphens, and cannot start or end with a hyphen.");
      return;
    }
    setSaving(true);
    setError("");
    try {
      if (isEdit) {
        await api.put(`/crm/customers/${id}`, buildPayload());
        navigate(`/crm/customers/${id}`);
      } else {
        const res = await api.post("/crm/customers", buildPayload());
        navigate(`/crm/customers/${res.id}`);
      }
    } catch (err) {
      setError(err.message);
    } finally {
      setSaving(false);
    }
  };

  const handleDelete = async () => {
    setSaving(true);
    try {
      const wipeEverything = deleteOpts.wipe_everything;
      const params = new URLSearchParams();
      if (wipeEverything || deleteOpts.wipe_comms) params.set("wipe_comms", "true");
      if (wipeEverything || deleteOpts.wipe_files) params.set("wipe_files", "true");
      if (wipeEverything) params.set("wipe_nextcloud", "true");
      const qs = params.toString();
      await api.delete(`/crm/customers/${id}${qs ? `?${qs}` : ""}`);
      navigate("/crm/customers");
    } catch (err) {
      setError(err.message);
      setSaving(false);
    }
  };

  const toggleDeleteOpt = (key) => {
    setDeleteOpts((prev) => {
      if (key === "wipe_everything") {
        const next = !prev.wipe_everything;
        // Checking "wipe everything" also checks the other two for clarity,
        // but unchecking it does NOT uncheck them.
        if (next) return { wipe_comms: true, wipe_files: true, wipe_everything: true };
        return { ...prev, wipe_everything: false };
      }
      return { ...prev, [key]: !prev[key] };
    });
  };

  if (loading) {
    return <div className="text-center py-8" style={{ color: "var(--text-muted)" }}>Loading...</div>;
  }

  return (
    <div style={{ maxWidth: 800, margin: "0 auto" }}>
      <div className="flex items-center justify-between mb-6">
        <h1 className="text-2xl font-bold" style={{ color: "var(--text-heading)" }}>
          {isEdit ? "Edit Customer" : "New Customer"}
        </h1>
        <div className="flex gap-2">
          <button
            onClick={() => navigate(isEdit ? `/crm/customers/${id}` : "/crm/customers")}
            className="px-4 py-2 text-sm rounded-md border cursor-pointer"
            style={{ borderColor: "var(--border-primary)", color: "var(--text-secondary)" }}
          >
            Cancel
          </button>
          {canEdit && (
            <button
              onClick={handleSave}
              disabled={saving}
              className="px-4 py-2 text-sm rounded-md hover:opacity-90 transition-colors cursor-pointer"
              style={{ backgroundColor: "var(--btn-primary)", color: "var(--text-white)", opacity: saving ? 0.7 : 1 }}
            >
              {saving ? "Saving..." : "Save"}
            </button>
          )}
        </div>
      </div>

      {error && (
        <div
          className="text-sm rounded-md p-3 mb-4 border"
          style={{ backgroundColor: "var(--danger-bg)", borderColor: "var(--danger)", color: "var(--danger-text)" }}
        >
          {error}
        </div>
      )}

      {/* Basic Info */}
      <SectionCard title="Basic Info">
        {/* Row 1: Title, Name, Surname */}
        <div style={{ display: "grid", gridTemplateColumns: "140px 1fr 1fr", gap: 16, marginBottom: 16 }}>
          <Field label="Title">
            <select className={inputClass} style={inputStyle} value={form.title}
              onChange={(e) => set("title", e.target.value)}>
              {TITLES.map((t) => <option key={t.value} value={t.value}>{t.label}</option>)}
            </select>
          </Field>
          <Field label="Name *">
            <input className={inputClass} style={inputStyle} value={form.name}
              onChange={(e) => set("name", e.target.value)} placeholder="First name" />
          </Field>
          <Field label="Surname">
            <input className={inputClass} style={inputStyle} value={form.surname}
              onChange={(e) => set("surname", e.target.value)} placeholder="Last name" />
          </Field>
        </div>

        {/* Row 2: Relationship Status + Organization + Religion + Language + Folder ID */}
        <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr 1fr 1fr 1fr", gap: 16, marginBottom: 16 }}>
          <Field label="Relationship Status">
            <select className={inputClass} style={inputStyle} value={form.relationship_status}
              onChange={(e) => set("relationship_status", e.target.value)}>
              {REL_STATUS_OPTIONS.map((o) => <option key={o.value} value={o.value}>{o.label}</option>)}
            </select>
          </Field>
          <Field label="Organization">
            <input className={inputClass} style={inputStyle} value={form.organization}
              onChange={(e) => set("organization", e.target.value)} placeholder="Church, organization, etc." />
          </Field>
          <Field label="Religion">
            <input className={inputClass} style={inputStyle} value={form.religion}
              onChange={(e) => set("religion", e.target.value)} placeholder="e.g. Christian Orthodox" />
          </Field>
          <Field label="Language">
            <select className={inputClass} style={inputStyle} value={form.language}
              onChange={(e) => set("language", e.target.value)}>
              {LANGUAGES.map((l) => <option key={l.value} value={l.value}>{l.label}</option>)}
            </select>
          </Field>
          {!isEdit ? (
            <Field label="Folder ID *">
              <input
                className={inputClass}
                style={inputStyle}
                value={form.folder_id}
                onChange={(e) => set("folder_id", e.target.value.toLowerCase().replace(/[^a-z0-9\-]/g, ""))}
                placeholder="e.g. saint-john-corfu"
              />
            </Field>
          ) : (
            <div>
              <div style={{ fontSize: 12, color: "var(--text-muted)", marginBottom: 4 }}>Folder ID</div>
              <div style={{ fontSize: 13, color: "var(--text-primary)", padding: "6px 0" }}>{form.folder_id || "—"}</div>
            </div>
          )}
        </div>
        {!isEdit && (
          <p className="text-xs" style={{ color: "var(--text-muted)", marginTop: -8, marginBottom: 16 }}>
            Lowercase letters, numbers and hyphens only. This becomes the Nextcloud folder name and cannot be changed later.
          </p>
        )}

        {/* Row 3: Tags */}
        <div>
          <label style={labelStyle}>Tags</label>
          <div className="flex flex-wrap gap-1.5 mb-2">
            {form.tags.map((tag) => (
              <span
                key={tag}
                className="flex items-center gap-1 px-2 py-0.5 text-xs rounded-full cursor-pointer"
                style={{ backgroundColor: "var(--bg-card-hover)", color: "var(--text-secondary)" }}
                onClick={() => removeTag(tag)}
                title="Click to remove"
              >
                {tag} ×
              </span>
            ))}
          </div>
          {/* Preset quick-add tags */}
          <div className="flex flex-wrap gap-1.5 mb-2">
            {PRESET_TAGS.filter((t) => !form.tags.includes(t)).map((t) => (
              <button
                key={t}
                type="button"
                onClick={() => addTag(t)}
                className="px-2 py-0.5 text-xs rounded-full border cursor-pointer hover:opacity-80"
                style={{ borderColor: "var(--border-primary)", color: "var(--text-muted)" }}
              >
                + {t}
              </button>
            ))}
          </div>
          <div style={{ position: "relative" }}>
            <input
              className={inputClass}
              style={inputStyle}
              value={tagInput}
              onChange={(e) => setTagInput(e.target.value)}
              onKeyDown={(e) => {
                const suggestions = allTags.filter(
                  (t) => t.toLowerCase().startsWith(tagInput.toLowerCase()) && !form.tags.includes(t)
                );
                if (e.key === "Tab" && suggestions.length > 0) {
                  e.preventDefault();
                  addTag(suggestions[0]);
                } else if (e.key === "Enter" || e.key === ",") {
                  e.preventDefault();
                  addTag(tagInput);
                } else if (e.key === "Escape") {
                  setTagInput("");
                }
              }}
              onBlur={(e) => {
                // Delay so suggestion clicks register first
                setTimeout(() => {
                  if (tagInput.trim()) addTag(tagInput);
                }, 150);
              }}
              placeholder="Type a custom tag and press Enter, comma, or Tab to autocomplete..."
            />
            {tagInput.trim().length > 0 && (() => {
              const suggestions = allTags.filter(
                (t) => t.toLowerCase().startsWith(tagInput.toLowerCase()) && !form.tags.includes(t)
              );
              if (suggestions.length === 0) return null;
              return (
                <div
                  style={{
                    position: "absolute",
                    top: "100%",
                    left: 0,
                    right: 0,
                    zIndex: 50,
                    border: "1px solid var(--border-primary)",
                    borderRadius: 6,
                    backgroundColor: "var(--bg-card)",
                    boxShadow: "0 4px 12px rgba(0,0,0,0.15)",
                    marginTop: 2,
                    overflow: "hidden",
                  }}
                >
                  {suggestions.map((s) => (
                    <div
                      key={s}
                      onMouseDown={(e) => { e.preventDefault(); addTag(s); }}
                      style={{
                        padding: "7px 12px",
                        fontSize: 13,
                        cursor: "pointer",
                        color: "var(--text-primary)",
                        borderBottom: "1px solid var(--border-primary)",
                      }}
                      onMouseEnter={(e) => e.currentTarget.style.backgroundColor = "var(--bg-card-hover)"}
                      onMouseLeave={(e) => e.currentTarget.style.backgroundColor = ""}
                    >
                      {s}
                      {s === suggestions[0] && (
                        <span style={{ fontSize: 11, color: "var(--text-muted)", marginLeft: 8 }}>Tab to fill</span>
                      )}
                    </div>
                  ))}
                </div>
              );
            })()}
          </div>
        </div>
      </SectionCard>

      {/* Location */}
      <SectionCard title="Location">
        <div style={{ display: "grid", gridTemplateColumns: "2fr 1fr 1fr", gap: 16, marginBottom: 16 }}>
          <Field label="Address">
            <input className={inputClass} style={inputStyle} value={form.location.address}
              onChange={(e) => setLoc("address", e.target.value)} placeholder="Street address" />
          </Field>
          <Field label="City">
            <input className={inputClass} style={inputStyle} value={form.location.city}
              onChange={(e) => setLoc("city", e.target.value)} placeholder="City" />
          </Field>
          <Field label="Postal Code">
            <input className={inputClass} style={inputStyle} value={form.location.postal_code}
              onChange={(e) => setLoc("postal_code", e.target.value)} placeholder="Postal code" />
          </Field>
        </div>
        <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 16 }}>
          <Field label="Region">
            <input className={inputClass} style={inputStyle} value={form.location.region}
              onChange={(e) => setLoc("region", e.target.value)} placeholder="Region / State" />
          </Field>
          <Field label="Country">
            <input className={inputClass} style={inputStyle} value={form.location.country}
              onChange={(e) => setLoc("country", e.target.value)} placeholder="Country" />
          </Field>
        </div>
      </SectionCard>

      {/* Contacts */}
      <SectionCard title="Contacts">
        {form.contacts.map((c, i) => (
          <div
            key={i}
            className="flex gap-2 mb-2 items-center"
          >
            <span className="text-base w-6 text-center flex-shrink-0">{CONTACT_TYPE_ICONS[c.type] || "🔗"}</span>
            <select
              className="px-2 py-2 text-sm rounded-md border w-32 flex-shrink-0"
              style={inputStyle}
              value={c.type}
              onChange={(e) => setContact(i, "type", e.target.value)}
            >
              {CONTACT_TYPES.map((t) => <option key={t} value={t}>{t}</option>)}
            </select>
            <input
              className="px-2 py-2 text-sm rounded-md border w-28 flex-shrink-0"
              style={inputStyle}
              value={c.label}
              onChange={(e) => setContact(i, "label", e.target.value)}
              placeholder="label (e.g. work)"
            />
            <input
              className={inputClass + " flex-1"}
              style={inputStyle}
              value={c.value}
              onChange={(e) => setContact(i, "value", e.target.value)}
              placeholder="value"
            />
            <label className="flex items-center gap-1 text-xs flex-shrink-0 cursor-pointer" style={{ color: "var(--text-muted)" }}>
              <input
                type="radio"
                name={`primary-${c.type}`}
                checked={!!c.primary}
                onChange={() => setPrimaryContact(i)}
                className="cursor-pointer"
              />
              Primary
            </label>
            <button
              type="button"
              onClick={() => removeContact(i)}
              className="text-xs cursor-pointer hover:opacity-70 flex-shrink-0"
              style={{ color: "var(--danger)" }}
            >
              ×
            </button>
          </div>
        ))}
        <button
          type="button"
          onClick={addContact}
          className="mt-2 px-3 py-1.5 text-xs rounded-md border cursor-pointer hover:opacity-80"
          style={{ borderColor: "var(--border-primary)", color: "var(--text-secondary)" }}
        >
          + Add Contact
        </button>
      </SectionCard>

      {/* Notes */}
      <SectionCard title="Notes">
        {form.notes.length > 0 && (
          <div className="mb-4 space-y-2">
            {form.notes.map((note, i) => (
              <div
                key={i}
                className="px-3 py-2 rounded-md text-sm"
                style={{ backgroundColor: "var(--bg-primary)", color: "var(--text-primary)" }}
              >
                {editingNoteIdx === i ? (
                  <div style={{ display: "flex", flexDirection: "column", gap: 6 }}>
                    <textarea
                      className={inputClass}
                      style={{ ...inputStyle, resize: "vertical", minHeight: 56 }}
                      value={editingNoteText}
                      onChange={(e) => setEditingNoteText(e.target.value)}
                      autoFocus
                      onKeyDown={(e) => {
                        if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) saveEditNote(i);
                        if (e.key === "Escape") setEditingNoteIdx(null);
                      }}
                    />
                    <div style={{ display: "flex", gap: 6 }}>
                      <button
                        type="button"
                        onClick={() => saveEditNote(i)}
                        disabled={!editingNoteText.trim()}
                        className="px-2 py-1 text-xs rounded-md cursor-pointer hover:opacity-90"
                        style={{ backgroundColor: "var(--btn-primary)", color: "var(--text-white)", opacity: editingNoteText.trim() ? 1 : 0.5 }}
                      >Save</button>
                      <button
                        type="button"
                        onClick={() => setEditingNoteIdx(null)}
                        className="px-2 py-1 text-xs rounded-md cursor-pointer hover:opacity-80"
                        style={{ border: "1px solid var(--border-primary)", color: "var(--text-secondary)" }}
                      >Cancel</button>
                    </div>
                  </div>
                ) : (
                  <>
                    <p>{note.text}</p>
                    <div className="flex items-center justify-between mt-1">
                      <p className="text-xs" style={{ color: "var(--text-muted)" }}>
                        {note.by} · {note.at ? new Date(note.at).toLocaleDateString() : ""}
                      </p>
                      <div style={{ display: "flex", gap: 8 }}>
                        <button
                          type="button"
                          onClick={() => startEditNote(i)}
                          className="text-xs cursor-pointer hover:opacity-70"
                          style={{ color: "var(--accent)", background: "none", border: "none", padding: 0 }}
                        >Edit</button>
                        <button
                          type="button"
                          onClick={() => removeNote(i)}
                          className="text-xs cursor-pointer hover:opacity-70"
                          style={{ color: "var(--danger)", background: "none", border: "none", padding: 0 }}
                        >Remove</button>
                      </div>
                    </div>
                  </>
                )}
              </div>
            ))}
          </div>
        )}
        <div className="flex gap-2">
          <textarea
            className={inputClass + " flex-1"}
            style={{ ...inputStyle, resize: "vertical", minHeight: 64 }}
            value={newNoteText}
            onChange={(e) => setNewNoteText(e.target.value)}
            placeholder="Add a note..."
          />
          <button
            type="button"
            onClick={addNote}
            disabled={!newNoteText.trim()}
            className="px-3 py-2 text-sm rounded-md cursor-pointer hover:opacity-80 self-start"
            style={{ backgroundColor: "var(--btn-primary)", color: "var(--text-white)", opacity: newNoteText.trim() ? 1 : 0.5 }}
          >
            Add
          </button>
        </div>
      </SectionCard>

      {/* Delete */}
      {isEdit && canEdit && (
        <div
          className="rounded-lg border p-5"
          style={{ backgroundColor: "var(--bg-card)", borderColor: "var(--border-primary)" }}
        >
          <h2 className="text-sm font-semibold mb-2" style={{ color: "var(--danger)" }}>Danger Zone</h2>
          {!showDeleteConfirm ? (
            <button
              type="button"
              onClick={() => setShowDeleteConfirm(true)}
              className="px-4 py-2 text-sm rounded-md border cursor-pointer hover:opacity-80"
              style={{ borderColor: "var(--danger)", color: "var(--danger)" }}
            >
              Delete Customer
            </button>
          ) : (
            <div>
              <p className="text-sm mb-3" style={{ color: "var(--text-secondary)" }}>
                Are you sure? This cannot be undone.
              </p>
              <div
                className="rounded-md border p-3 mb-3"
                style={{ borderColor: "var(--border-primary)", backgroundColor: "var(--bg-primary)" }}
              >
                {[
                  { key: "wipe_comms", label: "Wipe all communication logs for this customer" },
                  { key: "wipe_files", label: "Wipe all file entries from the database and rename Nextcloud folder to STALE_…" },
                  { key: "wipe_everything", label: "Wipe everything — delete Nextcloud folder entirely (implies both above)" },
                ].map(({ key, label }) => (
                  <label
                    key={key}
                    className="flex items-start gap-2 text-sm cursor-pointer mb-2 last:mb-0"
                    style={{ color: key === "wipe_everything" ? "var(--danger)" : "var(--text-secondary)" }}
                  >
                    <input
                      type="checkbox"
                      checked={deleteOpts[key]}
                      onChange={() => toggleDeleteOpt(key)}
                      className="mt-0.5 cursor-pointer flex-shrink-0"
                    />
                    {label}
                  </label>
                ))}
              </div>
              <div className="flex gap-2">
                <button
                  type="button"
                  onClick={handleDelete}
                  disabled={saving}
                  className="px-4 py-2 text-sm rounded-md cursor-pointer hover:opacity-80"
                  style={{ backgroundColor: "var(--danger)", color: "#fff", opacity: saving ? 0.7 : 1 }}
                >
                  {saving ? "Deleting..." : "Yes, Delete"}
                </button>
                <button
                  type="button"
                  onClick={() => { setShowDeleteConfirm(false); setDeleteOpts({ wipe_comms: false, wipe_files: false, wipe_everything: false }); }}
                  className="px-4 py-2 text-sm rounded-md border cursor-pointer"
                  style={{ borderColor: "var(--border-primary)", color: "var(--text-secondary)" }}
                >
                  Cancel
                </button>
              </div>
            </div>
          )}
        </div>
      )}
    </div>
  );
}
