
import React, { useMemo, useState } from 'react';
import { ProductForecastData, FilterState } from '../types';
import {
    BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer,
    LineChart, Line, Legend, ComposedChart, Area
} from 'recharts';

interface ForecastViewProps {
    data: ProductForecastData[];
    filters: FilterState;
    top50Ranking?: {
        eu: Map<string, number>;
        uk: Map<string, number>;
    };
}

const MONTH_ORDER = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

// Top 50 Badge Component (reused logic)
const Top50Badge: React.FC<{ rank: number; label: string; theme?: 'amber' | 'indigo' | 'blue' }> = ({ rank, label, theme = 'amber' }) => {
    const themeClasses = {
        amber: 'bg-amber-500/10 text-amber-500 border-amber-500/20',
        indigo: 'bg-indigo-500/10 text-indigo-400 border-indigo-500/20',
        blue: 'bg-blue-500/10 text-blue-400 border-blue-500/20'
    };

    return (
        <div className={`flex items-center gap-1 px-1.5 py-0.5 rounded border text-[9px] font-black uppercase tracking-tighter shadow-sm ${themeClasses[theme]}`}>
            <span className="opacity-70">{label}</span>
            <span className="text-sm">#{rank}</span>
        </div>
    );
};

const ForecastView: React.FC<ForecastViewProps> = ({ data, filters, top50Ranking }) => {
    const [searchTerm, setSearchTerm] = useState('');

    // Determine the "Current Month" based on available 2026 data
    const lastDataMonthIdx = useMemo(() => {
        let maxIdx = 0; // Default Jan
        data.forEach(p => {
            p.monthlyData.forEach((md, idx) => {
                if (md.actualUnits > 0 && idx > maxIdx) {
                    maxIdx = idx;
                }
            });
        });
        return maxIdx;
    }, [data]);

    // Active months for "Monthly Forecast" calculation
    const activeMonths = useMemo(() => {
        if (filters.month && filters.month.length > 0) {
            // Map e.g. "Apr-24" or just "Apr" to the short name
            return filters.month.map(m => m.split('-')[0]);
        }
        // If no filter, use Jan to lastDataMonth
        return MONTH_ORDER.slice(0, lastDataMonthIdx + 1);
    }, [filters.month, lastDataMonthIdx]);

    // Global Aggregate Data
    const globalMonthlyData = useMemo(() => {
        return MONTH_ORDER.map(m => {
            let forecast = 0;
            let actual = 0;
            data.forEach(p => {
                const monthPoint = p.monthlyData.find(md => md.month === m);
                if (monthPoint) {
                    forecast += monthPoint.forecastUnits;
                    actual += monthPoint.actualUnits;
                }
            });
            return { name: m, Forecast: forecast, Actual: actual };
        });
    }, [data]);

    const globalSummary = useMemo(() => {
        let totalForecast = 0;
        let totalActual = 0;
        data.forEach(p => {
            totalForecast += p.annualForecast;
            p.monthlyData.forEach(md => {
                totalActual += md.actualUnits;
            });
        });
        const fulfillment = totalForecast > 0 ? (totalActual / totalForecast) * 100 : 0;
        return { totalForecast, totalActual, fulfillment };
    }, [data]);

    const filteredProducts = useMemo(() => {
        if (!searchTerm) return data;
        const s = searchTerm.toLowerCase();
        return data.filter(p =>
            p.asin.toLowerCase().includes(s) ||
            p.sku.toLowerCase().includes(s) ||
            p.title.toLowerCase().includes(s)
        );
    }, [data, searchTerm]);

    const sortedProducts = useMemo(() => {
        return [...filteredProducts].sort((a, b) => b.annualForecast - a.annualForecast);
    }, [filteredProducts]);

    return (
        <div className="p-6 space-y-6 max-w-7xl mx-auto animate-fade-in pb-24">

            {/* Summary Cards */}
            <div className="grid grid-cols-1 md:grid-cols-3 gap-6">
                <div className="bg-slate-900 border border-border rounded-2xl p-6 shadow-xl relative overflow-hidden group">
                    <div className="absolute top-0 right-0 w-32 h-32 bg-indigo-500/10 rounded-full -mr-16 -mt-16 blur-3xl group-hover:bg-indigo-500/20 transition-all"></div>
                    <h3 className="text-xs font-black text-slate-500 uppercase tracking-widest mb-4">Total Forecast 2026</h3>
                    <div className="text-4xl font-black text-white">
                        {globalSummary.totalForecast.toLocaleString('de-DE')} <span className="text-sm font-medium text-slate-500">Units</span>
                    </div>
                </div>

                <div className="bg-slate-900 border border-border rounded-2xl p-6 shadow-xl relative overflow-hidden group">
                    <div className="absolute top-0 right-0 w-32 h-32 bg-emerald-500/10 rounded-full -mr-16 -mt-16 blur-3xl group-hover:bg-emerald-500/20 transition-all"></div>
                    <h3 className="text-xs font-black text-slate-500 uppercase tracking-widest mb-4">Total Actual Sales 2026</h3>
                    <div className="text-4xl font-black text-emerald-400">
                        {globalSummary.totalActual.toLocaleString('de-DE')} <span className="text-sm font-medium text-slate-500">Units</span>
                    </div>
                </div>

                <div className="bg-slate-900 border border-indigo-500/30 rounded-2xl p-6 shadow-xl relative overflow-hidden group">
                    <div className="absolute top-0 right-0 w-32 h-32 bg-indigo-500/20 rounded-full -mr-16 -mt-16 blur-3xl"></div>
                    <h3 className="text-xs font-black text-indigo-400 uppercase tracking-widest mb-4">Global Fulfillment</h3>
                    <div className="flex items-baseline gap-2">
                        <div className={`text-4xl font-black ${globalSummary.fulfillment >= 100 ? 'text-emerald-400' : 'text-indigo-400'}`}>
                            {globalSummary.fulfillment.toFixed(1)}%
                        </div>
                    </div>
                    {/* Fulfillment Progress Bar */}
                    <div className="mt-4 w-full h-2 bg-slate-800 rounded-full overflow-hidden">
                        <div
                            className="h-full bg-gradient-to-r from-indigo-500 to-fuchsia-500 shadow-[0_0_8px_rgba(99,102,241,0.5)] transition-all duration-1000"
                            style={{ width: `${Math.min(100, globalSummary.fulfillment)}%` }}
                        ></div>
                    </div>
                </div>
            </div>

            {/* Main Trend Chart */}
            <div className="bg-slate-900 border border-border rounded-2xl p-6 shadow-xl">
                <div className="flex justify-between items-center mb-6">
                    <h3 className="text-lg font-bold text-white flex items-center gap-2">
                        <svg className="w-5 h-5 text-indigo-400" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 7h8m0 0v8m0-8l-8 8-4-4-6 6" /></svg>
                        Monthly Evolution: Forecast vs Actual
                    </h3>
                </div>
                <div className="h-96">
                    <ResponsiveContainer width="100%" height="100%">
                        <ComposedChart data={globalMonthlyData}>
                            <CartesianGrid strokeDasharray="3 3" stroke="#1e293b" vertical={false} />
                            <XAxis dataKey="name" stroke="#64748b" />
                            <YAxis stroke="#64748b" />
                            <Tooltip
                                contentStyle={{ backgroundColor: '#0f172a', borderColor: '#1e293b', borderRadius: '12px', padding: '12px' }}
                                itemStyle={{ fontWeight: 'bold' }}
                            />
                            <Legend verticalAlign="top" height={36} />
                            <Bar dataKey="Actual" fill="#10b981" radius={[4, 4, 0, 0]} name="Actual Sales" barSize={40} />
                            <Line type="monotone" dataKey="Forecast" stroke="#6366f1" strokeWidth={4} dot={{ r: 6, fill: '#6366f1' }} name="Forecast Target" />
                            <Area type="monotone" dataKey="Forecast" fill="#6366f1" fillOpacity={0.05} stroke="none" />
                        </ComposedChart>
                    </ResponsiveContainer>
                </div>
            </div>

            {/* Product Table */}
            <div className="bg-slate-900 border border-border rounded-2xl shadow-xl overflow-hidden">
                <div className="p-6 border-b border-border flex flex-col md:flex-row justify-between gap-4">
                    <h3 className="text-lg font-bold text-white uppercase tracking-tight">Product Performance Comparison</h3>
                    <div className="relative">
                        <input
                            type="text"
                            placeholder="Search by ASIN, SKU or Title..."
                            value={searchTerm}
                            onChange={(e) => setSearchTerm(e.target.value)}
                            className="bg-slate-950 border border-slate-700 rounded-lg px-10 py-2 text-sm text-slate-200 focus:outline-none focus:border-indigo-500 w-full md:w-80"
                        />
                        <svg className="absolute left-3 top-2.5 w-4 h-4 text-slate-500" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" /></svg>
                    </div>
                </div>
                <div className="overflow-x-auto">
                    <table className="w-full text-left text-sm whitespace-nowrap">
                        <thead className="bg-slate-950 text-slate-500 uppercase text-[10px] font-black tracking-widest border-b border-border">
                            <tr>
                                <th className="px-6 py-4">Product Info</th>
                                <th className="px-6 py-4 text-right">Forecast (Period)</th>
                                <th className="px-6 py-4 text-right">Actual Units Sales 2026</th>
                                <th className="px-6 py-4 text-right">Fc 26 Achievement</th>
                                <th className="px-6 py-4 text-center">YTD Achievement</th>
                            </tr>
                        </thead>
                        <tbody className="divide-y divide-slate-800">
                            {sortedProducts.map((p) => {
                                const asin = p.asin.toUpperCase();

                                // Top 50 Badges logic
                                const ranks = [];
                                if (top50Ranking) {
                                    const rankEU = top50Ranking.eu.get(asin);
                                    const rankUK = top50Ranking.uk.get(asin);
                                    if (rankEU) ranks.push({ rank: rankEU, label: 'EU', theme: 'indigo' as const });
                                    if (rankUK) ranks.push({ rank: rankUK, label: 'UK', theme: 'blue' as const });
                                }

                                // Monthly Forecast Calculation
                                const filteredForecast = p.monthlyData
                                    .filter(md => activeMonths.includes(md.month))
                                    .reduce((acc, md) => acc + md.forecastUnits, 0);

                                // Actual Sales for 2026 (Selected Period)
                                const actualPeriod = p.monthlyData
                                    .filter(md => activeMonths.includes(md.month))
                                    .reduce((acc, md) => acc + md.actualUnits, 0);

                                // Total annual actual for achievement percentage
                                const actualTotal = p.monthlyData.reduce((acc, md) => acc + md.actualUnits, 0);
                                const totalAchievement = p.annualForecast > 0 ? (actualTotal / p.annualForecast) * 100 : 0;

                                // YTD Achievement (Achievement of the period)
                                const periodAchievement = filteredForecast > 0 ? (actualPeriod / filteredForecast) * 100 : 0;

                                return (
                                    <tr key={p.asin} className="hover:bg-indigo-500/5 transition-colors group">
                                        <td className="px-6 py-4">
                                            <div className="flex flex-col gap-1">
                                                <div className="flex items-center gap-2">
                                                    {ranks.map((r, i) => (
                                                        <Top50Badge key={i} rank={r.rank} label={r.label} theme={r.theme} />
                                                    ))}
                                                    <span className="font-black text-xs text-indigo-400 uppercase tracking-tighter">{p.sku}</span>
                                                    <span className="text-[10px] font-bold text-slate-500 bg-slate-800 px-1.5 py-0.5 rounded border border-white/5">{p.asin}</span>
                                                </div>
                                                <span className="text-[11px] text-white/70 truncate w-[240px] leading-tight" title={p.title}>{p.title}</span>
                                                <span className="text-[9px] text-fuchsia-400/80 font-bold uppercase tracking-widest">{p.line}</span>
                                            </div>
                                        </td>
                                        <td className="px-6 py-4 text-right font-mono font-bold text-slate-400">
                                            {filteredForecast.toLocaleString('de-DE')}
                                        </td>
                                        <td className="px-6 py-4 text-right font-mono font-bold text-emerald-400">
                                            {actualPeriod.toLocaleString('de-DE')}
                                        </td>
                                        <td className="px-6 py-4 text-right">
                                            <div className="flex flex-col items-end gap-1">
                                                <span className={`px-2 py-1 rounded text-xs font-black ${totalAchievement >= 50 ? 'bg-emerald-500/10 text-emerald-400' : totalAchievement >= 10 ? 'bg-indigo-500/10 text-indigo-400' : 'bg-slate-800 text-slate-500'}`}>
                                                    {totalAchievement.toFixed(1)}%
                                                </span>
                                                <span className="text-[9px] text-slate-500 font-bold uppercase">of Annual {p.annualForecast.toLocaleString('de-DE')}</span>
                                            </div>
                                        </td>
                                        <td className="px-6 py-4">
                                            <div className="flex flex-col items-center gap-2">
                                                <div className="w-32 h-2 bg-slate-800 rounded-full overflow-hidden border border-white/5">
                                                    <div
                                                        className={`h-full transition-all duration-1000 ${periodAchievement >= 100 ? 'bg-emerald-500' : periodAchievement >= 50 ? 'bg-indigo-500' : 'bg-amber-500'}`}
                                                        style={{ width: `${Math.min(100, periodAchievement)}%` }}
                                                    ></div>
                                                </div>
                                                <span className={`text-[11px] font-black ${periodAchievement >= 100 ? 'text-emerald-400' : periodAchievement >= 50 ? 'text-indigo-400' : 'text-amber-400'}`}>
                                                    {periodAchievement.toFixed(1)}%
                                                </span>
                                            </div>
                                        </td>
                                    </tr>
                                );
                            })}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    );
};

export default ForecastView;
