'use client';

import { useState } from 'react';

import OtpDigitInput from './OtpDigitInput';

interface VerifyEmailStepProps {
  email: string;
  submitting: boolean;
  errorMessage?: string | null;
  onVerify: (code: string) => void;
  onEditDetails: () => void;
  onResend: () => void;
  resending: boolean;
}

export default function VerifyEmailStep({
  email,
  submitting,
  errorMessage,
  onVerify,
  onEditDetails,
  onResend,
  resending,
}: VerifyEmailStepProps) {
  const [code, setCode] = useState('');
  const invalid = !!errorMessage;

  const handleSubmit = (e: React.FormEvent) => {
    e.preventDefault();
    if (code.length === 6) onVerify(code);
  };

  return (
    <form onSubmit={handleSubmit} noValidate className="mx-auto max-w-[440px]">
      <button
        type="button"
        onClick={onEditDetails}
        className="mb-3 inline-flex items-center gap-1.5 border-0 bg-transparent p-0 text-[13px] font-semibold text-[var(--fra-muted)] hover:text-[var(--fra-coral)]"
      >
        <svg className="size-3.5" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5">
          <path strokeLinecap="round" strokeLinejoin="round" d="M19 12H5M11 18l-6-6 6-6" />
        </svg>
        <span>Edit details</span>
      </button>

      <h2 className="mb-1.5 text-[22px] font-extrabold text-[var(--fra-heading)]">Verify your work email</h2>
      <p className="mb-5 text-sm text-[var(--fra-muted)]">
        We sent a 6-digit code to <strong className="text-[var(--fra-heading)]">{email}</strong>
      </p>

      <OtpDigitInput value={code} onChange={setCode} disabled={submitting} invalid={invalid} />
      {invalid && <small className="mt-1.5 block text-xs text-[var(--fra-rejected)]">{errorMessage}</small>}

      <button
        type="submit"
        disabled={submitting || code.length !== 6}
        className="group mt-4 flex h-11 w-full items-center justify-center gap-2 rounded-[var(--fra-radius-sm)] bg-[var(--fra-coral)] text-[15px] font-semibold text-white shadow-[0_4px_14px_rgba(244,93,107,0.35)] transition-colors hover:bg-[var(--fra-coral-dark)] disabled:cursor-not-allowed disabled:opacity-60"
      >
        <span>{submitting ? 'Verifying…' : 'Verify & create account'}</span>
        {!submitting && (
          <svg
            className="size-4 transition-transform group-hover:translate-x-0.5"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            strokeWidth="2.5"
          >
            <path strokeLinecap="round" strokeLinejoin="round" d="M5 12h14M13 6l6 6-6 6" />
          </svg>
        )}
      </button>

      <p className="mt-3.5 mb-0 text-center text-[13px] text-[var(--fra-muted)]">
        Didn&apos;t get a code?{' '}
        <button
          type="button"
          onClick={onResend}
          disabled={resending}
          className="border-0 bg-transparent p-0 font-semibold text-[var(--fra-coral)] disabled:text-[var(--fra-muted)]"
        >
          {resending ? 'Sending…' : 'Resend code'}
        </button>
      </p>
    </form>
  );
}
