import React, { useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import type { SetupStepProps } from '../SetupWizard'; import { useSuppliers } from '../../../../api/hooks/suppliers'; import { useIngredients } from '../../../../api/hooks/inventory'; import { useRecipes } from '../../../../api/hooks/recipes'; import { useQualityTemplates } from '../../../../api/hooks/qualityTemplates'; import { useCurrentTenant } from '../../../../stores/tenant.store'; import { useAuthUser } from '../../../../stores/auth.store'; export const ReviewSetupStep: React.FC = ({ onUpdate, onComplete, canContinue }) => { const { t } = useTranslation(); // Get tenant ID const currentTenant = useCurrentTenant(); const user = useAuthUser(); const tenantId = currentTenant?.id || user?.tenant_id || ''; // Fetch all data for review const { data: suppliersData, isLoading: suppliersLoading } = useSuppliers(tenantId); const { data: ingredientsData, isLoading: ingredientsLoading } = useIngredients(tenantId); const { data: recipesData, isLoading: recipesLoading } = useRecipes(tenantId); const { data: qualityTemplatesData, isLoading: qualityLoading } = useQualityTemplates(tenantId); const suppliers = suppliersData || []; const ingredients = ingredientsData || []; const recipes = recipesData || []; const qualityTemplates = qualityTemplatesData || []; const isLoading = suppliersLoading || ingredientsLoading || recipesLoading || qualityLoading; // Always allow to continue (review step is informational) useEffect(() => { onUpdate?.({ itemsCount: suppliers.length + ingredients.length + recipes.length, canContinue: true, }); }, [suppliers.length, ingredients.length, recipes.length, onUpdate]); // Calculate some helpful stats const totalCost = ingredients.reduce((sum, ing) => sum + (ing.standard_cost || 0), 0); const avgRecipeIngredients = recipes.length > 0 ? recipes.reduce((sum, recipe) => sum + (recipe.ingredients?.length || 0), 0) / recipes.length : 0; return (
{/* Header */}

{t('setup_wizard:review.title', 'Review Your Setup')}

{t('setup_wizard:review.subtitle', "Let's review everything you've configured. You can go back and make changes if needed.")}

{isLoading ? (

{t('common:loading', 'Loading...')}

) : ( <> {/* Overview Stats */}

{t('setup_wizard:review.suppliers', 'Suppliers')}

{suppliers.length}

{t('setup_wizard:review.ingredients', 'Ingredients')}

{ingredients.length}

{t('setup_wizard:review.recipes', 'Recipes')}

{recipes.length}

{t('setup_wizard:review.quality', 'Quality Checks')}

{qualityTemplates.length}

{/* Detailed Sections */}
{/* Suppliers Section */} {suppliers.length > 0 && (

{t('setup_wizard:review.suppliers_title', 'Suppliers')} ({suppliers.length})

{suppliers.slice(0, 6).map((supplier) => (

{supplier.name}

{supplier.email && (

{supplier.email}

)}
{supplier.is_active && ( )}
))} {suppliers.length > 6 && (
+{suppliers.length - 6} {t('setup_wizard:review.more', 'more')}
)}
)} {/* Ingredients Section */} {ingredients.length > 0 && (

{t('setup_wizard:review.ingredients_title', 'Inventory Items')} ({ingredients.length})

{totalCost > 0 && (

{t('setup_wizard:review.total_cost', 'Total value')}: ${totalCost.toFixed(2)}

)}
{ingredients.slice(0, 8).map((ingredient) => (

{ingredient.name}

{ingredient.unit_of_measure}

))} {ingredients.length > 8 && (
+{ingredients.length - 8} {t('setup_wizard:review.more', 'more')}
)}
)} {/* Recipes Section */} {recipes.length > 0 && (

{t('setup_wizard:review.recipes_title', 'Recipes')} ({recipes.length})

{avgRecipeIngredients > 0 && (

{t('setup_wizard:review.avg_ingredients', 'Avg ingredients')}: {avgRecipeIngredients.toFixed(1)}

)}
{recipes.slice(0, 4).map((recipe) => (

{recipe.name}

{recipe.ingredients?.length || 0} {t('setup_wizard:review.ingredients', 'ingredients')} {t('setup_wizard:review.yields', 'Yields')}: {recipe.yield_quantity} {recipe.yield_unit} {recipe.category && ( {recipe.category} )}
{recipe.estimated_cost_per_unit && (

{t('setup_wizard:review.cost', 'Cost')}

${Number(recipe.estimated_cost_per_unit).toFixed(2)}

)}
))} {recipes.length > 4 && (
+{recipes.length - 4} {t('setup_wizard:review.more', 'more')}
)}
)} {/* Quality Templates Section */} {qualityTemplates.length > 0 && (

{t('setup_wizard:review.quality_title', 'Quality Check Templates')} ({qualityTemplates.length})

{qualityTemplates.map((template) => (

{template.name}

{template.check_type}

{template.is_required && ( {t('setup_wizard:review.required', 'Required')} )}
))}
)}
{/* Summary Message */}

{t('setup_wizard:review.ready_title', 'Your Bakery is Ready to Go!')}

{t('setup_wizard:review.ready_message', "You've successfully configured {{suppliers}} suppliers, {{ingredients}} ingredients, and {{recipes}} recipes. Click 'Complete Setup' to finish and start using the system.", { suppliers: suppliers.length, ingredients: ingredients.length, recipes: recipes.length } )}

{/* Help Text */}

{t('setup_wizard:review.help', 'Need to make changes? Use the "Back" button to return to any step.')}

)} {/* Continue button - only shown when used in onboarding context */} {onComplete && (
)}
); };