"use client";

import { useState } from "react";
import { PlusCircle } from "lucide-react";
import { saveQuestion } from "@/lib/actions";
import { ActionForm, SubmitBtn } from "@/components/ui-client";

const inputCls =
  "w-full rounded-xl border border-emerald-900/15 bg-white/90 px-3.5 py-2.5 text-sm text-emerald-950 placeholder:text-emerald-900/35 outline-none transition focus:border-emerald-600 focus:ring-2 focus:ring-emerald-500/25";

export function QuestionForm({ lessonId }: { lessonId: number }) {
  const [type, setType] = useState("mcq");
  return (
    <ActionForm action={saveQuestion} className="grid gap-3" resetOnSuccess>
      <input type="hidden" name="lessonId" value={lessonId} />
      <div className="grid gap-3 sm:grid-cols-3">
        <label className="block">
          <span className="mb-1 block text-xs font-extrabold text-emerald-900/80">نوع سؤال</span>
          <select name="type" value={type} onChange={(e) => setType(e.target.value)} className={inputCls}>
            <option value="mcq">تستی (چهارگزینه‌ای)</option>
            <option value="tf">صحیح / غلط</option>
            <option value="short">کوتاه‌پاسخ</option>
            <option value="desc">تشریحی</option>
          </select>
        </label>
        <label className="block">
          <span className="mb-1 block text-xs font-extrabold text-emerald-900/80">سختی</span>
          <select name="difficulty" className={inputCls} defaultValue="1">
            <option value="1">آسان</option>
            <option value="2">متوسط</option>
            <option value="3">سخت</option>
          </select>
        </label>
      </div>
      <label className="block">
        <span className="mb-1 block text-xs font-extrabold text-emerald-900/80">متن سؤال</span>
        <textarea name="text" required rows={2} className={inputCls} placeholder="سؤال را بنویسید..." />
      </label>
      {type === "mcq" && (
        <>
          <div className="grid gap-2 sm:grid-cols-2">
            {[1, 2, 3, 4].map((n) => (
              <input key={n} name={`opt${n}`} placeholder={`گزینهٔ ${n}`} required={n <= 2} className={inputCls} />
            ))}
          </div>
          <label className="block">
            <span className="mb-1 block text-xs font-extrabold text-emerald-900/80">پاسخ صحیح (دقیقاً متن یکی از گزینه‌ها)</span>
            <input name="answer" required className={inputCls} />
          </label>
        </>
      )}
      {type === "tf" && (
        <div className="flex gap-4 rounded-xl border border-emerald-900/10 bg-emerald-50/60 p-3">
          <label className="flex items-center gap-2 text-sm font-bold"><input type="radio" name="tfAnswer" value="صحیح" defaultChecked className="size-4 accent-emerald-700" /> صحیح</label>
          <label className="flex items-center gap-2 text-sm font-bold"><input type="radio" name="tfAnswer" value="غلط" className="size-4 accent-emerald-700" /> غلط</label>
        </div>
      )}
      {(type === "short" || type === "desc") && (
        <label className="block">
          <span className="mb-1 block text-xs font-extrabold text-emerald-900/80">پاسخ {type === "desc" ? "(راهنمای تصحیح)" : "(دقیق)"}</span>
          <input name="answer" className={inputCls} required={type === "short"} placeholder={type === "desc" ? "نکات تصحیح — اختیاری" : "پاسخ دقیق"} />
        </label>
      )}
      <div>
        <SubmitBtn><PlusCircle size={16} /> ثبت سؤال در بانک</SubmitBtn>
      </div>
    </ActionForm>
  );
}
