"use client";

import { useActionState } from "react";
import { createFaqItemAction } from "@/lib/actions/faq";

export function NewFaqItemForm() {
  const [state, formAction, pending] = useActionState(createFaqItemAction, {});

  return (
    <form action={formAction} className="bg-[var(--rk-surface)] border border-white/10 rounded-2xl p-5 flex flex-col gap-3 mb-8">
      <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
        <input
          name="question"
          placeholder="Frage (Deutsch)"
          required
          className="bg-transparent border border-white/15 rounded-lg px-3 py-2 text-sm focus:outline-none focus:border-white/50"
        />
        <input
          name="category"
          placeholder="Kategorie, z. B. Preise & Bezahlung"
          defaultValue="Allgemein"
          className="bg-transparent border border-white/15 rounded-lg px-3 py-2 text-sm focus:outline-none focus:border-white/50"
        />
      </div>
      <textarea
        name="answer"
        placeholder="Antwort (Deutsch)"
        required
        rows={3}
        className="bg-transparent border border-white/15 rounded-lg px-3 py-2.5 text-sm focus:outline-none focus:border-white/50 resize-y"
      />
      <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
        <input
          name="questionEn"
          placeholder="Frage (Englisch, optional)"
          className="bg-transparent border border-white/15 rounded-lg px-3 py-2 text-sm focus:outline-none focus:border-white/50"
        />
        <input
          name="categoryEn"
          placeholder="Kategorie (Englisch, optional)"
          className="bg-transparent border border-white/15 rounded-lg px-3 py-2 text-sm focus:outline-none focus:border-white/50"
        />
      </div>
      <textarea
        name="answerEn"
        placeholder="Antwort (Englisch, optional)"
        rows={3}
        className="bg-transparent border border-white/15 rounded-lg px-3 py-2.5 text-sm focus:outline-none focus:border-white/50 resize-y"
      />
      <label className="flex items-center gap-2 text-sm">
        <input type="checkbox" name="visible" defaultChecked className="h-3.5 w-3.5" />
        Sofort sichtbar auf der öffentlichen FAQ-Seite
      </label>
      <button
        type="submit"
        disabled={pending}
        className="self-start border border-white/30 rounded-lg px-4 py-2 text-xs uppercase tracking-[0.15em] hover:border-white disabled:opacity-50"
      >
        Frage anlegen
      </button>
      {state.error && <p className="text-sm text-red-400">{state.error}</p>}
    </form>
  );
}
