import { Download, Trash2, Plus, RefreshCw, RotateCcw, Upload, Clock, Check, HardDrive, Loader2 } from 'lucide-react'
import { useTranslation } from '../../i18n'
import CustomSelect from '../shared/CustomSelect'
import ConfirmDialog from '../shared/ConfirmDialog'
import EmptyState from '../shared/EmptyState'
import { Tooltip } from '../shared/Tooltip'
import { fs } from '../shared/DialogShell'
import { Segmented } from '../shared/dialogParts'
import ToggleSwitch from '../Settings/ToggleSwitch'
import {
ChoiceChips,
SETTINGS_BUTTON,
SETTINGS_BUTTON_PRIMARY,
SETTINGS_ICON_BUTTON,
SettingRow,
SettingRows,
SettingsCard,
StatusPill,
} from '../Settings/settingsKit'
import { DAYS_OF_MONTH, DAYS_OF_WEEK, HOURS, INTERVAL_OPTIONS, KEEP_OPTIONS, useBackupAdmin } from './useBackupAdmin'
/** The compact buttons of a backup row: white on a hairline, a size under the card's own. */
const ROW_BUTTON = 'inline-flex items-center gap-1.5 rounded-[10px] bg-surface-card px-2.5 py-1.5 font-medium text-content shadow-sm ring-1 ring-edge-faint hover:bg-surface-secondary disabled:cursor-default disabled:opacity-50'
export default function BackupPanel() {
const {
backups, isLoading, isCreating, restoringFile, isUploading, autoSettings, autoSettingsSaving, autoSettingsDirty,
serverTimezone, restoreConfirm, setRestoreConfirm, deleteTarget, setDeleteTarget, fileInputRef, is12h,
loadBackups, handleCreate, handleDownload, handleRestore, handleUploadRestore, executeRestore, handleDelete,
executeDelete, handleAutoSettingsChange, handleSaveAutoSettings, formatSize, formatDate, isAuto,
} = useBackupAdmin()
const { t } = useTranslation()
const headerActions = (
<>
{/* Upload & Restore */}
>
)
let backupList
if (isLoading && backups.length === 0) {
backupList = (
{t('common.loading')}
)
} else if (backups.length === 0) {
backupList = (
{t('backup.createFirst')}
}
/>
)
} else {
backupList = (
{backups.map(backup => {
const auto = isAuto(backup.filename)
const restoring = restoringFile === backup.filename
return (
{auto
?
: }
{backup.filename}
{auto &&
Auto}
{formatDate(backup.created_at)}
ยท
{formatSize(backup.size)}
)
})}
)
}
const hourOptions = HOURS.map(h => {
let label: string
if (is12h) {
const period = h >= 12 ? 'PM' : 'AM'
const h12 = h === 0 ? 12 : h > 12 ? h - 12 : h
label = `${h12}:00 ${period}`
} else {
label = `${String(h).padStart(2, '0')}:00`
}
return { value: String(h), label }
})
return (
{/* Manual Backups */}
{backupList}
{/* Auto-Backup Settings */}
{/* The whole row is the switch's label, so a click on the text flips it too. */}
{autoSettings.enabled && (
<>
label={t('backup.auto.interval')}
value={autoSettings.interval}
onChange={v => handleAutoSettingsChange('interval', v)}
options={INTERVAL_OPTIONS.map(opt => ({ value: opt.value, label: t(opt.labelKey) }))}
/>
}
/>
{/* Hour picker (for daily, weekly, monthly) */}
{autoSettings.interval !== 'hourly' && (
handleAutoSettingsChange('hour', Number.parseInt(String(v), 10))}
size="sm"
options={hourOptions}
/>
}
/>
)}
{/* Day of week (for weekly) */}
{autoSettings.interval === 'weekly' && (
label={t('backup.auto.dayOfWeek')}
value={String(autoSettings.day_of_week)}
onChange={v => handleAutoSettingsChange('day_of_week', Number(v))}
options={DAYS_OF_WEEK.map(opt => ({ value: String(opt.value), label: t(opt.labelKey) }))}
/>
}
/>
)}
{/* Day of month (for monthly) */}
{autoSettings.interval === 'monthly' && (
handleAutoSettingsChange('day_of_month', Number.parseInt(String(v), 10))}
size="sm"
options={DAYS_OF_MONTH.map(d => ({ value: String(d), label: String(d) }))}
/>
}
/>
)}
{/* Keep duration */}
label={t('backup.auto.keepLabel')}
value={String(autoSettings.keep_days)}
onChange={v => handleAutoSettingsChange('keep_days', Number(v))}
options={KEEP_OPTIONS.map(opt => ({ value: String(opt.value), label: t(opt.labelKey) }))}
/>
}
/>
>
)}
{/* Save button */}
{/* Restore warning */}
setRestoreConfirm(null)}
onConfirm={executeRestore}
title={t('backup.restoreConfirmTitle')}
message={t('backup.restoreWarning')}
confirmLabel={t('backup.restoreConfirm')}
danger
>
{restoreConfirm && (
<>
{restoreConfirm.filename}
{t('backup.restoreTip')}
>
)}
{/* Delete question */}
setDeleteTarget(null)}
onConfirm={executeDelete}
title={t('common.delete')}
message={deleteTarget ? t('backup.confirm.delete', { name: deleteTarget }) : ''}
confirmLabel={t('common.delete')}
danger
/>
)
}