import type { Metadata } from "next";
import Link from "next/link";
import { AuthShell } from "../_components/auth-shell";
import { ResetPasswordForm } from "./reset-password-form";
import { FormAlert } from "../_components/form-alert";
import { authTexts } from "@/lib/i18n/auth";
import { commonTexts } from "@/lib/i18n/common";

export const metadata: Metadata = {
  title: `${authTexts.resetTitle} · ${commonTexts.appName}`,
  description: authTexts.resetSubtitle,
};

type SearchParams = Record<string, string | string[] | undefined>;

export default async function ResetPasswordPage(props: {
  searchParams: Promise<SearchParams>;
}) {
  const searchParams = await props.searchParams;
  const raw = searchParams.token;
  const token = (Array.isArray(raw) ? raw[0] : raw)?.trim() ?? "";

  if (!token) {
    return (
      <AuthShell
        title={authTexts.resetInvalidTitle}
        description={authTexts.resetInvalidSubtitle}
        footer={
          <Link
            href="/signin"
            className="font-medium text-primary transition-colors hover:text-primary/80"
          >
            {authTexts.backToSignIn}
          </Link>
        }
      >
        <div className="space-y-5">
          <FormAlert tone="error">{authTexts.errors.resetInvalid}</FormAlert>
          <Link
            href="/forgot-password"
            className="inline-flex h-11 w-full items-center justify-center rounded-lg bg-primary px-4 text-sm font-semibold text-primary-foreground transition-colors hover:bg-primary/90"
          >
            {authTexts.requestNewLink}
          </Link>
        </div>
      </AuthShell>
    );
  }

  return (
    <AuthShell
      title={authTexts.resetTitle}
      description={authTexts.resetSubtitle}
      footer={
        <>
          {authTexts.haveAccount}{" "}
          <Link
            href="/signin"
            className="font-medium text-primary transition-colors hover:text-primary/80"
          >
            {authTexts.signInLink}
          </Link>
        </>
      }
    >
      <ResetPasswordForm token={token} />
    </AuthShell>
  );
}
