"use client";

import { useState } from "react";

import { Switch } from "@/components/ui/switch";

/**
 * Radix Switch's own hidden-input form integration wasn't reliably reaching
 * the Server Action's FormData in this app (toggling off + saving silently
 * kept the old value) — this owns the submitted value directly instead of
 * depending on that mechanism.
 */
export function SettingSwitch({ name, defaultChecked }: { name: string; defaultChecked: boolean }) {
  const [checked, setChecked] = useState(defaultChecked);

  return (
    <>
      <input type="hidden" name={name} value={checked ? "true" : "false"} />
      <Switch checked={checked} onCheckedChange={setChecked} />
    </>
  );
}
