import {
    Archive,
    ArchiveRestore,
    Eye,
    MoreHorizontal,
    PencilIcon,
    TrashIcon,
} from 'lucide-react';
import { useState } from 'react';
import {
    AlertDialog,
    AlertDialogAction,
    AlertDialogCancel,
    AlertDialogContent,
    AlertDialogDescription,
    AlertDialogFooter,
    AlertDialogHeader,
    AlertDialogTitle,
} from '@/components/ui/alert-dialog';
import { Button, buttonVariants } from '@/components/ui/button';
import {
    DropdownMenu,
    DropdownMenuContent,
    DropdownMenuGroup,
    DropdownMenuItem,
    DropdownMenuLabel,
    DropdownMenuSeparator,
    DropdownMenuSub,
    DropdownMenuSubContent,
    DropdownMenuSubTrigger,
    DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';

type SubMenuAction = {
    id: string;
    label: string;
    onSelect: () => void;
    variant?: 'default' | 'destructive';
};

type ResourceActionsMenuProps = {
    onView?: () => void;
    onEdit?: () => void;
    onArchive?: () => void;
    onReactivate?: () => void;
    onDelete?: () => void;
    viewLabel?: string;
    editLabel?: string;
    archiveLabel?: string;
    reactivateLabel?: string;
    deleteLabel?: string;
    deleteTitle?: string;
    deleteDescription?: string;
    confirmLabel?: string;
    disableView?: boolean;
    disableEdit?: boolean;
    disableArchive?: boolean;
    disableReactivate?: boolean;
    disableDelete?: boolean;
    subMenuLabel?: string;
    subMenuActions?: SubMenuAction[];
};

export function ResourceActionsMenu({
    onView,
    onEdit,
    onArchive,
    onReactivate,
    onDelete,
    viewLabel = 'View details',
    editLabel = 'Edit',
    archiveLabel = 'Archive',
    reactivateLabel = 'Reactivate',
    deleteLabel = 'Delete',
    deleteTitle = 'Delete item?',
    deleteDescription = 'This action cannot be undone.',
    confirmLabel = 'Delete',
    disableView = false,
    disableEdit = false,
    disableArchive = false,
    disableReactivate = false,
    disableDelete = false,
    subMenuLabel,
    subMenuActions = [],
}: ResourceActionsMenuProps) {
    const [confirmOpen, setConfirmOpen] = useState(false);

    const hasView = Boolean(onView) && !disableView;
    const hasEdit = Boolean(onEdit) && !disableEdit;
    const hasArchive = Boolean(onArchive) && !disableArchive;
    const hasReactivate = Boolean(onReactivate) && !disableReactivate;
    const hasDelete = Boolean(onDelete) && !disableDelete;
    const hasSubMenu = subMenuActions.length > 0;

    if (!hasView && !hasEdit && !hasArchive && !hasReactivate && !hasDelete && !hasSubMenu) {
        return null;
    }

    const handleDeleteConfirm = () => {
        onDelete?.();
    };

    return (
        <div className="flex justify-end">
            <DropdownMenu>
                <DropdownMenuTrigger asChild>
                    <Button
                        variant="ghost"
                        size="icon"
                        className="size-8 text-muted-foreground"
                    >
                        <MoreHorizontal />
                        <span className="sr-only">Open actions</span>
                    </Button>
                </DropdownMenuTrigger>
                <DropdownMenuContent align="end" className="w-48">
                    <DropdownMenuLabel>Actions</DropdownMenuLabel>
                    <DropdownMenuGroup>
                        {hasView ? (
                            <DropdownMenuItem onClick={onView}>
                                <Eye />
                                {viewLabel}
                            </DropdownMenuItem>
                        ) : null}

                        {hasEdit ? (
                            <DropdownMenuItem onClick={onEdit}>
                                <PencilIcon />
                                {editLabel}
                            </DropdownMenuItem>
                        ) : null}

                        {hasArchive ? (
                            <DropdownMenuItem onClick={onArchive}>
                                <Archive />
                                {archiveLabel}
                            </DropdownMenuItem>
                        ) : null}

                        {hasReactivate ? (
                            <DropdownMenuItem onClick={onReactivate}>
                                <ArchiveRestore />
                                {reactivateLabel}
                            </DropdownMenuItem>
                        ) : null}

                        {hasSubMenu ? (
                            <DropdownMenuSub>
                                <DropdownMenuSubTrigger>
                                    {subMenuLabel ?? 'More actions'}
                                </DropdownMenuSubTrigger>
                                <DropdownMenuSubContent>
                                    <DropdownMenuGroup>
                                        {subMenuActions.map((action) => (
                                            <DropdownMenuItem
                                                key={action.id}
                                                variant={
                                                    action.variant ??
                                                    'default'
                                                }
                                                onClick={action.onSelect}
                                            >
                                                {action.variant ===
                                                'destructive' ? (
                                                    <TrashIcon />
                                                ) : null}
                                                {action.label}
                                            </DropdownMenuItem>
                                        ))}
                                    </DropdownMenuGroup>
                                </DropdownMenuSubContent>
                            </DropdownMenuSub>
                        ) : null}

                        {hasDelete ? (
                            <>
                                {hasView ||
                                hasEdit ||
                                hasArchive ||
                                hasReactivate ||
                                hasSubMenu ? (
                                    <DropdownMenuSeparator />
                                ) : null}
                                <DropdownMenuItem
                                    variant="destructive"
                                    onSelect={(event) => {
                                        event.preventDefault();
                                        setConfirmOpen(true);
                                    }}
                                >
                                    <TrashIcon />
                                    {deleteLabel}
                                </DropdownMenuItem>
                            </>
                        ) : null}
                    </DropdownMenuGroup>
                </DropdownMenuContent>
            </DropdownMenu>

            {hasDelete ? (
                <AlertDialog open={confirmOpen} onOpenChange={setConfirmOpen}>
                    <AlertDialogContent>
                        <AlertDialogHeader>
                            <AlertDialogTitle>{deleteTitle}</AlertDialogTitle>
                            <AlertDialogDescription>
                                {deleteDescription}
                            </AlertDialogDescription>
                        </AlertDialogHeader>
                        <AlertDialogFooter>
                            <AlertDialogCancel>Cancel</AlertDialogCancel>
                            <AlertDialogAction
                                className={buttonVariants({
                                    variant: 'destructive',
                                })}
                                onClick={handleDeleteConfirm}
                            >
                                {confirmLabel}
                            </AlertDialogAction>
                        </AlertDialogFooter>
                    </AlertDialogContent>
                </AlertDialog>
            ) : null}
        </div>
    );
}
