"use client";

import { ChevronDown, MessageCircle } from "lucide-react";
import { isLocationSlug, type Locale } from "@alterra/shared";
import { whatsappUrl } from "@/lib/i18n";
import { useLeadForm } from "@/lib/useLeadForm";
import { Consent, Honeypot, StatusLine, type FormMessages } from "../forms/FormBits";

export type InvestorFormCopy = {
  title: string;
  text: string;
  name: string;
  email: string;
  phone: string;
  budget: string;
  budgetOptions: string[];
  horizon: string;
  horizonOptions: string[];
  assetType: string;
  assetOptions: string[];
  zone: string;
  zoneAny: string;
  risk: string;
  riskOptions: string[];
  submit: string;
  whatsapp: string;
  disclaimer: string;
};

const field =
  "w-full border-0 border-b border-cream/25 bg-transparent py-3 text-cream outline-none transition placeholder:text-cream/40 focus:border-gold";

type Option = { value: string; label: string };

function Select({ name, label, options, placeholder }: { name: string; label: string; options: Option[]; placeholder?: string }) {
  return (
    <label className="relative block">
      <span className="eyebrow text-[0.6rem] text-cream/70">{label}</span>
      <select name={name} defaultValue="" className={`${field} cursor-pointer appearance-none pr-8`}>
        <option value="" className="text-ink">
          {placeholder ?? "—"}
        </option>
        {options.map((o) => (
          <option key={o.value} value={o.value} className="text-ink">
            {o.label}
          </option>
        ))}
      </select>
      <ChevronDown size={16} strokeWidth={1.4} className="pointer-events-none absolute bottom-3.5 right-0 text-gold-light" />
    </label>
  );
}

const asOptions = (labels: string[]) => labels.map((label) => ({ value: label, label }));

/** Investor profile to receive private opportunities. */
export default function InvestorProfileForm({
  copy,
  zones,
  lang,
  messages,
}: {
  copy: InvestorFormCopy;
  zones: Option[];
  lang: Locale;
  messages: FormMessages;
}) {
  const { status, submit, sending } = useLeadForm("investor_profile", lang);

  const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault();
    const form = e.currentTarget;
    const data = new FormData(form);
    const zone = String(data.get("zone") ?? "");
    const details = Object.fromEntries(
      ["budget", "horizon", "assetType", "risk"].map((key) => [key, String(data.get(key) ?? "")]).filter(([, v]) => v),
    );
    const ok = await submit(data, { zone: isLocationSlug(zone) ? zone : undefined, services: ["investment-opportunities"], details });
    if (ok) form.reset();
  };

  return (
    <section id="investor-profile" className="bg-ink text-cream">
      <div className="mx-auto grid max-w-7xl gap-14 px-5 py-24 md:px-8 md:py-32 lg:grid-cols-[1fr_1.3fr] lg:gap-20">
        <div>
          <h2 className="font-serif text-4xl leading-[1.08] md:text-5xl">{copy.title}</h2>
          <p className="mt-6 max-w-md text-cream/70">{copy.text}</p>
          <p className="mt-10 max-w-md border-l-2 border-gold pl-4 text-xs leading-relaxed text-cream/70">{copy.disclaimer}</p>
        </div>
        <form onSubmit={onSubmit} className="relative grid gap-x-8 gap-y-7 sm:grid-cols-2">
          <Honeypot />
          <label className="block">
            <span className="eyebrow text-[0.6rem] text-cream/70">{copy.name}</span>
            <input name="name" required autoComplete="name" className={field} />
          </label>
          <label className="block">
            <span className="eyebrow text-[0.6rem] text-cream/70">{copy.email}</span>
            <input name="email" type="email" required autoComplete="email" className={field} />
          </label>
          <label className="block sm:col-span-2">
            <span className="eyebrow text-[0.6rem] text-cream/70">{copy.phone}</span>
            <input name="phone" type="tel" autoComplete="tel" className={field} />
          </label>
          <Select name="budget" label={copy.budget} options={asOptions(copy.budgetOptions)} />
          <Select name="horizon" label={copy.horizon} options={asOptions(copy.horizonOptions)} />
          <Select name="assetType" label={copy.assetType} options={asOptions(copy.assetOptions)} />
          <Select name="zone" label={copy.zone} options={zones} placeholder={copy.zoneAny} />
          <div className="sm:col-span-2">
            <span className="eyebrow text-[0.6rem] text-cream/70">{copy.risk}</span>
            <div className="mt-3 flex flex-wrap gap-3">
              {copy.riskOptions.map((option, i) => (
                <label key={option} className="cursor-pointer">
                  <input type="radio" name="risk" value={option} defaultChecked={i === 1} className="peer sr-only" />
                  <span className="block border border-cream/30 px-4 py-2 text-sm transition peer-checked:border-gold peer-checked:bg-gold-light peer-checked:text-ink peer-focus-visible:ring-2 peer-focus-visible:ring-gold-light">
                    {option}
                  </span>
                </label>
              ))}
            </div>
          </div>
          <div className="sm:col-span-2">
            <Consent messages={messages} dark />
          </div>
          <div className="flex flex-col gap-4 sm:col-span-2 sm:flex-row sm:items-center">
            <button
              type="submit"
              disabled={sending}
              className="bg-cream px-8 py-4 text-[0.78rem] tracking-[0.16em] uppercase text-ink transition hover:bg-gold-light disabled:opacity-60"
            >
              {copy.submit}
            </button>
            <a
              href={whatsappUrl()}
              target="_blank"
              rel="noopener"
              className="flex items-center justify-center gap-2 border border-cream/35 px-6 py-4 text-[0.72rem] tracking-[0.16em] uppercase transition hover:border-gold-light hover:text-gold-light"
            >
              <MessageCircle size={15} strokeWidth={1.5} /> {copy.whatsapp}
            </a>
          </div>
          <div className="sm:col-span-2">
            <StatusLine status={status} messages={messages} dark />
          </div>
        </form>
      </div>
    </section>
  );
}
