import type { Metadata } from "next";
import Link from "next/link";
import { redirect } from "next/navigation";
import { Suspense } from "react";
import { LoginForm } from "@/components/hub/AuthForms";
import { HubShell } from "@/components/hub/HubShell";
import { currentUser } from "@/lib/auth";
import { pageMetadata } from "@/lib/seo";

/* Depends on the visitor's session: never prerendered or cached. */
export const dynamic = "force-dynamic";

/* Hub accounts aren't created on this site: publishers sign up through AdMedia. */
const SIGNUP_URL = "https://admedia.com/publishers/signup/full.php";

export const metadata: Metadata = pageMetadata({
  title: "Publisher Hub Login | Monetize.com",
  description: "Sign in to your Monetize Hub account to see your applications, their status and your support messages.",
  path: "/hub-login",
  noindex: true,
});

export default async function HubLoginPage() {
  if (await currentUser()) redirect("/hub");
  return (
    <HubShell
      eyebrow="Monetize Hub"
      title="Your applications and"
      accent="support in one place"
      lede="Log in to follow your applications, check their status and reach publisher support."
      note={<>Monetize Commerce publishers: sign in here with your AdMedia account name and password to see affiliate revenue, merchants and install code. Other products keep their own login, reporting and payouts: <Link className="mz-link" href="/login">find your product&rsquo;s login</Link>.</>}
    >
      <h2>Sign in</h2>
      <p className="login__sub">
        New to Monetize? <a className="mz-link" href={SIGNUP_URL} target="_blank" rel="noopener">Apply for a publisher account</a>, and our team will review your application and contact you to set up your account upon approval.
      </p>
      <Suspense>
        <LoginForm />
      </Suspense>
      <p className="login__divider">or</p>
      <a className="mz-btn mz-btn--secondary mz-btn--lg login__alt" href={SIGNUP_URL} target="_blank" rel="noopener">Apply as a publisher</a>
      <p className="login__help">
        Can&rsquo;t sign in? <Link className="mz-link" href="/contact?topic=support&product=Monetize.com%20Publisher%20Hub&cta=hub-login#message">Contact publisher support</Link>.
      </p>
    </HubShell>
  );
}
