"use client";

import Image from "next/image";
import type { ReactNode } from "react";

import Link from "@/app/components/LocaleLink";
import OtpInputBox from "@/app/components/OtpInputBox";

interface ResendProps {
  timer: number;
  resending: boolean;
  onResend: () => void;
  // Already-translated copy, so each flow keeps its own wording keys.
  resendInLabel: string;
  resendLabel: string;
  resendingLabel: string;
}

interface Props {
  /** Where the logo links to (customer site vs. astrologer site). */
  logoHref?: string;
  heading: string;
  subtitle: ReactNode;
  /** Anything that must sit between the subtitle and the code boxes. */
  children?: ReactNode;
  otp: string;
  onOtpChange: (otp: string) => void;
  onVerify: () => void;
  loading: boolean;
  verifyLabel: string;
  verifyingLabel: string;
  resend?: ResendProps;
}

/*
|--------------------------------------------------------------------------
| OTP verification screen
|--------------------------------------------------------------------------
| The one layout every OTP step uses (login, register mobile/email, social
| mobile step, astrologer mobile/email): centred logo → heading → hint → code
| boxes → primary button → resend line, in one card that stays vertically
| centred and never overflows on short or narrow screens. Flow-specific logic
| (what "verify" does, the timer) stays in each page.
*/

export default function OtpVerifyScreen({
  logoHref = "/",
  heading,
  subtitle,
  children,
  otp,
  onOtpChange,
  onVerify,
  loading,
  verifyLabel,
  verifyingLabel,
  resend,
}: Props) {
  return (
    <section className="otp-screen">
      <div className="otp-screen-inner">
        <div className="otp-logo">
          <Link href={logoHref}>
            <Image src="/images/logo-astro.svg" alt="AstroEye" width={150} height={128} priority />
          </Link>
        </div>

        <div className="card card-bg-primary border-0 shadow rounded-4">
          <div className="card-body otp-card-body text-white text-center">
            <h2 className="otp-heading">{heading}</h2>

            <p className="otp-subtitle">{subtitle}</p>

            {children}

            <div className="otp-fields">
              <OtpInputBox value={otp} onChange={onOtpChange} />
            </div>

            <button
              type="button"
              className="btn btn-primary w-100 py-3"
              onClick={onVerify}
              disabled={loading}
            >
              {loading ? verifyingLabel : verifyLabel}
            </button>

            {resend && (
              <div className="otp-resend">
                {resend.timer > 0 ? (
                  <p className="mb-0">
                    {resend.resendInLabel}
                    <strong className="ms-2">{resend.timer}s</strong>
                  </p>
                ) : (
                  <button
                    type="button"
                    className="btn btn-link p-0 text-decoration-none"
                    onClick={resend.onResend}
                    disabled={resend.resending}
                  >
                    {resend.resending ? resend.resendingLabel : resend.resendLabel}
                  </button>
                )}
              </div>
            )}
          </div>
        </div>
      </div>
    </section>
  );
}
