"use client";

import type { ComponentType } from "react";
import { cn } from "cn";
import type { Dictionary } from "@/i18n/get-dictionary";
import {
  CartOutlineIcon,
  CodeIcon,
  GlobeOutlineIcon,
  HeadphonesOutlineIcon,
  PhonePortraitIcon,
  RobotIcon,
  type IconProps,
} from "@/components/icons";
import { FieldLegend, FieldSet } from "@/components/ui/field";
import { RadioCard, RadioGroup } from "@/components/ui/radio-group";

type ServiceLabels = Dictionary["home"]["contact"]["form"]["services"];
type ServiceKey = keyof ServiceLabels;

const options: { key: ServiceKey; icon: ComponentType<IconProps> }[] = [
  { key: "web", icon: GlobeOutlineIcon },
  { key: "mobile", icon: PhonePortraitIcon },
  { key: "ecommerce", icon: CartOutlineIcon },
  { key: "ai", icon: RobotIcon },
  { key: "support", icon: HeadphonesOutlineIcon },
  { key: "other", icon: CodeIcon },
];

type ServicePickerProps = {
  name: string;
  legend: string;
  labels: ServiceLabels;
  /** Grid columns / gaps of the tiles. Default: 3 per row (Home contact form). */
  listClassName?: string;
  /** Extra classes for each tile (e.g. label color). */
  tileClassName?: string;
};

/** "Select Service" tiles — a single-choice radio group (arrow keys move the selection). */
export function ServicePicker({ name, legend, labels, listClassName = "grid-cols-3 gap-5", tileClassName }: ServicePickerProps) {
  return (
    // data-figma-small-text: 10px tile labels on phones come from the Figma phone frame.
    <FieldSet className="gap-1" data-figma-small-text>
      <FieldLegend variant="label" className="mb-1 text-xs leading-[1.4] font-normal text-soft md:text-sm">
        {legend}
      </FieldLegend>
      <RadioGroup name={name} className={listClassName}>
        {options.map(({ key, icon: Icon }) => (
          <RadioCard key={key} value={key} className={cn("max-md:px-1", tileClassName)}>
            <Icon />
            <span className="font-heading text-[10px] leading-6 md:text-sm">{labels[key]}</span>
          </RadioCard>
        ))}
      </RadioGroup>
    </FieldSet>
  );
}
