import { type BreadcrumbItem, type SharedData } from '@/types';
import { DndContext, DragEndEvent, DragOverlay, DragStartEvent, PointerSensor, useSensor, useSensors } from '@dnd-kit/core';
import { Head, Link, router, useForm, usePage } from '@inertiajs/react';
import { Archive, ChevronRight, ExternalLink, File as FileIcon, Folder as FolderIcon, FolderPlus, Info, Pencil, Trash2, X } from 'lucide-react';
import { FormEventHandler, useEffect, useRef, useState } from 'react';

import { BulkEditFilesDialog, type BulkEditPayload } from '@/components/bulk-edit-files-dialog';
import { CommentsRowButton } from '@/components/comments/comments-row-button';
import { ConfirmDialog } from '@/components/confirm-dialog';
import { DetailsPanel, DetailsTarget } from '@/components/details-panel';
import { DownloadAction, DownloadLimitNote } from '@/components/download-action';
import { DragChip, DragData, DropZone, useFolderDrop, useRowDrag } from '@/components/file-dnd';
import { FilePreviewDialog } from '@/components/file-preview-dialog';
import { ScanBadge, type ScanState } from '@/components/files/scan-badge';
import { VersionBadge, type VersionLinks } from '@/components/files/version-badge';
import Heading from '@/components/heading';
import { FilterField, ListToolbar } from '@/components/list-toolbar';
import { Pagination, PaginationMeta } from '@/components/pagination';
import { PreviewAction } from '@/components/preview-action';
import { SortableHeader, type SortDirection } from '@/components/sortable-header';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Checkbox } from '@/components/ui/checkbox';
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip';
import { ViewModeToggle } from '@/components/view-mode-toggle';
import { ZipDownloadDialog } from '@/components/zip-download-dialog';
import { useCapability } from '@/hooks/use-capability';
import { useFormatDate } from '@/hooks/use-format-date';
import { ALL, useListQuery } from '@/hooks/use-list-query';
import { type Selection, useSelection } from '@/hooks/use-selection';
import { useTranslation } from '@/hooks/use-translation';
import { useViewMode } from '@/hooks/use-view-mode';
import { useZipDownload } from '@/hooks/use-zip-download';
import AppLayout from '@/layouts/app-layout';
import { categoryColor } from '@/lib/category-colors';
import { formatBytes } from '@/lib/format-bytes';
import { isPreviewable } from '@/lib/previews';
import { isThumbnailable } from '@/lib/thumbnails';
import { slugify } from '@/lib/utils';
import { type DownloadLimit } from '@/types/portal';

interface FolderRow {
    id: number;
    name: string;
    public: boolean;
    public_url: string | null;
    children_count: number;
    files_count: number;
    shared_count: number;
    can_update: boolean;
    can_delete: boolean;
}

interface FileRow {
    id: number;
    name: string;
    original_name: string;
    mime_type: string;
    size: number;
    uploader: { name: string; type: 'staff' | 'client'; role: string | null } | null;
    public: boolean;
    public_url: string | null;
    expired: boolean;
    /** What the virus scanner made of it. Null when scanning is off. */
    scan: ScanState | null;
    assignments_count: number;
    downloads_count: number;
    /**
     * The cap on this file as it applies to the viewing staff member —
     * already decided server-side, including their exemption when they
     * uploaded it themselves.
     */
    download_limit: DownloadLimit;
    comments_count: number;
    /** Only ever non-zero for a moderator. */
    pending_comments_count: number;
    created_at: string | null;
    /** Already narrowed to what this viewer may be told; nulls mean "no link, or not yours to know". */
    version: VersionLinks;
    can_update: boolean;
    can_delete: boolean;
    categories: CategoryTag[];
}

type SortColumn = 'name' | 'size' | 'date';

interface Crumb {
    id: number;
    name: string;
}

interface CategoryTag {
    id: number;
    name: string;
    color: string;
}

interface FilesIndexProps {
    folder: Crumb | null;
    breadcrumb: Crumb[];
    folders: FolderRow[];
    files: FileRow[];
    pagination: PaginationMeta;
    search: string;
    searching: boolean;
    /** The order the rows come in, chosen or not. */
    sort: SortColumn;
    direction: SortDirection;
    /** False while the folder's own order applies: newest first in a folder, by name across a search. */
    sort_chosen: boolean;
    category: number | null;
    categories: CategoryTag[];
    uploader: number | null;
    visibility: 'public' | 'private' | null;
    downloads: 'none' | 'any' | null;
    role: number | null;
    version: 'current' | 'outdated' | null;
    uploader_options: Crumb[];
    role_options: Crumb[];
    expired: boolean;
    can_create_folders: boolean;
    can_upload: boolean;
    can_manage_public: boolean;
    comments_enabled: boolean;
    folder_options: Crumb[];
}

export default function FilesIndex({
    folder,
    breadcrumb,
    folders,
    files,
    pagination,
    search,
    searching,
    sort,
    direction,
    sort_chosen,
    category,
    categories,
    uploader,
    visibility,
    downloads,
    role,
    version,
    uploader_options,
    role_options,
    expired,
    can_create_folders,
    can_upload,
    can_manage_public,
    comments_enabled,
    folder_options,
}: FilesIndexProps) {
    const { t } = useTranslation();
    const { auth } = usePage<SharedData>().props;
    const canSetCategories = auth.permissions.includes('set_file_categories');
    const canSetExpiration = auth.permissions.includes('set_file_expiration_date');
    const canLimitDownloads = auth.permissions.includes('limit_downloads');
    const { viewMode, setViewMode } = useViewMode();
    const [newFolderOpen, setNewFolderOpen] = useState(false);
    const [panelTarget, setPanelTarget] = useState<DetailsTarget | null>(null);

    const zip = useZipDownload();
    // Withheld on some hosted plans. Only the zip buttons go: selection
    // also drives bulk edit here, so the checkboxes stay.
    const canZip = useCapability('downloads.zip');
    // Everything that changes which rows the list holds, except the page,
    // the page size and the order. A change here starts a new selection.
    const filterKey = JSON.stringify([folder?.id, search, category, uploader, visibility, downloads, role, version, expired]);
    const selection = useSelection({
        scope: 'files.index',
        resetKey: filterKey,
        rows: [
            ...folders.map((row) => ({ kind: 'folder' as const, id: row.id })),
            ...files.map((row) => ({ kind: 'file' as const, id: row.id, deletable: row.can_delete })),
        ],
    });
    const selectionCount = selection.count;
    const rowsOnPage = folders.length + files.length;
    const [selectingAll, setSelectingAll] = useState(false);
    const [selectAllFailed, setSelectAllFailed] = useState(false);
    const filterKeyRef = useRef(filterKey);
    filterKeyRef.current = filterKey;

    const clearSelection = () => {
        selection.clear();
        setSelectAllFailed(false);
    };

    const selectAllMatching = () => {
        const params = new URLSearchParams(window.location.search);
        params.delete('page');
        const requestedFor = filterKey;
        setSelectingAll(true);
        setSelectAllFailed(false);

        fetch(`${route('files.selection')}?${params.toString()}`, { credentials: 'same-origin', headers: { Accept: 'application/json' } })
            .then((response) => {
                if (!response.ok) throw new Error(String(response.status));
                return response.json() as Promise<{ folder_ids: number[]; file_ids: number[]; deletable_file_ids: number[] }>;
            })
            .then((ids) => {
                // The filters moved on while this was in flight.
                if (filterKeyRef.current !== requestedFor) return;
                selection.replace({ folderIds: ids.folder_ids, fileIds: ids.file_ids, deletableFileIds: ids.deletable_file_ids });
            })
            .catch(() => setSelectAllFailed(true))
            .finally(() => setSelectingAll(false));
    };

    const downloadSelectionAsZip = () => zip.start({ file_ids: [...selection.fileIds], folder_ids: [...selection.folderIds] });

    const bulkEditFiles = (payload: BulkEditPayload) =>
        router.patch(
            route('files.bulk-update'),
            { file_ids: [...selection.fileIds], ...payload },
            { preserveScroll: true, preserveState: false, onSuccess: () => clearSelection() },
        );

    // Only the selected files this person may delete. The server asks
    // each one again (FilesController::bulkDestroy); this decides whether
    // the button shows and what it says.
    const deletableFileIds = selection.deletableFileIds;

    const bulkDeleteFiles = () =>
        router.delete(route('files.bulk-destroy'), {
            data: { file_ids: deletableFileIds },
            preserveScroll: true,
            preserveState: false,
            onSuccess: () => clearSelection(),
        });

    // A search term or a category filter both switch to a flat, global view;
    // the server ignores the folder while one is set.
    // `sort`, `direction` and `per_page` ride along so a new filter keeps
    // them. Reset leaves them alone too: they are how the list is shown,
    // not what is in it.
    // The open folder goes along with every change, as in My files, or
    // sorting inside a folder went back to the top of the library (#1844).
    const perPage = pagination.per_page !== undefined && pagination.per_page !== pagination.per_page_options?.[0] ? String(pagination.per_page) : '';
    const { values, set, setMany, reset } = useListQuery(
        'files.index',
        {
            sort: sort_chosen ? sort : '',
            direction: sort_chosen ? direction : '',
            per_page: perPage,
            search,
            category: category === null ? ALL : String(category),
            uploader: uploader === null ? ALL : String(uploader),
            visibility: visibility ?? ALL,
            downloads: downloads ?? ALL,
            role: role === null ? ALL : String(role),
            version: version ?? ALL,
            expired: expired ? 'true' : '',
        },
        {
            sort: sort_chosen ? sort : '',
            direction: sort_chosen ? direction : '',
            per_page: perPage,
            search: '',
            category: ALL,
            uploader: ALL,
            visibility: ALL,
            downloads: ALL,
            role: ALL,
            version: ALL,
            expired: '',
        },
        folder !== null ? { folder: folder.id } : undefined,
    );
    const sortBy = (column: string, nextDirection: SortDirection) => setMany({ sort: column, direction: nextDirection });
    // The Clear button is for what narrows the list, not how it is shown.
    const filtering = Object.entries(values).some(
        ([key, value]) => !['sort', 'direction', 'per_page'].includes(key) && value !== '' && value !== ALL,
    );

    // A small drag threshold so clicking action buttons never starts a drag.
    // While searching, results are flat/global, so dragging (which reparents
    // relative to the current folder) is disabled by passing no sensors.
    const dragSensors = useSensors(useSensor(PointerSensor, { activationConstraint: { distance: 6 } }));
    const sensors = searching ? [] : dragSensors;

    // dnd-kit doesn't cancel the click that fires on pointer-up after a drag,
    // so dropping onto a folder/breadcrumb <Link> would also navigate to it.
    // We arm a one-shot capture that swallows that trailing click. A plain
    // click never crosses the 6px threshold, so onDragStart never fires and
    // normal navigation is untouched.
    const suppressNextClick = useRef(false);
    const [activeDrag, setActiveDrag] = useState<DragData | null>(null);
    useEffect(() => {
        const swallow = (e: MouseEvent) => {
            if (!suppressNextClick.current) return;
            e.preventDefault();
            e.stopPropagation();
            suppressNextClick.current = false;
        };
        document.addEventListener('click', swallow, true);
        return () => document.removeEventListener('click', swallow, true);
    }, []);

    const onDragStart = (event: DragStartEvent) => {
        suppressNextClick.current = true;
        setActiveDrag((event.active.data.current as DragData | undefined) ?? null);
    };

    const onDragEnd = (event: DragEndEvent) => {
        setActiveDrag(null);
        // Fallback: if no trailing click lands (dropped on empty space), clear
        // the guard so the next genuine click isn't eaten.
        window.setTimeout(() => {
            suppressNextClick.current = false;
        }, 300);

        const dragged = event.active.data.current as { kind: 'file' | 'folder'; id: number } | undefined;
        const dropFolderId = (event.over?.data.current as { folderId: number | null } | undefined)?.folderId;
        if (!dragged || event.over === null || dropFolderId === undefined) return;

        if (dragged.kind === 'file') {
            router.patch(route('files.move', dragged.id), { folder_id: dropFolderId }, { preserveScroll: true, preserveState: false });
        } else if (dragged.id !== dropFolderId) {
            router.patch(route('folders.move', dragged.id), { parent_id: dropFolderId }, { preserveScroll: true, preserveState: false });
        }
    };

    const breadcrumbs: BreadcrumbItem[] = [{ title: t('Files'), href: '/files' }];
    // Opening a folder keeps the chosen order and page size.
    const viewParams = Object.fromEntries(
        Object.entries({ sort: values.sort, direction: values.direction, per_page: values.per_page }).filter(([, v]) => v !== ''),
    );
    const folderUrl = (id: number | null) => (id === null ? route('files.index', viewParams) : route('files.index', { folder: id, ...viewParams }));

    const createForm = useForm({
        name: '',
        parent_id: folder?.id ?? null,
        public: false as boolean,
        slug: '',
        allow_client_uploads: false as boolean,
    });
    const [createSlugTouched, setCreateSlugTouched] = useState(false);
    const createFolder: FormEventHandler = (e) => {
        e.preventDefault();
        createForm.post(route('folders.store'), {
            preserveScroll: true,
            onSuccess: () => {
                createForm.reset('name', 'public', 'slug', 'allow_client_uploads');
                setCreateSlugTouched(false);
                setNewFolderOpen(false);
            },
        });
    };

    return (
        <AppLayout breadcrumbs={breadcrumbs}>
            <Head title={t('Files')} />

            <div className="px-4 py-6">
                <div className="flex flex-wrap items-start justify-between gap-x-4 [&>:not(:first-child)]:mb-6">
                    <Heading title={t('Files')} description={t('Your shared file library')} />
                    <div className="flex gap-2">
                        {canZip && folder !== null && !searching && (
                            <Button variant="outline" onClick={() => zip.start({ folder_ids: [folder.id] })}>
                                <Archive className="size-4" />
                                {t('Download as zip')}
                            </Button>
                        )}
                        {can_create_folders && !can_upload && (
                            <TooltipProvider delayDuration={0}>
                                <Tooltip>
                                    <TooltipTrigger asChild>
                                        <span>
                                            <Button variant="outline" disabled>
                                                <FolderPlus className="size-4" />
                                                {t('New folder')}
                                            </Button>
                                        </span>
                                    </TooltipTrigger>
                                    <TooltipContent>{t('You need upload permission to create folders')}</TooltipContent>
                                </Tooltip>
                            </TooltipProvider>
                        )}
                        {can_create_folders && can_upload && (
                            <Dialog open={newFolderOpen} onOpenChange={setNewFolderOpen}>
                                <DialogTrigger asChild>
                                    <Button variant="outline">
                                        <FolderPlus className="size-4" />
                                        {t('New folder')}
                                    </Button>
                                </DialogTrigger>
                                <DialogContent>
                                    <DialogHeader>
                                        <DialogTitle>{t('New folder')}</DialogTitle>
                                    </DialogHeader>
                                    <form onSubmit={createFolder} className="space-y-4">
                                        <div className="grid gap-2">
                                            <Label htmlFor="folder-name">{t('Name')}</Label>
                                            <Input
                                                id="folder-name"
                                                value={createForm.data.name}
                                                onChange={(e) => {
                                                    createForm.setData('name', e.target.value);
                                                    if (!createSlugTouched) createForm.setData('slug', slugify(e.target.value));
                                                }}
                                                autoFocus
                                                required
                                            />
                                        </div>

                                        {can_manage_public && (
                                            <div className="grid gap-2">
                                                <div className="flex items-start gap-2">
                                                    <Checkbox
                                                        id="new-folder-public"
                                                        checked={createForm.data.public}
                                                        onCheckedChange={(checked) => createForm.setData('public', checked === true)}
                                                    />
                                                    <Label htmlFor="new-folder-public" className="font-normal">
                                                        {t(
                                                            'Make this folder public. Every file inside it — now and later, at any depth — becomes public too.',
                                                        )}
                                                    </Label>
                                                </div>

                                                {createForm.data.public && (
                                                    <div className="mt-2 grid gap-4 pl-6">
                                                        <div className="grid gap-2">
                                                            <Label htmlFor="new-folder-slug">{t('URL slug')}</Label>
                                                            <Input
                                                                id="new-folder-slug"
                                                                value={createForm.data.slug}
                                                                onChange={(e) => {
                                                                    setCreateSlugTouched(true);
                                                                    createForm.setData('slug', e.target.value);
                                                                }}
                                                                required
                                                            />
                                                        </div>

                                                        <div className="flex items-start gap-2">
                                                            <Checkbox
                                                                id="new-folder-allow-client-uploads"
                                                                checked={createForm.data.allow_client_uploads}
                                                                onCheckedChange={(checked) =>
                                                                    createForm.setData('allow_client_uploads', checked === true)
                                                                }
                                                            />
                                                            <Label htmlFor="new-folder-allow-client-uploads" className="font-normal">
                                                                {t('Allow clients to add files to this folder from their portal.')}
                                                            </Label>
                                                        </div>
                                                    </div>
                                                )}
                                            </div>
                                        )}

                                        <DialogFooter>
                                            <Button type="submit" disabled={createForm.processing}>
                                                {t('Create folder')}
                                            </Button>
                                        </DialogFooter>
                                    </form>
                                </DialogContent>
                            </Dialog>
                        )}
                        {can_upload && (
                            <Button asChild>
                                {/* Into the folder on screen, not the top of the
                                    library (#1801). Not while searching: the
                                    results span folders, so there is no "here". */}
                                <Link href={route('files.create', folder !== null && !searching ? { folder: folder.id } : {})}>{t('Upload')}</Link>
                            </Button>
                        )}
                    </div>
                </div>

                <ListToolbar showClear={filtering} onClear={reset}>
                    <FilterField label={t('Search')} htmlFor="files-search">
                        <Input
                            id="files-search"
                            type="search"
                            placeholder={t('Search all files and folders')}
                            className="w-80"
                            value={values.search}
                            onChange={(e) => set('search', e.target.value, true)}
                        />
                    </FilterField>
                    {categories.length > 0 && (
                        <FilterField label={t('Category')} htmlFor="files-category">
                            <Select value={values.category} onValueChange={(v) => set('category', v)}>
                                <SelectTrigger id="files-category" className="w-48">
                                    <SelectValue />
                                </SelectTrigger>
                                <SelectContent>
                                    <SelectItem value={ALL}>{t('All categories')}</SelectItem>
                                    {categories.map((c) => (
                                        <SelectItem key={c.id} value={String(c.id)}>
                                            <span className="flex items-center gap-2">
                                                <span className={`size-2 shrink-0 rounded-full ${categoryColor(c.color).swatch}`} />
                                                {c.name}
                                            </span>
                                        </SelectItem>
                                    ))}
                                </SelectContent>
                            </Select>
                        </FilterField>
                    )}
                    {uploader_options.length > 0 && (
                        <FilterField label={t('Uploaded by')} htmlFor="files-uploader">
                            <Select value={values.uploader} onValueChange={(v) => set('uploader', v)}>
                                <SelectTrigger id="files-uploader" className="w-48">
                                    <SelectValue />
                                </SelectTrigger>
                                <SelectContent>
                                    <SelectItem value={ALL}>{t('All users')}</SelectItem>
                                    {uploader_options.map((u) => (
                                        <SelectItem key={u.id} value={String(u.id)}>
                                            {u.name}
                                        </SelectItem>
                                    ))}
                                </SelectContent>
                            </Select>
                        </FilterField>
                    )}
                    {role_options.length > 0 && (
                        <FilterField label={t('Uploader role')} htmlFor="files-role">
                            <Select value={values.role} onValueChange={(v) => set('role', v)}>
                                <SelectTrigger id="files-role" className="w-48">
                                    <SelectValue />
                                </SelectTrigger>
                                <SelectContent>
                                    <SelectItem value={ALL}>{t('All roles')}</SelectItem>
                                    {role_options.map((r) => (
                                        <SelectItem key={r.id} value={String(r.id)}>
                                            {r.name}
                                        </SelectItem>
                                    ))}
                                </SelectContent>
                            </Select>
                        </FilterField>
                    )}
                    <FilterField label={t('Visibility')} htmlFor="files-visibility">
                        <Select value={values.visibility} onValueChange={(v) => set('visibility', v)}>
                            <SelectTrigger id="files-visibility" className="w-40">
                                <SelectValue />
                            </SelectTrigger>
                            <SelectContent>
                                <SelectItem value={ALL}>{t('All files')}</SelectItem>
                                <SelectItem value="public">{t('Public')}</SelectItem>
                                <SelectItem value="private">{t('Private')}</SelectItem>
                            </SelectContent>
                        </Select>
                    </FilterField>
                    <FilterField label={t('Downloads')} htmlFor="files-downloads">
                        <Select value={values.downloads} onValueChange={(v) => set('downloads', v)}>
                            <SelectTrigger id="files-downloads" className="w-48">
                                <SelectValue />
                            </SelectTrigger>
                            <SelectContent>
                                <SelectItem value={ALL}>{t('Any number of downloads')}</SelectItem>
                                <SelectItem value="none">{t('Never downloaded')}</SelectItem>
                                <SelectItem value="any">{t('Downloaded at least once')}</SelectItem>
                            </SelectContent>
                        </Select>
                    </FilterField>
                    <FilterField label={t('Version')} htmlFor="files-version">
                        <Select value={values.version} onValueChange={(v) => set('version', v)}>
                            <SelectTrigger id="files-version" className="w-44">
                                <SelectValue />
                            </SelectTrigger>
                            <SelectContent>
                                <SelectItem value={ALL}>{t('All versions')}</SelectItem>
                                <SelectItem value="current">{t('Current version')}</SelectItem>
                                <SelectItem value="outdated">{t('Outdated')}</SelectItem>
                            </SelectContent>
                        </Select>
                    </FilterField>
                    <FilterField label={t('Expired')} htmlFor="files-expired">
                        <div className="flex h-9 items-center gap-2">
                            <Checkbox
                                id="files-expired"
                                checked={values.expired === 'true'}
                                onCheckedChange={(checked) => set('expired', checked === true ? 'true' : '')}
                            />
                            <Label htmlFor="files-expired" className="text-muted-foreground font-normal">
                                {t('Expired only')}
                            </Label>
                        </div>
                    </FilterField>
                </ListToolbar>

                <div className="mb-3 flex items-center justify-between gap-3">
                    {viewMode === 'grid' && rowsOnPage > 0 ? (
                        <label className="text-muted-foreground flex items-center gap-2 text-sm">
                            <Checkbox checked={selection.pageState} onCheckedChange={(checked) => selection.togglePage(checked === true)} />
                            {t('Select all on this page')}
                        </label>
                    ) : (
                        <span />
                    )}
                    <div className="flex items-center gap-2">
                        <Select value={`${sort}:${direction}`} onValueChange={(value) => sortBy(...(value.split(':') as [string, SortDirection]))}>
                            <SelectTrigger className="h-9 w-auto min-w-[10rem]" aria-label={t('Sort by')}>
                                <SelectValue />
                            </SelectTrigger>
                            <SelectContent>
                                <SelectItem value="name:asc">{t('Name (A–Z)')}</SelectItem>
                                <SelectItem value="name:desc">{t('Name (Z–A)')}</SelectItem>
                                <SelectItem value="date:desc">{t('Newest first')}</SelectItem>
                                <SelectItem value="date:asc">{t('Oldest first')}</SelectItem>
                                <SelectItem value="size:desc">{t('Largest first')}</SelectItem>
                                <SelectItem value="size:asc">{t('Smallest first')}</SelectItem>
                            </SelectContent>
                        </Select>
                        <ViewModeToggle value={viewMode} onChange={setViewMode} />
                    </div>
                </div>

                <DndContext sensors={sensors} onDragStart={onDragStart} onDragEnd={onDragEnd} onDragCancel={() => setActiveDrag(null)}>
                    {searching ? (
                        <p className="text-muted-foreground mb-3 text-sm">{t('Showing matches from across your library')}</p>
                    ) : (
                        <nav className="text-muted-foreground mb-3 flex flex-wrap items-center gap-1 text-sm">
                            <DropZone folderId={null} className="px-1">
                                <Link href={folderUrl(null)} className="hover:text-foreground">
                                    {t('Library')}
                                </Link>
                            </DropZone>
                            {breadcrumb.map((crumb, index) => (
                                <span key={crumb.id} className="flex items-center gap-1">
                                    <ChevronRight className="size-3.5" />
                                    {index === breadcrumb.length - 1 ? (
                                        <span className="text-foreground">{crumb.name}</span>
                                    ) : (
                                        <DropZone folderId={crumb.id} className="px-1">
                                            <Link href={folderUrl(crumb.id)} className="hover:text-foreground">
                                                {crumb.name}
                                            </Link>
                                        </DropZone>
                                    )}
                                </span>
                            ))}
                        </nav>
                    )}

                    {selectionCount > 0 && (
                        <div className="bg-muted/40 mb-3 flex items-center justify-between gap-3 rounded-lg border px-4 py-2">
                            <div className="flex flex-wrap items-center gap-x-3 gap-y-1 text-sm">
                                <p className="font-medium" aria-live="polite">
                                    {selectionCount === pagination.total
                                        ? t('All :count items are selected.', { count: pagination.total })
                                        : t(':count selected', { count: selectionCount })}
                                </p>
                                {selection.pageState === true && selectionCount < pagination.total && (
                                    <Button variant="link" size="sm" className="h-auto p-0" disabled={selectingAll} onClick={selectAllMatching}>
                                        {selectingAll ? t('Selecting…') : t('Select all :count items', { count: pagination.total })}
                                    </Button>
                                )}
                                {selectAllFailed && <span className="text-destructive text-xs">{t('Could not select every item. Try again.')}</span>}
                            </div>
                            <div className="flex flex-wrap items-center justify-end gap-2">
                                {canZip && (
                                    <Button size="sm" onClick={downloadSelectionAsZip}>
                                        <Archive className="size-4" />
                                        {t('Download as zip')}
                                    </Button>
                                )}
                                {selection.fileIds.size === 0 ? (
                                    <TooltipProvider delayDuration={0}>
                                        <Tooltip>
                                            <TooltipTrigger asChild>
                                                <span>
                                                    <Button size="sm" variant="outline" disabled>
                                                        <Pencil className="size-4" />
                                                        {t('Edit')}
                                                    </Button>
                                                </span>
                                            </TooltipTrigger>
                                            <TooltipContent>{t('Select files to bulk-edit them')}</TooltipContent>
                                        </Tooltip>
                                    </TooltipProvider>
                                ) : (
                                    <BulkEditFilesDialog
                                        trigger={
                                            <Button size="sm" variant="outline">
                                                <Pencil className="size-4" />
                                                {t('Edit')}
                                            </Button>
                                        }
                                        count={selection.fileIds.size}
                                        folderOptions={folder_options}
                                        categories={categories}
                                        canSetCategories={canSetCategories}
                                        canSetExpiration={canSetExpiration}
                                        canLimitDownloads={canLimitDownloads}
                                        onConfirm={bulkEditFiles}
                                    />
                                )}
                                {deletableFileIds.length > 0 && (
                                    <ConfirmDialog
                                        trigger={
                                            <Button size="sm" variant="outline" className="text-destructive hover:text-destructive">
                                                <Trash2 className="size-4" />
                                                {t('Delete')}
                                            </Button>
                                        }
                                        title={t('Delete the selected files?')}
                                        description={
                                            deletableFileIds.length === selection.fileIds.size
                                                ? t('They will no longer be available to anyone they were shared with.')
                                                : t(
                                                      'Only the files you are allowed to delete are removed. They will no longer be available to anyone they were shared with.',
                                                  )
                                        }
                                        confirmLabel={t('Delete')}
                                        onConfirm={bulkDeleteFiles}
                                    />
                                )}
                                <Button variant="ghost" size="sm" onClick={clearSelection}>
                                    <X className="size-4" />
                                    {t('Clear')}
                                </Button>
                            </div>
                        </div>
                    )}

                    {folders.length === 0 && files.length === 0 && (
                        <p className="text-muted-foreground rounded-lg border px-4 py-10 text-center text-sm">
                            {searching ? t('No files or folders match your search.') : t('This folder is empty.')}
                        </p>
                    )}

                    {viewMode === 'list' ? (
                        (folders.length > 0 || files.length > 0) && (
                            <div className="relative overflow-x-auto rounded-lg border">
                                <table className="w-full text-sm">
                                    <thead className="bg-muted/40 border-b text-left">
                                        <tr>
                                            <th className="w-8 px-2 py-2.5">
                                                <Checkbox
                                                    checked={selection.pageState}
                                                    onCheckedChange={(checked) => selection.togglePage(checked === true)}
                                                    aria-label={t('Select all on this page')}
                                                />
                                            </th>
                                            <SortableHeader column="name" sort={sort} direction={direction} onSort={sortBy}>
                                                {t('Name')}
                                            </SortableHeader>
                                            <SortableHeader column="size" sort={sort} direction={direction} onSort={sortBy} firstDirection="desc">
                                                {t('Size')}
                                            </SortableHeader>
                                            <SortableHeader column="date" sort={sort} direction={direction} onSort={sortBy} firstDirection="desc">
                                                {t('Uploaded')}
                                            </SortableHeader>
                                            <th className="px-4 py-2.5 font-medium">
                                                <span className="sr-only">{t('Activity')}</span>
                                            </th>
                                            <th className="px-4 py-2.5 text-right font-medium">{t('Actions')}</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {folders.map((row) => (
                                            <FolderRow
                                                key={`folder-${row.id}`}
                                                row={row}
                                                folderUrl={folderUrl}
                                                onDetails={() => setPanelTarget({ type: 'folder', id: row.id })}
                                                checkbox={selection.checkboxProps({ kind: 'folder', id: row.id })}
                                            />
                                        ))}
                                        {files.map((row) => (
                                            <FileRow
                                                key={`file-${row.id}`}
                                                row={row}
                                                onDetails={() => setPanelTarget({ type: 'file', id: row.id })}
                                                onComments={
                                                    comments_enabled ? () => setPanelTarget({ type: 'file', id: row.id, tab: 'comments' }) : undefined
                                                }
                                                checkbox={selection.checkboxProps({ kind: 'file', id: row.id, deletable: row.can_delete })}
                                            />
                                        ))}
                                    </tbody>
                                </table>
                            </div>
                        )
                    ) : (
                        <div className="grid grid-cols-2 gap-4 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 xl:grid-cols-6">
                            {folders.map((row) => (
                                <FolderCard
                                    key={`folder-${row.id}`}
                                    row={row}
                                    folderUrl={folderUrl}
                                    onDetails={() => setPanelTarget({ type: 'folder', id: row.id })}
                                    checkbox={selection.checkboxProps({ kind: 'folder', id: row.id })}
                                />
                            ))}
                            {files.map((row) => (
                                <FileCard
                                    key={`file-${row.id}`}
                                    row={row}
                                    onDetails={() => setPanelTarget({ type: 'file', id: row.id })}
                                    onComments={comments_enabled ? () => setPanelTarget({ type: 'file', id: row.id, tab: 'comments' }) : undefined}
                                    checkbox={selection.checkboxProps({ kind: 'file', id: row.id, deletable: row.can_delete })}
                                />
                            ))}
                        </div>
                    )}

                    <DragOverlay dropAnimation={null}>{activeDrag && <DragChip data={activeDrag} />}</DragOverlay>
                </DndContext>

                <Pagination meta={pagination} />

                {!searching && (
                    <p className="text-muted-foreground mt-2 text-xs">
                        {t('Tip: drag files and folders onto a folder or a breadcrumb to move them.')}
                    </p>
                )}
            </div>

            {panelTarget && <DetailsPanel target={panelTarget} onClose={() => setPanelTarget(null)} />}
            <ZipDownloadDialog status={zip.status} error={zip.error} onClose={zip.close} />
        </AppLayout>
    );
}

type CheckboxProps = ReturnType<Selection['checkboxProps']>;

// The whole row is the drag handle (and, for folders, the drop target). A 6px
// activation distance keeps clicks on the title/action buttons from starting a
// drag; the dimmed source row plus the floating DragChip show what's moving.
const rowBase = 'group hover:bg-muted/40 cursor-grab border-b select-none last:border-0 active:cursor-grabbing';

function FolderRow({
    row,
    folderUrl,
    onDetails,
    checkbox,
}: {
    row: FolderRow;
    folderUrl: (id: number | null) => string;
    onDetails: () => void;
    checkbox: CheckboxProps;
}) {
    const { t } = useTranslation();
    const { setDragRef, dragHandleProps, isDragging } = useRowDrag({ kind: 'folder', id: row.id, name: row.name });
    const { setDropRef, isOver } = useFolderDrop(row.id);
    const setRef = (node: HTMLTableRowElement | null) => {
        setDragRef(node);
        setDropRef(node);
    };

    return (
        <tr
            ref={setRef}
            {...dragHandleProps}
            className={`${rowBase} ${isDragging ? 'opacity-40' : ''} ${isOver ? 'bg-primary/15 [box-shadow:inset_0_0_0_2px_var(--primary)]' : ''}`}
        >
            <td className="w-8 px-2 py-2.5" onPointerDown={(e) => e.stopPropagation()}>
                <Checkbox {...checkbox} aria-label={t('Select :name', { name: row.name })} />
            </td>
            <td className="px-4 py-2.5">
                <Link href={folderUrl(row.id)} draggable={false} className="inline-flex items-center gap-2 font-medium">
                    <span className="bg-primary/10 text-primary flex size-10 shrink-0 items-center justify-center rounded">
                        <FolderIcon className="size-5" strokeWidth={1.75} />
                    </span>
                    {row.name}
                    {row.public && (
                        <Badge variant="secondary" className="text-[11px] font-normal">
                            {t('Public')}
                        </Badge>
                    )}
                </Link>
            </td>
            <td className="text-muted-foreground px-4 py-2.5 text-sm whitespace-nowrap">
                {t(':files files', { files: row.files_count })}
                {row.children_count > 0 && ` · ${t(':count folders', { count: row.children_count })}`}
            </td>
            <td />
            <td className="text-muted-foreground px-4 py-2.5 text-sm whitespace-nowrap">
                {row.shared_count > 0 && t('Shared with :count', { count: row.shared_count })}
            </td>
            <td className="px-4 py-2.5 text-right">
                <div className="flex justify-end gap-1">
                    <Button variant="ghost" size="sm" onClick={onDetails}>
                        <Info className="size-4" />
                        <span className="sr-only">{t('Details')}</span>
                    </Button>
                    {row.public_url && (
                        <Button variant="ghost" size="sm" asChild>
                            <a href={row.public_url} target="_blank" rel="noreferrer">
                                <ExternalLink className="size-4" />
                                <span className="sr-only">{t('Open public page')}</span>
                            </a>
                        </Button>
                    )}
                    <Button variant="outline" size="sm" asChild>
                        <Link href={route('folders.share', row.id)}>{row.can_update ? t('Edit') : t('View')}</Link>
                    </Button>
                    {row.can_delete && (
                        <ConfirmDialog
                            trigger={
                                <Button variant="ghost" size="sm" className="text-destructive hover:text-destructive">
                                    {t('Delete')}
                                </Button>
                            }
                            title={t('Delete folder?')}
                            description={t('The folder ":name" and everything inside it will be deleted. This can be undone by an administrator.', {
                                name: row.name,
                            })}
                            confirmLabel={t('Delete folder')}
                            onConfirm={() => router.delete(route('folders.destroy', row.id), { preserveScroll: true })}
                        />
                    )}
                </div>
            </td>
        </tr>
    );
}

function FileRow({
    row,
    onDetails,
    onComments,
    checkbox,
}: {
    row: FileRow;
    onDetails: () => void;
    /** Undefined when the installation has commenting switched off. */
    onComments?: () => void;
    checkbox: CheckboxProps;
}) {
    const { t } = useTranslation();
    const { date } = useFormatDate();
    const { setDragRef, dragHandleProps, isDragging } = useRowDrag({ kind: 'file', id: row.id, name: row.name });

    return (
        <tr ref={setDragRef} {...dragHandleProps} className={`${rowBase} ${isDragging ? 'opacity-40' : ''}`}>
            <td className="w-8 px-2 py-2.5" onPointerDown={(e) => e.stopPropagation()}>
                <Checkbox {...checkbox} aria-label={t('Select :name', { name: row.name })} />
            </td>
            <td className="px-4 py-2.5">
                <div className="flex items-start gap-2">
                    <FilePreviewDialog
                        previewUrl={route('files.preview', row.id)}
                        mimeType={row.mime_type}
                        fileName={row.original_name}
                        className="shrink-0"
                    >
                        {isThumbnailable(row.mime_type) ? (
                            <img src={route('files.thumbnail', row.id)} alt="" className="size-10 rounded border object-cover" draggable={false} />
                        ) : (
                            <span className="bg-muted text-muted-foreground flex size-10 shrink-0 items-center justify-center rounded border">
                                <FileIcon className="size-5" strokeWidth={1.75} />
                            </span>
                        )}
                    </FilePreviewDialog>
                    <div className="min-w-0">
                        <div className="flex items-center gap-1.5">
                            <Link href={route('files.edit', row.id)} draggable={false} className="font-medium">
                                {row.name}
                            </Link>
                            {row.public && (
                                <Badge variant="secondary" className="text-[11px] font-normal">
                                    {t('Public')}
                                </Badge>
                            )}
                            {row.expired && (
                                <Badge variant="destructive" className="text-[11px] font-normal">
                                    {t('Expired')}
                                </Badge>
                            )}
                            <ScanBadge scan={row.scan} />
                            <VersionBadge version={row.version} />
                        </div>
                        <p className="text-muted-foreground text-xs">
                            {row.original_name}
                            {row.uploader && (
                                <>
                                    {' · '}
                                    {row.uploader.name}
                                    {row.uploader.role && <span className="text-[10px] opacity-75"> ({row.uploader.role})</span>}
                                </>
                            )}
                        </p>
                        {row.categories.length > 0 && (
                            <div className="mt-1 flex flex-wrap gap-1">
                                {row.categories.map((category) => (
                                    <Badge
                                        key={category.id}
                                        variant="outline"
                                        className={`text-[11px] font-normal ${categoryColor(category.color).badge}`}
                                    >
                                        {category.name}
                                    </Badge>
                                ))}
                            </div>
                        )}
                    </div>
                </div>
            </td>
            <td className="text-muted-foreground px-4 py-2.5 text-sm whitespace-nowrap tabular-nums">{formatBytes(row.size)}</td>
            <td className="text-muted-foreground px-4 py-2.5 text-sm whitespace-nowrap tabular-nums">{date(row.created_at)}</td>
            <td className="text-muted-foreground px-4 py-2.5 text-sm whitespace-nowrap">
                {t(':count downloads', { count: row.downloads_count })}
                {row.download_limit.limit !== null && (
                    <>
                        {' · '}
                        <DownloadLimitNote limit={row.download_limit} />
                    </>
                )}
                {row.assignments_count > 0 && ` · ${t('Shared with :count', { count: row.assignments_count })}`}
            </td>
            <td className="px-4 py-2.5 text-right">
                <div className="flex justify-end gap-1">
                    <Button variant="ghost" size="sm" onClick={onDetails}>
                        <Info className="size-4" />
                        <span className="sr-only">{t('Details')}</span>
                    </Button>
                    {onComments && <CommentsRowButton count={row.comments_count} pending={row.pending_comments_count} onOpen={onComments} />}
                    {row.public_url && (
                        <Button variant="ghost" size="sm" asChild>
                            <a href={row.public_url} target="_blank" rel="noreferrer">
                                <ExternalLink className="size-4" />
                                <span className="sr-only">{t('Open public page')}</span>
                            </a>
                        </Button>
                    )}
                    <PreviewAction
                        previewUrl={route('files.preview', row.id)}
                        mimeType={row.mime_type}
                        fileName={row.original_name}
                        variant="ghost"
                        size="sm"
                        iconOnly
                    />
                    <DownloadAction href={route('files.download', row.id)} limit={row.download_limit} variant="ghost" size="sm" iconOnly />
                    <Button variant="outline" size="sm" asChild>
                        <Link href={route('files.edit', row.id)}>{row.can_update ? t('Edit') : t('View')}</Link>
                    </Button>
                    {row.can_delete && (
                        <ConfirmDialog
                            trigger={
                                <Button variant="ghost" size="sm" className="text-destructive hover:text-destructive">
                                    {t('Delete')}
                                </Button>
                            }
                            title={t('Delete file?')}
                            description={t('The file ":name" will no longer be available to anyone it was shared with.', {
                                name: row.name,
                            })}
                            confirmLabel={t('Delete file')}
                            onConfirm={() => router.delete(route('files.destroy', row.id), { preserveScroll: true })}
                        />
                    )}
                </div>
            </td>
        </tr>
    );
}

function FolderCard({
    row,
    folderUrl,
    onDetails,
    checkbox,
}: {
    row: FolderRow;
    folderUrl: (id: number | null) => string;
    onDetails: () => void;
    checkbox: CheckboxProps;
}) {
    const { t } = useTranslation();
    const { setDragRef, dragHandleProps, isDragging } = useRowDrag({ kind: 'folder', id: row.id, name: row.name });
    const { setDropRef, isOver } = useFolderDrop(row.id);
    const setRef = (node: HTMLDivElement | null) => {
        setDragRef(node);
        setDropRef(node);
    };

    return (
        <div
            ref={setRef}
            {...dragHandleProps}
            className={`group hover:border-primary/50 relative flex cursor-grab flex-col items-center justify-center gap-2 rounded-xl border p-6 text-center transition select-none hover:shadow-md active:cursor-grabbing ${isDragging ? 'opacity-40' : ''} ${isOver ? 'border-primary bg-primary/10' : ''}`}
        >
            <Checkbox
                {...checkbox}
                onPointerDown={(e) => e.stopPropagation()}
                aria-label={t('Select :name', { name: row.name })}
                className="absolute top-3 left-3"
            />
            <Link href={folderUrl(row.id)} draggable={false} className="flex w-full flex-col items-center gap-2">
                <FolderIcon className="text-primary size-10 shrink-0" strokeWidth={1.25} />
                <span className="flex max-w-full items-center gap-1.5">
                    <p className="truncate text-sm font-medium" title={row.name}>
                        {row.name}
                    </p>
                    {row.public && (
                        <Badge variant="secondary" className="text-[11px] font-normal">
                            {t('Public')}
                        </Badge>
                    )}
                </span>
                <p className="text-muted-foreground text-xs">
                    {t(':files files', { files: row.files_count })}
                    {row.children_count > 0 && ` · ${t(':count folders', { count: row.children_count })}`}
                </p>
            </Link>
            <div className="bg-background/80 absolute top-2 right-2 flex rounded-md opacity-0 transition-opacity group-hover:opacity-100 focus-within:opacity-100 hover:opacity-100">
                <Button variant="ghost" size="sm" onClick={onDetails}>
                    <Info className="size-4" />
                    <span className="sr-only">{t('Details')}</span>
                </Button>
                {row.public_url && (
                    <Button variant="ghost" size="sm" asChild>
                        <a href={row.public_url} target="_blank" rel="noreferrer">
                            <ExternalLink className="size-4" />
                            <span className="sr-only">{t('Open public page')}</span>
                        </a>
                    </Button>
                )}
                {row.can_delete && (
                    <ConfirmDialog
                        trigger={
                            <Button variant="ghost" size="sm" className="text-destructive hover:text-destructive">
                                <X className="size-4" />
                                <span className="sr-only">{t('Delete')}</span>
                            </Button>
                        }
                        title={t('Delete folder?')}
                        description={t('The folder ":name" and everything inside it will be deleted. This can be undone by an administrator.', {
                            name: row.name,
                        })}
                        confirmLabel={t('Delete folder')}
                        onConfirm={() => router.delete(route('folders.destroy', row.id), { preserveScroll: true })}
                    />
                )}
            </div>
        </div>
    );
}

function FileCard({
    row,
    onDetails,
    onComments,
    checkbox,
}: {
    row: FileRow;
    onDetails: () => void;
    /** Undefined when the installation has commenting switched off. */
    onComments?: () => void;
    checkbox: CheckboxProps;
}) {
    const { t } = useTranslation();
    const { setDragRef, dragHandleProps, isDragging } = useRowDrag({ kind: 'file', id: row.id, name: row.name });

    return (
        <div
            ref={setDragRef}
            {...dragHandleProps}
            className={`group hover:border-primary/50 relative cursor-grab overflow-hidden rounded-xl border transition select-none hover:shadow-lg active:cursor-grabbing ${isDragging ? 'opacity-40' : ''}`}
        >
            <Checkbox
                {...checkbox}
                onPointerDown={(e) => e.stopPropagation()}
                aria-label={t('Select :name', { name: row.name })}
                className="bg-background/80 absolute top-3 left-3 z-10"
            />

            {isPreviewable(row.mime_type) ? (
                <FilePreviewDialog
                    previewUrl={route('files.preview', row.id)}
                    mimeType={row.mime_type}
                    fileName={row.original_name}
                    className="bg-muted block aspect-square w-full overflow-hidden"
                >
                    {isThumbnailable(row.mime_type) ? (
                        <img
                            src={route('files.thumbnail', row.id)}
                            alt=""
                            draggable={false}
                            className="h-full w-full object-cover transition duration-300 group-hover:scale-105"
                        />
                    ) : (
                        <div className="flex h-full w-full items-center justify-center">
                            <FileIcon className="text-muted-foreground size-10" strokeWidth={1.25} />
                        </div>
                    )}
                </FilePreviewDialog>
            ) : (
                <div className="bg-muted flex aspect-square items-center justify-center">
                    <FileIcon className="text-muted-foreground size-10" strokeWidth={1.25} />
                </div>
            )}

            <div className="p-3">
                <div className="flex items-center gap-1.5">
                    <Link href={route('files.edit', row.id)} draggable={false} className="truncate text-sm font-medium" title={row.name}>
                        {row.name}
                    </Link>
                    {row.public && (
                        <Badge variant="secondary" className="text-[11px] font-normal">
                            {t('Public')}
                        </Badge>
                    )}
                    {row.expired && (
                        <Badge variant="destructive" className="text-[11px] font-normal">
                            {t('Expired')}
                        </Badge>
                    )}
                    <ScanBadge scan={row.scan} />
                    <VersionBadge version={row.version} />
                </div>
                <p className="text-muted-foreground truncate text-xs">
                    {formatBytes(row.size)}
                    {row.uploader && (
                        <>
                            {' · '}
                            {row.uploader.name}
                            {row.uploader.role && <span className="text-[10px] opacity-75"> ({row.uploader.role})</span>}
                        </>
                    )}
                </p>
                {row.categories.length > 0 && (
                    <div className="mt-1 flex flex-wrap gap-1">
                        {row.categories.map((category) => (
                            <Badge key={category.id} variant="outline" className={`text-[11px] font-normal ${categoryColor(category.color).badge}`}>
                                {category.name}
                            </Badge>
                        ))}
                    </div>
                )}
                <div className="mt-2 flex items-center justify-end gap-1">
                    <Button variant="ghost" size="sm" onClick={onDetails}>
                        <Info className="size-4" />
                        <span className="sr-only">{t('Details')}</span>
                    </Button>
                    {onComments && <CommentsRowButton count={row.comments_count} pending={row.pending_comments_count} onOpen={onComments} />}
                    {row.public_url && (
                        <Button variant="ghost" size="sm" asChild>
                            <a href={row.public_url} target="_blank" rel="noreferrer">
                                <ExternalLink className="size-4" />
                                <span className="sr-only">{t('Open public page')}</span>
                            </a>
                        </Button>
                    )}
                    <PreviewAction
                        previewUrl={route('files.preview', row.id)}
                        mimeType={row.mime_type}
                        fileName={row.original_name}
                        variant="ghost"
                        size="sm"
                        iconOnly
                    />
                    <DownloadAction href={route('files.download', row.id)} limit={row.download_limit} variant="ghost" size="sm" iconOnly />
                    {row.can_delete && (
                        <ConfirmDialog
                            trigger={
                                <Button variant="ghost" size="sm" className="text-destructive hover:text-destructive">
                                    {t('Delete')}
                                </Button>
                            }
                            title={t('Delete file?')}
                            description={t('The file ":name" will no longer be available to anyone it was shared with.', {
                                name: row.name,
                            })}
                            confirmLabel={t('Delete file')}
                            onConfirm={() => router.delete(route('files.destroy', row.id), { preserveScroll: true })}
                        />
                    )}
                </div>
            </div>
        </div>
    );
}
