"use client";

import { GoogleReCaptchaProvider } from "react-google-recaptcha-v3";
import SmartContactForm from "./SmartContactForm";
import type { Lang } from "@/lib/config";

type Topic =
  | "sales"
  | "support"
  | "billing"
  | "partnerships"
  | "press"
  | "careers"
  | "security";
type LanguageChoice = "ar" | "en";

type FormCopy = {
  nameLabel: string;
  emailLabel: string;
  phoneLabel: string;
  phoneHint: string;
  companyLabel: string;
  topicLabel: string;
  topicPlaceholder: string;
  messageLabel: string;
  messagePlaceholder: string;
  attachmentsLabel: string;
  attachmentsHint: string;
  consentLabel: string;
  languageLabel: string;
  submitCta: string;
  submittingCta: string;
  successTitle: string;
  successTemplate: string;
  successRouteTemplate: string;
  afterHoursNote: string;
  validation: {
    required: string;
    email: string;
    consent: string;
    messageLength: string;
    fileType: string;
    fileSize: string;
  };
  routingLabel: string;
  routingNotePrefix: string;
  languageOptions: Array<{ value: LanguageChoice; label: string }>;
  topicOptions: Array<{ value: Topic; label: string }>;
};

type RoutingInfo = {
  [key in Topic]: {
    destination: string;
    note: string;
  };
};

interface SmartContactFormWithProviderProps {
  lang: Lang;
  copy: FormCopy;
  routingInfo: RoutingInfo;
}

export default function SmartContactFormWithProvider({
  lang,
  copy,
  routingInfo,
}: SmartContactFormWithProviderProps) {
  const siteKey = process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY;

  // If no reCAPTCHA key, render form without provider (graceful degradation)
  if (!siteKey) {
    console.warn(
      "reCAPTCHA site key not configured. Bot protection is disabled.",
    );
    return (
      <SmartContactForm lang={lang} copy={copy} routingInfo={routingInfo} />
    );
  }

  return (
    <GoogleReCaptchaProvider
      reCaptchaKey={siteKey}
      language={lang}
      scriptProps={{
        async: true,
        defer: true,
        appendTo: "head",
      }}
    >
      <SmartContactForm lang={lang} copy={copy} routingInfo={routingInfo} />
    </GoogleReCaptchaProvider>
  );
}
