'use client';

import React, { useMemo } from 'react';
import {
    ModalOpcoesBase,
    type ModalOpcoesBaseProps,
} from '@/components/ModalOpcoesBase/ModalOpcoesBase';
import {
    mergeDetailPrintIntoActions,
    mergeDetailPrintIntoGroups,
    useDetailPrint,
} from '@/components/layouts/DetailPrint';
import { DetailOpcoesSidePanel } from './DetailOpcoesSidePanel';

export type DetailOpcoesPresentationProps = ModalOpcoesBaseProps & {
    /**
     * Detalhe: rail fixo em viewport larga; seta flutuante abaixo de 1600px.
     * Listagens não passam `desktopSidePanel`.
     */
    desktopSidePanel?: boolean;
    /**
     * Lado do rail em desktop.
     * @default 'right'
     */
    side?: 'left' | 'right';
    /**
     * Título da seção de `children` no rail (ex.: "Filtros").
     * String vazia omite o título. @default "Filtros"
     */
    childrenTitle?: string;
    /** Impressão da ficha (`DetailPrintSheet`). Injeta "Imprimir" no rail/modal. */
    onPrint?: () => void;
    /** @default true no rail de detalhe */
    showPrintAction?: boolean;
    printLabel?: string;
};

/**
 * Shell de opções para detalhe: rail permanente no desktop, modal no resto.
 * Preferir este wrapper (ou o mesmo padrão nos `ModalOpcoes*`) em vez de alterar
 * `ModalOpcoesBase` globalmente.
 */
export function DetailOpcoesPresentation({
    desktopSidePanel = false,
    side = 'right',
    open,
    onClose,
    title,
    subtitle,
    actions = [],
    groups,
    children,
    childrenTitle,
    width = 400,
    onPrint,
    showPrintAction = true,
    printLabel = 'Imprimir',
    'data-testid': dataTestId,
    'aria-label': ariaLabel,
    focusTriggerAfterClose,
    afterClose,
    premiumModalShell,
    loading,
}: DetailOpcoesPresentationProps) {
    const defaultPrint = useDetailPrint();
    const handlePrint = onPrint ?? defaultPrint;
    const railWidth = children || side === 'left' ? width : Math.min(width, 280);

    const modalActions = useMemo(
        () =>
            groups?.length
                ? undefined
                : mergeDetailPrintIntoActions(actions, {
                      onPrint: handlePrint,
                      label: printLabel,
                      enabled: showPrintAction,
                  }),
        [actions, groups, handlePrint, printLabel, showPrintAction],
    );

    const modalGroups = useMemo(
        () =>
            groups?.length
                ? mergeDetailPrintIntoGroups(groups, {
                      onPrint: handlePrint,
                      label: printLabel,
                      enabled: showPrintAction,
                  })
                : undefined,
        [groups, handlePrint, printLabel, showPrintAction],
    );

    if (desktopSidePanel) {
        return (
            <DetailOpcoesSidePanel
                actions={actions}
                groups={groups}
                width={railWidth}
                side={side}
                loading={loading}
                onPrint={handlePrint}
                showPrintAction={showPrintAction}
                printLabel={printLabel}
                childrenTitle={childrenTitle}
                data-testid={dataTestId}
                aria-label={ariaLabel}
            >
                {children}
            </DetailOpcoesSidePanel>
        );
    }

    return (
        <ModalOpcoesBase
            open={open}
            onClose={onClose}
            title={title}
            subtitle={subtitle}
            actions={modalActions ?? actions}
            groups={modalGroups}
            width={width}
            data-testid={dataTestId}
            aria-label={ariaLabel}
            focusTriggerAfterClose={focusTriggerAfterClose}
            afterClose={afterClose}
            premiumModalShell={premiumModalShell}
            loading={loading}
        >
            {children}
        </ModalOpcoesBase>
    );
}
