import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Package, Salad, AlertCircle, ArrowRight, ArrowLeft, CheckCircle } from 'lucide-react'; import Button from '../../../ui/Button/Button'; import Card from '../../../ui/Card/Card'; import Input from '../../../ui/Input/Input'; export interface ProductWithStock { id: string; name: string; type: 'ingredient' | 'finished_product'; category?: string; unit?: string; initialStock?: number; } export interface InitialStockEntryStepProps { products?: ProductWithStock[]; // Made optional - will use empty array if not provided onUpdate?: (data: { productsWithStock: ProductWithStock[] }) => void; onComplete?: () => void; onPrevious?: () => void; initialData?: { productsWithStock?: ProductWithStock[]; }; } export const InitialStockEntryStep: React.FC = ({ products: initialProducts, onUpdate, onComplete, onPrevious, initialData, }) => { const { t } = useTranslation(); const [products, setProducts] = useState(() => { if (initialData?.productsWithStock) { return initialData.productsWithStock; } // Handle case where initialProducts is undefined (shouldn't happen, but defensive) if (!initialProducts || initialProducts.length === 0) { return []; } return initialProducts.map(p => ({ ...p, initialStock: p.initialStock ?? undefined, })); }); const ingredients = products.filter(p => p.type === 'ingredient'); const finishedProducts = products.filter(p => p.type === 'finished_product'); const handleStockChange = (productId: string, value: string) => { const numValue = value === '' ? undefined : parseFloat(value); const updatedProducts = products.map(p => p.id === productId ? { ...p, initialStock: numValue } : p ); setProducts(updatedProducts); onUpdate?.({ productsWithStock: updatedProducts }); }; const handleSetAllToZero = () => { const updatedProducts = products.map(p => ({ ...p, initialStock: 0 })); setProducts(updatedProducts); onUpdate?.({ productsWithStock: updatedProducts }); }; const handleSkipForNow = () => { // Set all undefined values to 0 const updatedProducts = products.map(p => ({ ...p, initialStock: p.initialStock ?? 0, })); setProducts(updatedProducts); onUpdate?.({ productsWithStock: updatedProducts }); onComplete?.(); }; const handleContinue = () => { onComplete?.(); }; const productsWithStock = products.filter(p => p.initialStock !== undefined && p.initialStock >= 0); const productsWithoutStock = products.filter(p => p.initialStock === undefined); const completionPercentage = products.length > 0 ? (productsWithStock.length / products.length) * 100 : 100; const allCompleted = productsWithoutStock.length === 0; // If no products, show a skip message if (products.length === 0) { return (

{t('onboarding:stock.no_products_title', 'Stock Inicial')}

{t('onboarding:stock.no_products_message', 'Podrás configurar los niveles de stock más tarde en la sección de inventario.')}

); } return (
{/* Header */}

{t('onboarding:stock.title', 'Niveles de Stock Inicial')}

{t( 'onboarding:stock.subtitle', 'Ingresa las cantidades actuales de cada producto. Esto permite que el sistema rastree el inventario desde hoy.' )}

{/* Info Banner */}

{t('onboarding:stock.info_title', '¿Por qué es importante?')}

{t( 'onboarding:stock.info_text', 'Sin niveles de stock iniciales, el sistema no puede alertarte sobre stock bajo, planificar producción o calcular costos correctamente. Tómate un momento para ingresar tus cantidades actuales.' )}

{/* Progress */}
{t('onboarding:stock.progress', 'Progreso de captura')} {productsWithStock.length} / {products.length}
{/* Quick Actions */}
{/* Ingredients Section */} {ingredients.length > 0 && (

{t('onboarding:stock.ingredients', 'Ingredientes')} ({ingredients.length})

{ingredients.map(product => { const hasStock = product.initialStock !== undefined; return (
{product.name} {hasStock && }
{product.category && (
{product.category}
)}
handleStockChange(product.id, e.target.value)} placeholder="0" min="0" step="0.01" className="w-20 sm:w-24 text-right min-h-[44px]" /> {product.unit || 'kg'}
); })}
)} {/* Finished Products Section */} {finishedProducts.length > 0 && (

{t('onboarding:stock.finished_products', 'Productos Terminados')} ({finishedProducts.length})

{finishedProducts.map(product => { const hasStock = product.initialStock !== undefined; return (
{product.name} {hasStock && }
{product.category && (
{product.category}
)}
handleStockChange(product.id, e.target.value)} placeholder="0" min="0" step="1" className="w-24 text-right" /> {product.unit || t('common:units', 'unidades')}
); })}
)} {/* Warning for incomplete */} {!allCompleted && (

{t('onboarding:stock.incomplete_warning', 'Faltan {count} productos por completar', { count: productsWithoutStock.length, })}

{t( 'onboarding:stock.incomplete_help', 'Puedes continuar, pero recomendamos ingresar todas las cantidades para un mejor control de inventario.' )}

)} {/* Footer Actions */}
); }; export default InitialStockEntryStep;