import { useApp } from "@/pages/app/AppContext";
import { BackHeader, Btn } from "@/pages/app/components/ui";
import { cn } from "@/pages/app/components/ui";

type Flow = "success" | "failed" | "pending";

const META: Record<Flow, { icon: string; title: string; desc: string; tone: "mint" | "danger" | "warn" }> = {
  success: {
    icon: "ri-checkbox-circle-fill",
    title: "تم الدفع بنجاح",
    desc: "تم استلام قيمة الشراكة، وأصبحت الشراكة حالة «حالية» بين الطرفين.",
    tone: "mint",
  },
  failed: {
    icon: "ri-close-circle-fill",
    title: "فشل الدفع",
    desc: "لم تتم عملية الدفع. يمكنك إعادة المحاولة أو تغيير طريقة الدفع.",
    tone: "danger",
  },
  pending: {
    icon: "ri-time-line",
    title: "بانتظار مراجعة الإدارة",
    desc: "تم رفع إثبات التحويل، وحالة الشراكة الآن «بانتظار مراجعة الإدارة» حتى اعتماد الدفعة.",
    tone: "warn",
  },
};

/** Payment result: نجاح / فشل / بانتظار مراجعة الإدارة (section 2.21). */
export default function BizPartnerPayResult() {
  const { goBack, navigate, params } = useApp();
  const flow = (params.flow as Flow) ?? "success";
  const meta = META[flow] ?? META.success;
  const partnerId = params.partnerId ? String(params.partnerId) : undefined;

  const tones = {
    mint: "bg-accent-100 text-accent-600",
    danger: "bg-danger-100 text-danger-500",
    warn: "bg-secondary-100 text-secondary-900",
  } as const;

  return (
    <div className="min-h-full flex flex-col bg-background-50">
      <BackHeader title="نتيجة الدفع" onBack={goBack} />

      <div className="flex-1 flex flex-col items-center justify-center text-center px-6 py-12">
        <div className={cn("w-20 h-20 rounded-full flex items-center justify-center mb-5", tones[meta.tone])}>
          <i className={cn(meta.icon, "text-[44px] leading-none")} />
        </div>
        <h2 className="text-[20px] font-bold text-foreground-950">{meta.title}</h2>
        <p className="text-[13px] text-foreground-600 mt-2 max-w-[280px] leading-relaxed">{meta.desc}</p>
      </div>

      <div className="sticky bottom-0 z-30 px-4 py-4 bg-background-50 border-t border-background-200/70 space-y-2.5">
        {flow === "failed" && (
          <Btn full onClick={() => navigate("bizPartnerPay", { partnerId })}>
            إعادة المحاولة
          </Btn>
        )}
        {partnerId && flow !== "failed" && (
          <Btn full icon="ri-file-list-3-line" onClick={() => navigate("bizPartnership", { partnerId, flash: "1" })}>
            عرض حالة الشراكة
          </Btn>
        )}
        <Btn
          full
          variant={flow === "failed" ? "primary" : "secondary"}
          onClick={() => navigate("bizPartnerships")}
        >
          العودة إلى الشراكات
        </Btn>
      </div>
    </div>
  );
}