import { useState } from "react";
import { useApp } from "../AppContext";
import { BackHeader } from "../components/ui";

export default function Dispute() {
  const { params, goBack } = useApp();
  const [desc, setDesc] = useState("");
  const [files, setFiles] = useState<string[]>([]);
  const [submitted, setSubmitted] = useState(false);

  const addFile = () => setFiles((f) => [...f, `مستند-${f.length + 1}.pdf`]);

  if (submitted) {
    return (
      <div className="min-h-full flex flex-col">
        <BackHeader title="فتح نزاع" onBack={goBack} />
        <div className="flex-1 flex flex-col items-center justify-center px-6 text-center pb-16">
          <div className="w-24 h-24 rounded-full bg-accent-100 flex items-center justify-center mb-5">
            <i className="ri-check-double-line text-[44px] text-accent-700 leading-none" />
          </div>
          <h1 className="text-[20px] font-bold text-foreground-950">تم إرسال النزاع للإدارة</h1>
          <p className="text-[13px] text-foreground-600 mt-2 max-w-[280px] leading-relaxed">
            سنراجع تفاصيل النزاع على الطلب #{params.orderId ?? "1045"} ونبلغك بالنتيجة خلال 48 ساعة.
          </p>
          <button
            type="button"
            onClick={goBack}
            className="mt-8 h-[50px] px-8 rounded-[14px] bg-primary-500 text-background-50 text-[14px] font-semibold cursor-pointer active:scale-[0.98] transition-transform"
          >
            العودة للطلب
          </button>
        </div>
      </div>
    );
  }

  return (
    <div className="min-h-full flex flex-col">
      <BackHeader title="فتح نزاع" subtitle={`الطلب #${params.orderId ?? "1045"}`} onBack={goBack} />

      <div className="px-4 py-4 space-y-5">
        <div>
          <label className="text-[14px] font-semibold text-foreground-950 block mb-2">وصف المشكلة</label>
          <div className="rounded-[18px] bg-background-100 border border-background-200 p-4 focus-within:border-foreground-400 transition-colors">
            <textarea
              value={desc}
              onChange={(e) => setDesc(e.target.value.slice(0, 500))}
              rows={6}
              maxLength={500}
              placeholder="اشرح المشكلة بالتفصيل، مثل: تأخر التوصيل، منتج غير مطابق للوصف، تلف في الشحنة..."
              className="w-full bg-transparent text-[13.5px] text-foreground-950 placeholder:text-foreground-500 outline-none resize-none font-body leading-relaxed"
            />
            <div className="text-left text-[10.5px] text-foreground-400 mt-1">{desc.length}/500</div>
          </div>
        </div>

        <div>
          <label className="text-[14px] font-semibold text-foreground-950 block mb-2">إرفاق مستندات</label>
          <button
            type="button"
            onClick={addFile}
            className="w-full rounded-[18px] border-2 border-dashed border-background-300 bg-background-100 p-6 flex flex-col items-center gap-2 cursor-pointer hover:border-foreground-400 transition-colors"
          >
            <div className="w-12 h-12 rounded-full bg-background-200 flex items-center justify-center">
              <i className="ri-upload-cloud-2-line text-[22px] text-foreground-700 leading-none" />
            </div>
            <span className="text-[13px] font-medium text-foreground-800">اضغط لإرفاق صور أو ملفات</span>
            <span className="text-[11px] text-foreground-500">PNG, JPG, PDF حتى 10 ميجابايت</span>
          </button>
          {files.length > 0 && (
            <div className="mt-3 space-y-2">
              {files.map((f) => (
                <div key={f} className="flex items-center gap-3 rounded-[14px] bg-background-100 border border-background-200 p-3">
                  <div className="w-9 h-9 rounded-[10px] bg-background-200 flex items-center justify-center shrink-0">
                    <i className="ri-file-text-line text-[17px] text-foreground-700 leading-none" />
                  </div>
                  <span className="flex-1 text-[12.5px] text-foreground-900 truncate">{f}</span>
                  <button
                    type="button"
                    onClick={() => setFiles((prev) => prev.filter((x) => x !== f))}
                    className="w-7 h-7 rounded-full flex items-center justify-center text-danger-500 hover:bg-danger-50 cursor-pointer"
                  >
                    <i className="ri-close-line text-[17px] leading-none" />
                  </button>
                </div>
              ))}
            </div>
          )}
        </div>

        <div className="flex items-start gap-2.5 rounded-[16px] bg-secondary-50 border border-secondary-200 p-3.5">
          <i className="ri-information-line text-[17px] text-secondary-900 leading-none mt-0.5" />
          <p className="text-[11.5px] text-secondary-900 leading-relaxed">
            سيتم مراجعة النزاع من فريق الإدارة. تأكد من إرفاق كل المستندات الداعمة لتسريع المعالجة.
          </p>
        </div>
      </div>

      <div className="sticky bottom-0 z-30 mt-auto p-4 bg-background-50/95 backdrop-blur border-t border-background-200/70">
        <button
          type="button"
          disabled={desc.trim().length < 10}
          onClick={() => setSubmitted(true)}
          className="w-full h-[52px] rounded-[14px] bg-danger-500 text-background-50 text-[15px] font-semibold cursor-pointer active:scale-[0.98] transition-transform disabled:opacity-40 disabled:pointer-events-none"
        >
          إرسال النزاع
        </button>
      </div>
    </div>
  );
}