import { type FeatureFlags } from "@/app/lib/store";
import { Badge } from "../ui/badge";
import { Switch } from "../ui/switch";
import { toast } from "sonner";

type FlagConfig = {
  key: keyof FeatureFlags;
  label: string;
  description: string;
  icon: React.ElementType;
};

type FeatureFlagsTabProps = {
  configs: FlagConfig[];
  flags: FeatureFlags;
  setFlag: (key: keyof FeatureFlags, value: boolean) => void;
};

export function FeatureFlagsTab({
  configs,
  flags,
  setFlag,
}: FeatureFlagsTabProps) {
  return (
    <section className="rounded-lg border border-border bg-card overflow-hidden">
      <div className="border-b border-border px-6 py-4">
        <div className="flex items-center gap-2">
          <h2 className="text-sm font-semibold">Feature flags</h2>
          <Badge variant="secondary" className="text-caption">
            optional
          </Badge>
        </div>
        <p className="text-xs text-muted-foreground mt-0.5">
          Toggle optional UI features on or off. Changes take effect immediately
          and are persisted locally.
        </p>
      </div>
      <div className="divide-y divide-border">
        {configs.map(({ key, label, description, icon: Icon }) => (
          <div
            key={key}
            className="flex items-center justify-between gap-4 px-6 py-3.5"
          >
            <div className="flex items-start gap-3">
              <div className="mt-0.5 flex h-7 w-7 shrink-0 items-center justify-center rounded-md bg-muted-card">
                <Icon className="h-3.5 w-3.5 text-muted-foreground" />
              </div>
              <div>
                <p className="text-sm font-medium">{label}</p>
                <p className="text-xs text-muted-foreground">{description}</p>
              </div>
            </div>
            <Switch
              checked={flags[key]}
              onCheckedChange={(v) => {
                setFlag(key, v);
                toast.success(`${label} ${v ? "enabled" : "disabled"}`);
              }}
              className="shrink-0"
            />
          </div>
        ))}
      </div>
    </section>
  );
}
