import React, { useMemo, useState, useEffect, useCallback } from 'react';
import * as XLSX from 'xlsx';
import { ProductForecastData, FilterState, CombinedKPIs } from '../types';
import { DownloadIcon, FunnelIcon, TrendingIcon, ChartIcon } from './Icons';
import { StockBadge } from './StockBadge';
import { Top50Badge } from './Top50Badge';
import { VendorStockBadge } from './VendorStockBadge';
import { InColumnStockFilter } from './InColumnStockFilter';
import { PAN_EU_COUNTRIES } from '../services/dataProcessor';
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> };
    stockMap: Map<string, number>;
    top50Mode: 'eu' | 'uk';
    vendorStockMap?: Map<string, { eu: number; uk: number }>;
    stockFilter: string[];
    onStockFilterChange: (newFilters: string[]) => void;
    vendorStockFilter: string[];
    onVendorStockFilterChange: (newFilters: string[]) => void;
}

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

const ForecastRow: React.FC<{
    item: ProductForecastData;
    activeMonths: string[];
    top50Ranking?: { eu: Map<string, number>; uk: Map<string, number> };
    top50Mode: 'eu' | 'uk';
    stockMap?: Map<string, number>;
    vendorStockMap?: Map<string, { eu: number; uk: number }>;
}> = React.memo(({ item, activeMonths, top50Ranking, top50Mode, stockMap, vendorStockMap }) => {
    const asin = item.asin.trim().toUpperCase();
    const ranks: { rank: number; label: string; theme: 'amber' | 'blue' | 'indigo' }[] = [];

    if (top50Ranking) {
        if (top50Mode === 'eu') {
            const rankEU = top50Ranking.eu.get(asin);
            if (rankEU) ranks.push({ rank: rankEU, label: 'EU', theme: 'indigo' });
        } else {
            const rankUK = top50Ranking.uk.get(asin);
            if (rankUK) ranks.push({ rank: rankUK, label: 'UK', theme: 'blue' });
        }
    }

    // Calculations for new columns
    const forecastPeriod = item.forecastUnits || 0;
    const actualUnits = item.actualUnits || 0;
    const annualForecast = item.annualForecast || 0;

    // FC 26 Achievement: Actual / Annual Forecast
    const fcAchievement = annualForecast > 0 ? (actualUnits / annualForecast) * 100 : 0;

    // YTD Achievement: Actual / Period Forecast
    // If activeMonths is basically "Year To Date" (which it usually is in this view unless filtered), this logic holds.
    // If user selects specific months, it becomes "Period Achievement".
    const ytdAchievement = forecastPeriod > 0 ? (actualUnits / forecastPeriod) * 100 : 0;

    let achievementColor = 'bg-slate-700';
    if (ytdAchievement >= 100) achievementColor = 'bg-emerald-500';
    else if (ytdAchievement >= 80) achievementColor = 'bg-emerald-400';
    else if (ytdAchievement >= 50) achievementColor = 'bg-amber-400';
    else achievementColor = 'bg-rose-500';

    return (
        <tr key={item.asin} className="hover:bg-indigo-500/5 transition-colors group border-b border-slate-800/50 last:border-0">
            {/* Product Info */}
            <td className="px-6 py-4">
                <div className="flex flex-col gap-1">
                    <div className="flex items-center gap-2 mb-1">
                        {ranks.map((r, i) => (
                            <Top50Badge key={i} rank={r.rank} label={r.label} theme={r.theme} />
                        ))}
                        <span className="text-xs font-black text-indigo-400 tracking-tighter uppercase">
                            {item.sku}
                        </span>
                        <div className="px-1.5 py-0.5 rounded bg-slate-800 border border-slate-700 text-[9px] font-bold text-slate-400 uppercase tracking-wider">
                            {item.asin}
                        </div>
                    </div>
                    <span className="text-sm font-medium text-slate-200 line-clamp-1 group-hover:line-clamp-none transition-all" title={item.title}>
                        {item.title}
                    </span>
                    <div className="flex items-center gap-2 mt-1">
                        <span className="px-2 py-0.5 rounded bg-purple-500/10 text-[9px] font-black text-purple-300 uppercase tracking-widest border border-purple-500/20">
                            {item.line}
                        </span>
                        {/* WOC Indicator preserved */}
                        <VendorStockBadge asin={item.asin} vendorStockMap={vendorStockMap} mode={top50Mode} avgWeeklySales={item.avgWeeklySales} />
                    </div>
                </div>
            </td>
            {/* Forecast (Period) */}
            <td className="px-6 py-4 text-center">
                <span className="text-sm font-bold text-slate-400">
                    {forecastPeriod.toLocaleString('de-DE')}
                </span>
            </td>

            {/* Actual Units Sales 2026 */}
            <td className="px-6 py-4 text-center">
                <span className="text-base font-black text-emerald-400">
                    {actualUnits.toLocaleString('de-DE')}
                </span>
            </td>

            {/* FC 26 Achievement (Annual %) */}
            <td className="px-6 py-4 text-center">
                <div className="flex flex-col items-center justify-center gap-1">
                    <div className="px-3 py-1 rounded bg-slate-800 border border-slate-700 text-xs font-bold text-white shadow-sm">
                        {fcAchievement.toFixed(1)}%
                    </div>
                    <span className="text-[9px] font-bold text-slate-500 uppercase tracking-tight">
                        OF ANNUAL {Math.round(annualForecast / 1000)}K
                    </span>
                </div>
            </td>

            {/* YTD Achievement (Period %) */}
            <td className="px-6 py-4 align-middle">
                <div className="flex flex-col gap-1 w-full max-w-[200px] mx-auto">
                    <div className="flex justify-between items-end mb-1">
                        <span className={`text-xs font-black ${ytdAchievement >= 100 ? 'text-emerald-400' : 'text-slate-300'}`}>
                            {ytdAchievement.toFixed(1)}%
                        </span>
                    </div>
                    <div className="h-2 w-full bg-slate-800 rounded-full overflow-hidden">
                        <div
                            className={`h-full rounded-full transition-all duration-500 ${achievementColor}`}
                            style={{ width: `${Math.min(100, ytdAchievement)}%` }}
                        />
                    </div>
                </div>
            </td>
        </tr>
    );
});

const ForecastView: React.FC<ForecastViewProps> = ({
    data,
    filters,
    top50Ranking,
    stockMap,
    vendorStockMap,
    stockFilter,
    onStockFilterChange,
    vendorStockFilter,
    onVendorStockFilterChange,
    top50Mode
}) => {
    const [searchTerm, setSearchTerm] = useState('');
    const [showOnlyTop50, setShowOnlyTop50] = useState(false);
    const [displayCount, setDisplayCount] = useState(50);

    const lastDataMonthIdx = useMemo(() => {
        for (let i = MONTH_ORDER.length - 1; i >= 0; i--) {
            if (data.some(p => p.monthlyData[MONTH_ORDER[i]]?.units > 0)) return i;
        }
        return 0;
    }, [data]);

    const activeMonths = useMemo(() => {
        if (filters.month && filters.month.length > 0) {
            return filters.month.map(m => m.split('-')[0]);
        }
        return MONTH_ORDER.slice(0, lastDataMonthIdx + 1);
    }, [filters.month, lastDataMonthIdx]);

    const baseFilteredData = useMemo(() => {
        let result = data;
        if (filters.line && filters.line.length > 0) result = result.filter(p => filters.line.includes(p.line));
        if (filters.asin && filters.asin.length > 0) result = result.filter(p => filters.asin.includes(p.asin));
        if (filters.sku && filters.sku.length > 0) result = result.filter(p => filters.sku.includes(p.sku));
        return result;
    }, [data, filters.line, filters.asin, filters.sku]);

    const globalSummary = useMemo(() => {
        return baseFilteredData.reduce((acc, curr) => ({
            actualUnits: acc.actualUnits + curr.actualUnits,
            forecastUnits: acc.forecastUnits + curr.forecastUnits,
        }), { actualUnits: 0, forecastUnits: 0 });
    }, [baseFilteredData]);

    const finalFilteredData = useMemo(() => {
        let result = baseFilteredData;
        if (showOnlyTop50 && top50Ranking) {
            const currentRankMap = top50Mode === 'eu' ? top50Ranking.eu : top50Ranking.uk;
            result = result.filter(p => currentRankMap.has(p.asin.toUpperCase()));
        }
        if (searchTerm) {
            const s = searchTerm.toLowerCase();
            result = result.filter(p => p.sku.toLowerCase().includes(s) || p.asin.toLowerCase().includes(s) || p.title.toLowerCase().includes(s));
        }
        return result;
    }, [baseFilteredData, searchTerm, showOnlyTop50, top50Ranking, top50Mode]);

    const handleExportExcel = useCallback(() => {
        const exportData = finalFilteredData.map(p => ({
            SKU: p.sku,
            ASIN: p.asin,
            Title: p.title,
            Line: p.line,
            'Actual Units (2025)': p.actualUnits,
            'Forecast Units (2025)': p.forecastUnits,
            'Accuracy (%)': p.accuracy
        }));
        const ws = XLSX.utils.json_to_sheet(exportData);
        const wb = XLSX.utils.book_new();
        XLSX.utils.book_append_sheet(wb, ws, 'Forecast');
        XLSX.writeFile(wb, `Forecast_Export_${new Date().toISOString().slice(0, 10)}.xlsx`);
    }, [finalFilteredData]);

    return (
        <div className="flex-1 overflow-hidden flex flex-col min-h-0 bg-slate-950/50">
            <div className="flex-1 overflow-auto min-h-0 custom-scrollbar relative">
                <table className="w-full text-left border-collapse relative">
                    <thead className="sticky top-0 z-20 bg-slate-950 shadow-sm text-xs font-bold text-slate-400 uppercase tracking-wider">
                        <tr>
                            <th className="px-6 py-4 font-black text-white w-[35%]">Product Info</th>
                            <th className="px-6 py-4 text-center w-[12%]">Forecast (Period)</th>
                            <th className="px-6 py-4 text-center w-[12%]">Actual Units Sales 2026</th>
                            <th className="px-6 py-4 text-center w-[15%]">FC 26 Achievement</th>
                            <th className="px-6 py-4 text-center w-[26%]">YTD Achievement</th>
                        </tr>
                    </thead>
                    <tbody className="divide-y divide-slate-800/50">
                        {finalFilteredData.map(item => (
                            <ForecastRow
                                key={item.asin}
                                item={item}
                                activeMonths={filters.month.length > 0 ? filters.month : MONTH_ORDER}
                                top50Ranking={top50Ranking}
                                top50Mode={top50Mode}
                                stockMap={stockMap}
                                vendorStockMap={vendorStockMap}
                            />
                        ))}
                    </tbody>
                </table>

                {finalFilteredData.length === 0 && (
                    <div className="flex flex-col items-center justify-center py-20 text-slate-500">
                        <p className="text-lg">No forecast data found for current filters</p>
                    </div>
                )}
            </div>
        </div>
    );
};

export default ForecastView;
