import Link from 'next/link';
import { resetPassword } from '../actions';

const ERROR_MESSAGES: Record<string, string> = {
  invalid: 'This reset link is invalid.',
  expired: 'This reset link has expired — request a new one.',
  mismatch: "Passwords don't match.",
  weak: 'Password must be at least 8 characters.',
};

export default function ResetPasswordPage({ searchParams }: { searchParams: { token?: string; error?: string } }) {
  const token = searchParams.token ?? '';
  const error = searchParams.error ? (ERROR_MESSAGES[searchParams.error] ?? 'Something went wrong.') : null;

  if (!token) {
    return (
      <div className="mx-auto max-w-md px-6 py-16">
        <h1 className="font-display text-3xl">Reset your password</h1>
        <p className="mt-6 border border-splash-pink/40 bg-splash-pink/10 px-4 py-3 text-sm text-splash-pink">
          This reset link is invalid or incomplete.
        </p>
        <p className="mt-6 text-sm">
          <Link href="/account/forgot-password" className="text-clay hover:text-clay-dark">
            Request a new link
          </Link>
        </p>
      </div>
    );
  }

  return (
    <div className="mx-auto max-w-md px-6 py-16">
      <h1 className="font-display text-3xl">Choose a new password</h1>

      {error && (
        <p className="mt-6 border border-splash-pink/40 bg-splash-pink/10 px-4 py-3 text-sm text-splash-pink">
          {error}
        </p>
      )}

      <form action={resetPassword} className="mt-8 space-y-6">
        <input type="hidden" name="token" value={token} />
        <div>
          <label htmlFor="password" className="tag-label mb-2 block">
            New password
          </label>
          <input
            id="password"
            name="password"
            type="password"
            required
            minLength={8}
            className="w-full border border-line px-3 py-2 text-sm"
          />
        </div>
        <div>
          <label htmlFor="confirmPassword" className="tag-label mb-2 block">
            Confirm new password
          </label>
          <input
            id="confirmPassword"
            name="confirmPassword"
            type="password"
            required
            minLength={8}
            className="w-full border border-line px-3 py-2 text-sm"
          />
        </div>
        <button type="submit" className="bg-clay px-6 py-3 text-sm font-medium text-paper hover:bg-clay-dark">
          Set new password
        </button>
      </form>
    </div>
  );
}
