import { FormField } from "@/components/shared/form-field";

export function FirmwareFields({ invalidField }: { invalidField?: string }) {
  const field = (name: string) => ({ name, id: `operation-${name}`, "aria-invalid": invalidField === name, "aria-describedby": invalidField === name ? "operation-error" : undefined });
  return <div className="space-y-4">
    <p className="rounded-lg border border-warning/25 bg-warning/5 p-3 text-sm">Use firmware approved for this charger model. The charger verifies the signature before installation; an update can interrupt charging or restart the device.</p>
    <FormField {...field("requestId")} label="Request ID" type="number" min={0} max={2_147_483_647} step={1} required placeholder="e.g. 1001" />
    <FormField {...field("location")} label="Firmware download URL" maxLength={512} required autoComplete="off" placeholder="https://server.example.com/firmware.bin" />
    <FormField {...field("retrieveDateTime")} label="Retrieve firmware at" type="datetime-local" step={1} required />
    <p className="text-xs text-muted-foreground">Times use your browser’s local timezone and are sent with an explicit timezone. The charger downloads the file directly from the supplied FTP, HTTP, or HTTPS URL.</p>
    <div><label className="field-label" htmlFor="operation-signingCertificate">Firmware signing certificate <span className="text-destructive">*</span></label><textarea {...field("signingCertificate")} rows={6} required maxLength={11_000} autoComplete="off" spellCheck={false} className="w-full rounded-lg border border-input bg-card px-3 py-2.5 font-mono text-xs outline-none focus-visible:ring-2 focus-visible:ring-ring" placeholder={"-----BEGIN CERTIFICATE-----\nSigning certificate content\n-----END CERTIFICATE-----"} /></div>
    <div><label className="field-label" htmlFor="operation-signature">Firmware signature (base64) <span className="text-destructive">*</span></label><textarea {...field("signature")} rows={3} required maxLength={1_600} autoComplete="off" spellCheck={false} className="w-full rounded-lg border border-input bg-card px-3 py-2.5 font-mono text-xs outline-none focus-visible:ring-2 focus-visible:ring-ring" placeholder="Paste the signature for this exact firmware file" /></div>
    <details className="rounded-lg border p-3"><summary className="cursor-pointer text-sm font-medium focus-visible:outline-2 focus-visible:outline-ring">Retry options</summary><div className="mt-4 grid gap-4 sm:grid-cols-2"><FormField {...field("retries")} label="Retries" type="number" min={0} max={10} step={1} defaultValue={3} /><FormField {...field("retryInterval")} label="Retry interval (seconds)" type="number" min={1} max={86_400} step={1} defaultValue={60} /></div></details>
  </div>;
}
