"use client"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, useZodForm, } from "@/components/ui/form"; import { RetentionSettings, RetentionSettingsSchema, } from "@/features/database/retention-policy.schema"; import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import { Badge } from "@/components/ui/badge"; import { Separator } from "@/components/ui/separator"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Calendar, Save } from "lucide-react"; export type BackupRetentionSettingsFormProps = { defaultValues?: RetentionSettings; currentType?: string; isPending?: boolean; onSave: (values: RetentionSettings) => Promise; }; export const BackupRetentionSettingsForm = ({ defaultValues, currentType, isPending = false, onSave, }: BackupRetentionSettingsFormProps) => { const form = useZodForm({ schema: RetentionSettingsSchema, defaultValues: defaultValues ?? { count: 7, days: 30, gfs: { daily: 7, weekly: 4, monthly: 12, yearly: 3 }, }, }); const calculateTotalFiles = (values: RetentionSettings) => { if (values.type === "gfs" && values.gfs) { return ( (values.gfs.daily ?? 0) + (values.gfs.weekly ?? 0) + (values.gfs.monthly ?? 0) + (values.gfs.yearly ?? 0) ); } return values.type === "count" ? (values.count ?? 0) : values.type === "days" ? (values.days ?? 0) : 0; }; const getStorageEstimate = (totalFiles: number) => { if (totalFiles <= 10) return "Low"; if (totalFiles <= 30) return "Medium"; return "High"; }; return (
{ await onSave(values); }} > ( Retention Policy Type {[ { id: "count", label: "Keep last N backups", desc: "Simple count-based retention (e.g., keep last 10 backups)", }, { id: "days", label: "Keep backups for X days", desc: "Time-based retention (e.g., keep backups for 30 days)", }, { id: "gfs", label: "GFS Rotation", desc: "Grandfather-Father-Son rotation for enterprise/critical systems", badge: "Recommended", }, ].map((opt) => (
{opt.label} {opt.badge && ( {opt.badge} )}

{opt.desc}

{currentType === opt.id && ( Actual )}
))}
)} /> {form.watch("type") && } {form.watch("type") === "count" && ( ( Number of backups to keep field.onChange(e.target.valueAsNumber)} /> Older backups beyond this count will be automatically deleted. )} /> )} {form.watch("type") === "days" && ( ( Retention period (days) field.onChange(e.target.valueAsNumber)} /> Backups older than {field.value} days will be automatically deleted. )} /> )} {form.watch("type") === "gfs" && (
{[ { key: "daily" as const, label: "Daily", max: 31 }, { key: "weekly" as const, label: "Weekly", max: 52 }, { key: "monthly" as const, label: "Monthly", max: 120 }, { key: "yearly" as const, label: "Yearly", max: 50 }, ].map(({ key, label, max }) => ( ( {label} backups field.onChange(e.target.valueAsNumber) } /> Keep N {key} backups )} /> ))}
)} {form.watch("type") && ( <>
Storage Impact Summary
{(() => { const totalFiles = calculateTotalFiles(form.getValues()); const estimate = getStorageEstimate(totalFiles); return ( {estimate} Usage ); })()}
Estimated files per database:

{calculateTotalFiles(form.getValues())} backup files

Policy type:

{form.watch("type") === "gfs" ? "GFS Rotation" : form.watch("type") === "count" ? "Count-based" : "Time-based"}

)}
); };