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

export const metadata: Metadata = {
  title: "Share to AdultWorld",
  robots: { index: false, follow: false },
};

// R17 D.2: Web Share Target API receiver. The PWA manifest declares
// share_target → /share so users can share an external URL/text from
// any other app's share sheet straight into AdultWorld. Today this
// surfaces a "Send to chat" CTA that drops the shared content into a
// new-message draft via the messages page query string.
export default async function ShareReceiverPage({
  searchParams,
}: {
  searchParams: Promise<{ title?: string; text?: string; url?: string }>;
}) {
  const params = await searchParams;
  const title = params.title?.trim() || "";
  const text = params.text?.trim() || "";
  const url = params.url?.trim() || "";

  // Build a single message body from whichever fields the source app sent.
  const composed = [title, text, url].filter(Boolean).join(" — ").slice(0, 2000);

  return (
    <div className="max-w-2xl mx-auto p-6 space-y-6">
      <header>
        <h1 className="text-2xl font-bold text-text">Share to AdultWorld</h1>
        <p className="text-sm text-text-muted mt-1">
          Pick what you want to do with this content.
        </p>
      </header>

      <section className="bg-surface border border-surface-light rounded-lg p-4 space-y-2">
        {title && (
          <p className="text-sm font-medium text-text">{title}</p>
        )}
        {text && (
          <p className="text-sm text-text-muted whitespace-pre-wrap">{text}</p>
        )}
        {url && (
          <a
            href={url}
            target="_blank"
            rel="noopener noreferrer"
            className="text-sm text-primary hover:text-primary-dark break-all underline"
          >
            {url}
          </a>
        )}
      </section>

      <div className="flex flex-col gap-3">
        <Link
          href={`/messages?compose=${encodeURIComponent(composed)}`}
          className="bg-primary hover:bg-primary-dark text-white px-5 py-3 rounded-lg font-semibold text-center transition-colors"
        >
          Send in a chat
        </Link>
        <Link
          href={`/manage/messaging/replies?prefill=${encodeURIComponent(composed)}`}
          className="bg-surface-light hover:bg-surface text-text px-5 py-3 rounded-lg text-center transition-colors"
        >
          Save as a reply
        </Link>
        <Link
          href="/"
          className="text-text-muted hover:text-text text-center text-sm"
        >
          Cancel
        </Link>
      </div>
    </div>
  );
}
