import { useEffect, useRef, useState } from "react";
import { Plus, Save, Trash2 } from "lucide-react";
import { api } from "../api.js";
import { Button, ErrorNote, Field, Panel, Spinner, timeAgo } from "../ui.jsx";
/** Client page tab: hashtag sets, approved phrases/CTAs, words to avoid, voice notes. */
export default function BrandKit({ slug }) {
const [kit, setKit] = useState(null);
const [sets, setSets] = useState([]);
const [phrases, setPhrases] = useState("");
const [avoid, setAvoid] = useState("");
const [notes, setNotes] = useState("");
const [busy, setBusy] = useState(false);
const [error, setError] = useState(null);
const [saved, setSaved] = useState(false);
const apply = (k) => { setKit(k); setSets(k.hashtagSets.map((s) => ({ name: s.name, tags: s.tags.join(" ") }))); setPhrases(k.phrases.join("\n")); setAvoid(k.avoid.join("\n")); setNotes(k.notes); };
useEffect(() => { api(`/api/brandkit/${slug}`).then(apply).catch(setError); }, [slug]);
const save = async () => {
setBusy(true); setError(null); setSaved(false);
try { apply(await api(`/api/brandkit/${slug}`, { method: "PUT", body: { hashtagSets: sets, phrases, avoid, notes } })); setSaved(true); } catch (e) { setError(e); }
setBusy(false);
};
if (!kit) return error ? : ;
return (
{saved && Saved.}
{kit.updatedAt && !saved && Last changed {timeAgo(kit.updatedAt)} by {kit.updatedBy}}
);
}
/** Under a caption box: insert hashtag sets/phrases and show live brand checks. */
export function CaptionHelper({ client, caption, platforms, onInsert }) {
const [kit, setKit] = useState(null);
const [warnings, setWarnings] = useState([]);
const timer = useRef(null);
useEffect(() => { setKit(null); if (client) api(`/api/brandkit/${client}`).then(setKit).catch(() => {}); }, [client]);
useEffect(() => {
clearTimeout(timer.current);
if (!client || !caption?.trim()) { setWarnings([]); return; }
timer.current = setTimeout(() => api(`/api/brandkit/${client}/check`, { method: "POST", body: { caption, platforms } }).then((r) => setWarnings(r.warnings)).catch(() => {}), 500);
return () => clearTimeout(timer.current);
}, [client, caption, platforms.join(",")]); // eslint-disable-line react-hooks/exhaustive-deps
return (
{kit && (kit.hashtagSets.length > 0 || kit.phrases.length > 0) && (
Insert:
{kit.hashtagSets.map((s) => )}
{kit.phrases.slice(0, 6).map((p) => )}
)}
{warnings.map((w) =>
{w.level === "bad" ? "Fix: " : ""}{w.text}
)}
);
}