'use client';

import type { ReactNode } from 'react';
import ProjetoLayout from '@/components/layouts/ProjetoLayout/ProjetoLayout';
import {
    useSolicitacoesChrome,
    useSolicitacoesShellActive,
} from '@/features/projetos/feira-solicitacoes/SolicitacoesChromeContext';
import { useFeiraListResponsiveToolbar } from './useFeiraListResponsiveToolbar';

type FeiraListBreadcrumbItem = { title: string; path?: string };

type FeiraListScreenLayoutProps = {
    projetoId: string;
    pageTitle: string;
    breadcrumbItems: FeiraListBreadcrumbItem[];
    showPageTitleIcon?: boolean;
    screenTestId: string;
    filtroSalvoAtivo: { nome: string } | null;
    filtroSalvoDataTestId: string;
    filtrosAtivosCount: number;
    filtrosModalOpen: boolean;
    onOpenFiltros: () => void;
    filterButtonTestId: string;
    filterTooltipActive: string;
    filterTooltipEmpty: string;
    filterAriaLabel?: string;
    /** Ações à esquerda do toolbar (ex.: busca avançada). */
    leadingExtra?: ReactNode;
    /** Botão "Opções" do header (à esquerda de Filtrar). */
    optionsButton?: ReactNode;
    exportButton: ReactNode;
    children: ReactNode;
};

/** `ProjetoLayout` partilhado pelas listagens operacionais da feira. */
export function FeiraListScreenLayout({
    projetoId,
    pageTitle,
    breadcrumbItems,
    showPageTitleIcon = false,
    screenTestId,
    filtroSalvoAtivo,
    filtroSalvoDataTestId,
    filtrosAtivosCount,
    filtrosModalOpen,
    onOpenFiltros,
    filterButtonTestId,
    filterTooltipActive,
    filterTooltipEmpty,
    filterAriaLabel,
    leadingExtra,
    optionsButton,
    exportButton,
    children,
}: FeiraListScreenLayoutProps) {
    const inSolicitacoesShell = useSolicitacoesShellActive();

    const { headerAction, mobileActionBar } = useFeiraListResponsiveToolbar({
        filtroSalvoAtivo,
        filtroSalvoDataTestId,
        filtrosAtivosCount,
        filtrosModalOpen,
        onOpenFiltros,
        filterButtonTestId,
        filterTooltipActive,
        filterTooltipEmpty,
        filterAriaLabel,
        leadingExtra,
        optionsButton,
        exportButton,
    });

    useSolicitacoesChrome({
        pageTitle,
        headerAction,
    });

    if (inSolicitacoesShell) {
        return (
            <>
                <div data-testid={screenTestId}>{children}</div>
                {mobileActionBar}
            </>
        );
    }

    return (
        <ProjetoLayout
            projetoId={projetoId}
            pageTitle={pageTitle}
            showPageTitleIcon={showPageTitleIcon}
            breadcrumbItems={breadcrumbItems}
            headerAction={headerAction}
        >
            <div data-testid={screenTestId}>{children}</div>
            {mobileActionBar}
        </ProjetoLayout>
    );
}
