import Link from "next/link";
import type { Metadata } from "next";
import ConfirmationCartClear from "./ConfirmationCartClear";

export const metadata: Metadata = { title: "Order — Awafi Portal" };

export default async function OrderConfirmationPage({
  searchParams,
}: {
  searchParams: Promise<{ order?: string; status?: string }>;
}) {
  const sp = await searchParams;
  const order = sp.order || "";
  const paid = (sp.status || "").toLowerCase() === "paid";

  return (
    <>
      <ConfirmationCartClear paid={paid} />

      <section className="pb-cathead pb-tone-cream">
        <div className="pb-shell">
          <nav className="pb-crumbs" aria-label="Breadcrumb">
            <Link href="/portal">Home</Link><span>/</span><span className="pb-crumbs-current">Order</span>
          </nav>
        </div>
      </section>

      <div className="pb-shell">
        <div className="pb-empty" style={{ maxWidth: 620, margin: "40px auto 56px" }}>
          {paid ? (
            <>
              <svg viewBox="0 0 24 24" width="54" height="54" fill="none" stroke="#1a8f4c" strokeWidth="1.6" style={{ margin: "0 auto 18px" }}><circle cx="12" cy="12" r="10" /><path d="m8 12 3 3 5-6" strokeLinecap="round" strokeLinejoin="round" /></svg>
              <h1 className="pb-cathead-title" style={{ marginBottom: 8 }}>Thank you — payment received</h1>
              <p>Your order has been placed and paid successfully.</p>
              {order && <p style={{ fontWeight: 700, marginTop: 6 }}>Order number: <span style={{ color: "var(--brand)" }}>{order}</span></p>}
              <p className="pb-cathead-sub" style={{ marginTop: 10 }}>A confirmation email is on its way. You can track this order from your account.</p>
              <div style={{ marginTop: 22, display: "flex", gap: 12, justifyContent: "center", flexWrap: "wrap" }}>
                <Link href={order ? `/portal/account/orders/${encodeURIComponent(order)}` : "/portal/account/orders"} className="pb-btn pb-btn--primary">View order details</Link>
                <Link href="/portal" className="pb-btn pb-btn--ghost">Continue shopping</Link>
              </div>
            </>
          ) : (
            <>
              <svg viewBox="0 0 24 24" width="54" height="54" fill="none" stroke="#c23b3b" strokeWidth="1.6" style={{ margin: "0 auto 18px" }}><circle cx="12" cy="12" r="10" /><path d="M12 7v6M12 16.5v.5" strokeLinecap="round" /></svg>
              <h1 className="pb-cathead-title" style={{ marginBottom: 8 }}>Payment unsuccessful</h1>
              <p>We couldn&apos;t confirm your payment. You have not been charged, or the payment was declined.</p>
              <p className="pb-cathead-sub" style={{ marginTop: 10 }}>Your cart has been kept — you can try again.</p>
              <div style={{ marginTop: 22, display: "flex", gap: 12, justifyContent: "center", flexWrap: "wrap" }}>
                <Link href="/portal/checkout" className="pb-btn pb-btn--primary">Return to checkout</Link>
                <Link href="/portal" className="pb-btn pb-btn--ghost">Continue shopping</Link>
              </div>
            </>
          )}
        </div>
      </div>

      <div className="pb-section-spacer" />
    </>
  );
}
