"use client";

import type { KanbanGroupBy } from "@/types/asset-settings";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select";

export interface GroupBySelectorProps {
  value: KanbanGroupBy;
  onChange: (value: KanbanGroupBy) => void;
}

const GROUP_BY_OPTIONS: { value: KanbanGroupBy; label: string }[] = [
  { value: "assetClass", label: "Asset class" },
  { value: "status", label: "Status" },
  { value: "priority", label: "Priority" },
];

/**
 * Kanban grouping control, on the shadcn `Select`.
 *
 * A native `<select>` cannot be styled consistently across browsers — the
 * option list was rendered by the OS, so it ignored the app's theme entirely
 * (and would have stayed light in dark mode). Radix renders the list itself.
 *
 * The "Group by" prefix moved out of every option label and into the trigger's
 * own label, so the selected value reads as "Asset class" rather than
 * repeating "Group by" in the closed control.
 */
export function GroupBySelector({ value, onChange }: GroupBySelectorProps) {
  return (
    <Select value={value} onValueChange={(v) => onChange(v as KanbanGroupBy)}>
      <SelectTrigger size="sm" className="w-[11rem]" aria-label="Group cards by">
        <span className="text-muted-foreground">Group by</span>
        <SelectValue />
      </SelectTrigger>
      <SelectContent>
        {GROUP_BY_OPTIONS.map(({ value: v, label }) => (
          <SelectItem key={v} value={v}>
            {label}
          </SelectItem>
        ))}
      </SelectContent>
    </Select>
  );
}
