'use client';

import { Check, Eye, Pencil, Send, Trash2, X } from 'lucide-react';
import { ICON_SIZE_MD } from '@/components/icons';
import React from 'react';
import { ModalOpcoesBase, type ModalOpcaoAction } from '@/components/ModalOpcoesBase';
import type { EntregaType } from './types';

const STATUS_NAO_PERMITE_ENTREGAR = new Set(['entregue', 'aceita', 'rejeitada', 'cancelada']);

export interface ModalOpcoesEntregaProps {
    open: boolean;
    onClose: () => void;
    item: EntregaType | null;
    onVer: (item: EntregaType) => void;
    onEditar?: (item: EntregaType) => void;
    onExcluir?: (item: EntregaType) => void;
    onEntregar?: (item: EntregaType) => void;
    onAceitar?: (item: EntregaType) => void;
    onRejeitar?: (item: EntregaType) => void;
}

export function ModalOpcoesEntrega({
    open,
    onClose,
    item,
    onVer,
    onEditar,
    onExcluir,
    onEntregar,
    onAceitar,
    onRejeitar,
}: ModalOpcoesEntregaProps) {
    if (!item) return null;
    const subtitle = [item.codigo, item.titulo, item.status].filter(Boolean).join(' · ');
    const actions: ModalOpcaoAction[] = [
        {
            key: 'ver',
            label: 'Ver detalhes',
            icon: <Eye size={ICON_SIZE_MD} aria-hidden />,
            onClick: () => {
                onClose();
                onVer(item);
            },
            primary: true,
        },
    ];
    if (onEditar) {
        actions.push({
            key: 'editar',
            label: 'Editar',
            icon: <Pencil size={ICON_SIZE_MD} aria-hidden />,
            onClick: () => {
                onClose();
                onEditar(item);
            },
        });
    }
    if (onEntregar && !STATUS_NAO_PERMITE_ENTREGAR.has(item.status)) {
        actions.push({
            key: 'entregar',
            label: 'Marcar como entregue',
            icon: <Send size={ICON_SIZE_MD} aria-hidden />,
            onClick: () => {
                onClose();
                onEntregar(item);
            },
        });
    }
    if (onAceitar && item.status === 'entregue') {
        actions.push({
            key: 'aceitar',
            label: 'Aceitar',
            icon: <Check size={ICON_SIZE_MD} aria-hidden />,
            onClick: () => {
                onClose();
                onAceitar(item);
            },
        });
    }
    if (onRejeitar && item.status === 'entregue') {
        actions.push({
            key: 'rejeitar',
            label: 'Rejeitar',
            icon: <X size={ICON_SIZE_MD} aria-hidden />,
            onClick: () => {
                onClose();
                onRejeitar(item);
            },
            danger: true,
        });
    }
    if (onExcluir) {
        actions.push({
            key: 'excluir',
            label: 'Excluir',
            icon: <Trash2 size={ICON_SIZE_MD} aria-hidden />,
            onClick: () => {
                onClose();
                onExcluir(item);
            },
            danger: true,
        });
    }
    return (
        <ModalOpcoesBase
            open={open}
            onClose={onClose}
            title="Opções da entrega"
            subtitle={subtitle}
            actions={actions}
            width={380}
            data-testid="projeto-entrega-opcoes-modal"
            aria-label="Opções da entrega"
        />
    );
}
