import { Head, router } from '@inertiajs/react';
import { DatabaseBackup, Download } from 'lucide-react';
import { useState } from 'react';
import EmptyState from '@/components/member/empty-state';
import MemberPage from '@/components/member/member-page';
import { Button } from '@/components/ui/button';
import { formatDateTime } from '@/lib/format';
import { download, index, store } from '@/routes/admin/backups';

function size(bytes: number): string {
    return bytes >= 1024 * 1024
        ? `${(bytes / (1024 * 1024)).toFixed(1)} MB`
        : `${Math.max(1, Math.round(bytes / 1024))} KB`;
}

export default function Backups({
    backups,
}: {
    backups: { name: string; size: number; at: string }[];
}) {
    const [busy, setBusy] = useState(false);

    return (
        <>
            <Head title="Backups" />

            <MemberPage
                title="Database backups"
                description="A compressed copy of the database is saved every night at 02:00; the 14 newest are kept. Download one to keep it off the server."
                actions={
                    <Button
                        type="button"
                        disabled={busy}
                        onClick={() =>
                            router.post(
                                store.url(),
                                {},
                                {
                                    onStart: () => setBusy(true),
                                    onFinish: () => setBusy(false),
                                },
                            )
                        }
                    >
                        <DatabaseBackup className="size-4" />
                        {busy ? 'Backing up…' : 'Back up now'}
                    </Button>
                }
            >
                {backups.length === 0 ? (
                    <EmptyState
                        icon={DatabaseBackup}
                        title="No backups yet"
                        description="The first one is made tonight, or press “Back up now”."
                    />
                ) : (
                    <ul className="divide-y divide-surface-100 rounded-landing-lg border border-surface-200 bg-white shadow-landing-sm">
                        {backups.map((backup) => (
                            <li
                                key={backup.name}
                                className="flex items-center gap-3 px-4 py-3"
                            >
                                <span className="min-w-0 flex-1">
                                    <span className="block truncate font-mono text-sm text-ink">
                                        {backup.name}
                                    </span>
                                    <span className="block text-xs text-ink-400">
                                        {formatDateTime(backup.at)} ·{' '}
                                        {size(backup.size)}
                                    </span>
                                </span>
                                <a
                                    href={download.url(backup.name)}
                                    className="inline-flex items-center gap-1.5 rounded-md border border-input px-3 py-1.5 text-sm font-semibold text-ink-700 hover:bg-surface-50"
                                >
                                    <Download className="size-4" />
                                    Download
                                </a>
                            </li>
                        ))}
                    </ul>
                )}
            </MemberPage>
        </>
    );
}

Backups.layout = {
    breadcrumbs: [{ title: 'Backups', href: index() }],
};
