"use client";

import { useCallback, useRef, useState } from "react";
import { Building2, Compass, LineChart, Users } from "lucide-react";
import RevealText from "@/components/immersive/RevealText";
import Reveal from "@/components/immersive/Reveal";

const AUDIENCES = [
  {
    number: "01",
    icon: Users,
    title: "Wealth Managers",
    body: "Bring client portfolios together across custodians, accounts and asset classes, giving advisors a complete view of each client and the tools to manage, report and serve them better.",
    change: "One client view across every custodian they already use.",
    detail: "Advisory books · Discretionary mandates · Held-away assets",
  },
  {
    number: "02",
    icon: LineChart,
    title: "Asset Managers",
    body: "Manage portfolios and investment mandates with centralised data, streamlined trade workflows, performance analytics, compliance monitoring and reporting, all from one platform.",
    change: "Mandates, models and vehicles on one validated book.",
    detail: "Fund vehicles · Mandates · Model portfolios",
  },
  {
    number: "03",
    icon: Building2,
    title: "Multi-Family Offices",
    body: "Consolidate complex, multi-asset and multi-entity portfolios into one trusted view for clearer oversight, reporting and decision-making.",
    change: "Trusts, holdings and cross-border structures in one picture.",
    detail: "Trusts · Holding companies · Cross-border structures",
  },
  {
    number: "04",
    icon: Compass,
    title: "Independent Advisors",
    body: "Turn fragmented client and investment data into actionable insights, with the tools to manage portfolios, reporting and compliance from one platform.",
    change: "Boutique operations without a back-office stack of tools.",
    detail: "Boutique practices · Consultants · Outsourced CIOs",
  },
];

/**
 * Who the platform is for.
 *
 * A persona switcher rather than four equal cards: the visitor picks their
 * firm type and reads what changes for them, without waiting on a carousel.
 */
export default function Audience() {
  const [active, setActive] = useState(0);
  const tabRefs = useRef<(HTMLButtonElement | null)[]>([]);
  const item = AUDIENCES[active];
  const Icon = item.icon;

  const pick = useCallback((index: number) => setActive(index), []);

  const onRailKey = useCallback(
    (e: React.KeyboardEvent, index: number) => {
      const last = AUDIENCES.length - 1;
      let next: number | null = null;
      if (e.key === "ArrowDown" || e.key === "ArrowRight") next = index === last ? 0 : index + 1;
      else if (e.key === "ArrowUp" || e.key === "ArrowLeft") next = index === 0 ? last : index - 1;
      else if (e.key === "Home") next = 0;
      else if (e.key === "End") next = last;
      if (next === null) return;
      e.preventDefault();
      pick(next);
      tabRefs.current[next]?.focus();
    },
    [pick]
  );

  return (
    <section
      id="resource"
      className="immersive-section scroll-mt-28 px-6 py-20 lg:px-12 lg:py-28"
    >
      <div className="mx-auto w-full max-w-[88rem]">
        <div className="immersive-section-head">
          <span className="immersive-label">Who we serve</span>
          <span className="immersive-label">[ 04 ]</span>
        </div>

        <div className="grid gap-8 lg:grid-cols-12 lg:gap-16">
          <div className="lg:col-span-6">
            <RevealText as="h2" className="immersive-heading">
              Built for the firms that manage wealth.
            </RevealText>
          </div>
          <Reveal delay={0.1} className="lg:col-span-5 lg:col-start-8">
            <p className="text-base leading-relaxed text-[hsl(var(--muted-foreground))]">
              The same operating layer, configured to how your firm is structured and
              what it is accountable for.
            </p>
          </Reveal>
        </div>

        <Reveal delay={0.05} className="mt-14 lg:mt-16">
          <div className="grid gap-10 border-t border-[color:var(--hairline)] pt-10 lg:grid-cols-12 lg:gap-16 lg:pt-12">
            <div
              role="tablist"
              aria-orientation="vertical"
              aria-label="Firm types"
              className="relative flex flex-col lg:col-span-4"
            >
              <span
                aria-hidden
                className="absolute inset-y-0 left-0 hidden w-px bg-[var(--hairline)] lg:block"
              />
              {AUDIENCES.map((audience, index) => {
                const isActive = index === active;
                return (
                  <button
                    key={audience.number}
                    ref={(node) => {
                      tabRefs.current[index] = node;
                    }}
                    type="button"
                    role="tab"
                    id={`audience-tab-${audience.number}`}
                    aria-selected={isActive}
                    aria-controls="audience-panel"
                    tabIndex={isActive ? 0 : -1}
                    onClick={() => pick(index)}
                    onKeyDown={(e) => onRailKey(e, index)}
                    className={`group relative flex items-baseline justify-between gap-4 border-b border-[color:var(--hairline)] py-4 text-left transition-colors lg:pl-6 ${
                      isActive
                        ? "text-[hsl(var(--foreground))]"
                        : "text-[hsl(var(--foreground))]/50 hover:text-[hsl(var(--foreground))]/80"
                    }`}
                  >
                    <span
                      aria-hidden
                      className={`absolute inset-y-0 left-0 hidden w-px bg-[hsl(var(--primary))] transition-opacity duration-300 lg:block ${
                        isActive ? "opacity-100" : "opacity-0"
                      }`}
                    />
                    <span className="font-heading text-lg font-medium tracking-tight lg:text-xl">
                      {audience.title}
                    </span>
                    <span
                      className={`immersive-label tabular-nums ${
                        isActive ? "immersive-label-accent" : ""
                      }`}
                    >
                      {audience.number}
                    </span>
                  </button>
                );
              })}
            </div>

            <div
              id="audience-panel"
              role="tabpanel"
              aria-labelledby={`audience-tab-${item.number}`}
              className="flex flex-col justify-center lg:col-span-8 lg:pl-4"
            >
              <span className="immersive-chip">
                <Icon className="size-5" aria-hidden />
              </span>
              <h3 className="font-heading mt-6 text-2xl font-medium tracking-tight lg:text-[2rem]">
                {item.title}
              </h3>
              <p className="mt-4 max-w-xl text-base leading-relaxed text-[hsl(var(--muted-foreground))]">
                {item.body}
              </p>
              <p className="mt-8 border-t border-[color:var(--hairline)] pt-5 text-sm font-medium text-[hsl(var(--foreground))]">
                {item.change}
              </p>
              <p className="immersive-label mt-4 !tracking-[0.12em]">{item.detail}</p>
            </div>
          </div>
        </Reveal>
      </div>
    </section>
  );
}
