'use client' import { useState } from 'react' import { useTranslations } from 'next-intl' import Box from '@mui/material/Box' import Card from '@mui/material/Card' import CardContent from '@mui/material/CardContent' import Typography from '@mui/material/Typography' import Button from '@mui/material/Button' import Collapse from '@mui/material/Collapse' // Types interface FarmData { soilType: string organicMatter: string waterEC: string } interface GrowthStage { id: string icon: string } interface CropOption { id: string labelKey: string icon: string } interface FertilizationPlan { npkRatio: string amountPerHectare: string applicationMethod: string applicationInterval: string reasoning: string } // Mock farm data (from stored soil/water data - no inputs) const DEFAULT_FARM_DATA: FarmData = { soilType: 'Loamy', organicMatter: 'Medium (2.5%)', waterEC: '1.2 dS/m' } const GROWTH_STAGES: GrowthStage[] = [ { id: 'prePlanting', icon: 'tabler-seedling' }, { id: 'earlyGrowth', icon: 'tabler-leaf' }, { id: 'flowering', icon: 'tabler-flower' }, { id: 'fruiting', icon: 'tabler-apple' }, { id: 'postHarvest', icon: 'tabler-basket' } ] const CROP_OPTIONS: CropOption[] = [ { id: 'wheat', labelKey: 'wheat', icon: 'tabler-wheat' }, { id: 'corn', labelKey: 'corn', icon: 'tabler-plant-2' }, { id: 'cotton', labelKey: 'cotton', icon: 'tabler-flower' }, { id: 'saffron', labelKey: 'saffron', icon: 'tabler-flower-2' }, { id: 'canola', labelKey: 'canola', icon: 'tabler-leaf' }, { id: 'vegetables', labelKey: 'vegetables', icon: 'tabler-carrot' } ] // Mock plan generator (replace with API in production) function generateFertilizationPlan( _cropId: string, _growthStageId: string, _farmData: FarmData ): FertilizationPlan { return { npkRatio: '20-20-20 (NPK)', amountPerHectare: '150 kg/ha', applicationMethod: 'Foliar spray + soil broadcast', applicationInterval: 'Every 14 days', reasoning: 'Your loamy soil with medium organic matter (2.5%) provides good nutrient retention. Water EC of 1.2 dS/m indicates low salinity—suitable for most crops. At the flowering stage, increased phosphorus supports bloom development. We recommend a balanced NPK to maintain nitrogen for vegetative growth while boosting phosphorous for flowering.' } } export default function SmartFertilizationRecommendation() { const t = useTranslations('fertilization') const [farmData] = useState(DEFAULT_FARM_DATA) const [growthStage, setGrowthStage] = useState(GROWTH_STAGES[0].id) const [selectedCrop, setSelectedCrop] = useState(null) const [plan, setPlan] = useState(null) const [loading, setLoading] = useState(false) const [reasoningExpanded, setReasoningExpanded] = useState(false) const handleGenerate = () => { if (!selectedCrop) return setLoading(true) setPlan(null) setReasoningExpanded(false) setTimeout(() => { setPlan( generateFertilizationPlan(selectedCrop, growthStage, farmData) ) setLoading(false) }, 1400) } const stageIndex = GROWTH_STAGES.findIndex(s => s.id === growthStage) return ( {/* 1) Header */} {t('title')} {t('subtitle')} {/* 2) Farm Data Card */} {t('farmData.title')} {t('verifiedBadge')} {/* 3) Growth Stage Selector */} {t('growthStage.title')} {GROWTH_STAGES.map((stage, idx) => { const isSelected = growthStage === stage.id const isPast = idx < stageIndex return ( setGrowthStage(stage.id)} className='flex flex-col items-center gap-1.5 px-4 py-3 rounded-2xl shrink-0 transition-all duration-300 ease-out border-2 cursor-pointer min-w-[72px]' sx={{ borderColor: isSelected ? '#22c55e' : 'transparent', background: isSelected ? 'linear-gradient(145deg, rgba(34, 197, 94, 0.15) 0%, rgba(124, 58, 237, 0.06) 100%)' : 'linear-gradient(145deg, #ffffff 0%, #faf5ff 100%)', boxShadow: isSelected ? '0 4px 20px rgba(34, 197, 94, 0.2), inset 0 1px 0 rgba(255,255,255,0.8)' : '0 2px 8px rgba(0,0,0,0.04)', '&:hover': { transform: 'translateY(-2px)', boxShadow: isSelected ? '0 6px 24px rgba(34, 197, 94, 0.25)' : '0 4px 16px rgba(124, 58, 237, 0.1)' } }} > {t(`growthStage.${stage.id}`)} ) })} {/* 4) Plant Selection */} {t('plantSelection.title')} {CROP_OPTIONS.map(crop => ( setSelectedCrop(prev => (prev === crop.id ? null : crop.id)) } /> ))} {/* 6) Result Section - Prescription style */} {plan && ( {t('result.title')} {/* Expandable "Why this recommendation?" */} setReasoningExpanded(!reasoningExpanded)} className='w-full flex items-center justify-between px-4 py-3 text-start cursor-pointer' sx={{ '&:hover': { bgcolor: 'rgba(34, 197, 94, 0.06)' } }} > {t('result.whyRecommendation')} {plan.reasoning} )} {/* Loading state */} {loading && ( {t('generating')} )} {/* 5) Primary CTA Button - Sticky */} ) } // ─── Sub-components ────────────────────────────────────────────────────────── function FarmBadge({ icon, label, value }: { icon: string label: string value: string }) { return ( {label} {value} ) } function CropCard({ crop, label, selected, onClick }: { crop: CropOption label: string selected: boolean onClick: () => void }) { return ( {label} {selected && ( )} ) } function PrescriptionRow({ icon, label, value }: { icon: string label: string value: string }) { return ( {label} {value} ) }