import { useState, useEffect } from 'react';
import { useParams, useNavigate, Link } from 'react-router-dom';
import {
    ArrowLeft, Calendar, Droplets, Thermometer, Wind, Zap,
    Bug, Camera, Activity, TrendingUp, Clock, CheckCircle2,
    AlertTriangle, Sprout, Sun, History, ClipboardList,
    ShieldCheck, BarChart3, MoreHorizontal, Download, Plus,
    Trash2, ExternalLink
} from 'lucide-react';
import { batchesApi, Batch } from '../lib/batchesApi';
import { Card } from '../components/ui/card';
import { cn } from '../lib/utils';
import { motion, AnimatePresence } from 'framer-motion';

// Mock sensor data for Sparklines
function generateMockData(count: number, min: number, max: number) {
    return Array.from({ length: count }, (_, i) => ({
        value: min + Math.random() * (max - min),
    }));
}

export default function BatchDetailPage() {
    const { id } = useParams<{ id: string }>();
    const navigate = useNavigate();
    const [batch, setBatch] = useState<Batch | null>(null);
    const [loading, setLoading] = useState(true);
    const [activeTab, setActiveTab] = useState<'overview' | 'environment' | 'tasks' | 'compliance'>('overview');

    useEffect(() => {
        if (id) {
            setLoading(true);
            batchesApi.getById(id)
                .then(setBatch)
                .catch((err) => {
                    console.error('Failed to load batch:', err);
                    navigate('/batches');
                })
                .finally(() => setLoading(false));
        }
    }, [id, navigate]);

    if (loading) {
        return (
            <div className="flex items-center justify-center min-h-[50vh]">
                <div className="animate-spin w-8 h-8 border-4 border-emerald-500 border-t-transparent rounded-full" />
            </div>
        );
    }

    if (!batch) return null;

    const startDate = new Date(batch.startDate);
    const daysInCycle = Math.floor((Date.now() - startDate.getTime()) / (1000 * 60 * 60 * 24));

    return (
        <div className="space-y-8 pb-20 max-w-[1600px] mx-auto">
            {/* Context Header */}
            <div className="flex flex-col md:flex-row justify-between items-start md:items-end gap-6 bg-[var(--color-bg-tertiary)]/50 p-6 rounded-2xl border border-[var(--color-border-subtle)]">
                <div className="space-y-4 flex-1">
                    <button
                        onClick={() => navigate('/batches')}
                        className="flex items-center gap-2 text-xs font-bold text-[var(--color-text-tertiary)] hover:text-[var(--color-primary)] uppercase tracking-widest transition-colors"
                    >
                        <ArrowLeft size={14} /> Back to Inventory
                    </button>

                    <div>
                        <div className="flex items-center gap-3">
                            <h1 className="text-4xl font-black tracking-tight text-[var(--color-text-primary)] uppercase italic">
                                {batch.name}
                            </h1>
                            <span className={cn(
                                "px-3 py-1 rounded-full text-[10px] font-black uppercase tracking-[0.2em] border shadow-sm",
                                batch.stage === 'FLOWERING'
                                    ? "bg-purple-500/10 border-purple-500/20 text-purple-500"
                                    : "bg-[var(--color-primary)]/10 border-emerald-500/20 text-[var(--color-primary)]"
                            )}>
                                {batch.stage}
                            </span>
                        </div>
                        <div className="flex items-center gap-6 mt-4 text-xs font-bold uppercase tracking-widest text-[var(--color-text-tertiary)]">
                            <div className="flex items-center gap-2">
                                <Sprout size={14} className="text-[var(--color-primary)]" />
                                <span>{batch.strain}</span>
                            </div>
                            <div className="flex items-center gap-2">
                                <Layers size={14} className="text-[var(--color-text-tertiary)]" />
                                <span>{batch.plantCount} Plants</span>
                            </div>
                            <div className="flex items-center gap-2">
                                <Calendar size={14} className="text-[var(--color-text-tertiary)]" />
                                <span>Cycle Day {daysInCycle}</span>
                            </div>
                            <div className="flex items-center gap-2">
                                <ShieldCheck size={14} className="text-[var(--color-accent)]" />
                                <span className="text-[var(--color-accent)]">METRC SYNCED</span>
                            </div>
                        </div>
                    </div>
                </div>

                <div className="flex gap-2">
                    <button className="p-2.5 rounded-xl border border-[var(--color-border-subtle)] hover:bg-slate-100 dark:hover:bg-slate-800 transition-all text-[var(--color-text-tertiary)]">
                        <Download size={18} />
                    </button>
                    <button className="flex items-center gap-2 bg-[var(--color-primary)] hover:bg-[var(--color-primary-hover)] text-white px-6 py-2.5 rounded-xl font-bold text-sm shadow-xl shadow-emerald-500/20 transition-all uppercase tracking-widest">
                        <Plus size={18} /> Batch Event
                    </button>
                </div>
            </div>

            {/* KPI Strip */}
            <div className="grid grid-cols-2 lg:grid-cols-4 gap-4">
                <KPICard label="Current Health" value="98%" trend="+2%" status="success" icon={Activity} />
                <KPICard label="Expected Yield" value="4.2kg" trend="On Target" status="neutral" icon={BarChart3} />
                <KPICard label="Room Temp Avg" value="76.4°F" trend="Optimal" status="success" icon={Thermometer} />
                <KPICard label="VPD Focus" value="1.25" trend="Stable" status="success" icon={Wind} />
            </div>

            {/* Main Content Area */}
            <div className="grid grid-cols-1 xl:grid-cols-12 gap-8">
                {/* Vertical Timeline - Left Column */}
                <div className="xl:col-span-3 space-y-6">
                    <div className="flex items-center justify-between px-2">
                        <h3 className="text-xs font-bold uppercase tracking-[0.2em] text-[var(--color-text-tertiary)]">Batch Lifecycle</h3>
                        <History size={14} className="text-[var(--color-text-tertiary)]" />
                    </div>

                    <div className="relative pl-6 space-y-8 before:absolute before:left-2.5 before:top-2 before:bottom-2 before:w-0.5 before:bg-slate-100 dark:before:bg-slate-800">
                        <TimelineEvent
                            status="completed"
                            title="Harvested"
                            date="5 days remaining (Est.)"
                            desc="Target moisture: 10-12%"
                            icon={CheckCircle2}
                        />
                        <TimelineEvent
                            status="current"
                            title="Flowering"
                            date="Week 8 of 9"
                            desc="Pistils darkening (40%)"
                            icon={Activity}
                        />
                        <TimelineEvent
                            status="completed"
                            title="Veg Phase"
                            date="Completed Oct 15"
                            desc="28 days total"
                            icon={CheckCircle2}
                        />
                        <TimelineEvent
                            status="completed"
                            title="Cloned"
                            date="Completed Sept 18"
                            desc="Rooted successfully"
                            icon={CheckCircle2}
                        />
                    </div>
                </div>

                {/* Tabbed Content - Right Column */}
                <div className="xl:col-span-9 space-y-6">
                    <div className="flex items-center gap-8 border-b border-[var(--color-border-subtle)]">
                        <TabButton
                            active={activeTab === 'overview'}
                            label="Batch Overview"
                            icon={LayoutDashboard}
                            onClick={() => setActiveTab('overview')}
                        />
                        <TabButton
                            active={activeTab === 'environment'}
                            label="Environmental History"
                            icon={Activity}
                            onClick={() => setActiveTab('environment')}
                        />
                        <TabButton
                            active={activeTab === 'tasks'}
                            label="Task Log"
                            icon={ClipboardList}
                            count={12}
                            onClick={() => setActiveTab('tasks')}
                        />
                        <TabButton
                            active={activeTab === 'compliance'}
                            label="Compliance"
                            icon={ShieldCheck}
                            onClick={() => setActiveTab('compliance')}
                        />
                    </div>

                    <AnimatePresence mode="wait">
                        <motion.div
                            key={activeTab}
                            initial={{ opacity: 0, y: 10 }}
                            animate={{ opacity: 1, y: 0 }}
                            exit={{ opacity: 0, y: -10 }}
                            transition={{ duration: 0.2 }}
                        >
                            {activeTab === 'overview' && (
                                <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
                                    <Card className="p-6 bg-[var(--color-bg-elevated)] border-[var(--color-border-subtle)] space-y-6">
                                        <h4 className="text-xs font-bold uppercase tracking-widest text-[var(--color-text-tertiary)]">Quick Actions</h4>
                                        <div className="grid grid-cols-2 gap-3">
                                            <ActionButton label="Log Feeding" icon={Droplets} />
                                            <ActionButton label="IPM Action" icon={Bug} />
                                            <ActionButton label="Waste Log" icon={Trash2} />
                                            <ActionButton label="Room Transfer" icon={Layers} />
                                        </div>
                                    </Card>

                                    <Card className="p-6 bg-[var(--color-bg-elevated)] border-[var(--color-border-subtle)] space-y-6">
                                        <h4 className="text-xs font-bold uppercase tracking-widest text-[var(--color-text-tertiary)]">Integration Hub</h4>
                                        <div className="space-y-4">
                                            <IntegrationItem name="METRC" status="Connected" time="Sync 2m ago" />
                                            <IntegrationItem name="DLI Monitor" status="Active" time="840 umol/m2" />
                                            <IntegrationItem name="Sensors" status="Online" time="8 Nodes active" />
                                        </div>
                                    </Card>

                                    <Card className="md:col-span-2 p-6 bg-[var(--color-bg-elevated)] border-[var(--color-border-subtle)]">
                                        <div className="flex items-center justify-between mb-6">
                                            <h4 className="text-xs font-bold uppercase tracking-widest text-[var(--color-text-tertiary)] text-left">Recent Activity</h4>
                                            <button className="text-[10px] font-bold text-[var(--color-primary)] uppercase tracking-widest hover:underline">View All Log</button>
                                        </div>
                                        <div className="space-y-4">
                                            {batch.touchPoints?.map(tp => (
                                                <div key={tp.id} className="flex items-center justify-between p-3 rounded-xl bg-[var(--color-bg-tertiary)]/50 border border-[var(--color-border-subtle)]/50">
                                                    <div className="flex items-center gap-4">
                                                        <div className="w-8 h-8 rounded-lg bg-[var(--color-bg-elevated)] flex items-center justify-center border border-[var(--color-border-subtle)] shadow-sm">
                                                            <Activity size={14} className="text-[var(--color-primary)]" />
                                                        </div>
                                                        <div>
                                                            <p className="text-sm font-bold text-[var(--color-text-primary)]">{tp.type}</p>
                                                            <p className="text-[10px] text-[var(--color-text-tertiary)] font-medium">{tp.notes || 'Routine check complete'}</p>
                                                        </div>
                                                    </div>
                                                    <div className="text-right">
                                                        <p className="text-[10px] font-bold text-[var(--color-text-primary)] uppercase tracking-widest">{tp.user?.name}</p>
                                                        <p className="text-[10px] text-[var(--color-text-tertiary)]">{new Date(tp.createdAt).toLocaleDateString()}</p>
                                                    </div>
                                                </div>
                                            ))}
                                        </div>
                                    </Card>
                                </div>
                            )}

                            {activeTab === 'environment' && (
                                <Card className="p-8 bg-[var(--color-bg-elevated)] border-[var(--color-border-subtle)]">
                                    <div className="flex items-center justify-between mb-8">
                                        <h4 className="text-xs font-bold uppercase tracking-widest text-[var(--color-text-tertiary)]">Sensor Rollups</h4>
                                        <div className="flex items-center gap-2">
                                            <button className="px-3 py-1.5 rounded-lg bg-[var(--color-bg-tertiary)] text-[10px] font-bold uppercase tracking-widest">24h</button>
                                            <button className="px-3 py-1.5 rounded-lg text-[10px] font-bold uppercase tracking-widest">7d</button>
                                        </div>
                                    </div>
                                    <div className="h-[400px] w-full bg-slate-50/50 dark:bg-slate-900/50 rounded-2xl border border-[var(--color-border-subtle)] flex items-center justify-center text-[var(--color-text-tertiary)]">
                                        <div className="flex flex-col items-center gap-2 text-center">
                                            <TrendingUp size={32} />
                                            <span className="text-xs font-bold tracking-widest uppercase">Interactive Analytics Terminal</span>
                                            <span className="text-[10px] opacity-50">Rendering environmental time-series...</span>
                                        </div>
                                    </div>
                                </Card>
                            )}

                            {activeTab === 'compliance' && (
                                <div className="space-y-6">
                                    <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
                                        <div className="p-5 rounded-2xl bg-[var(--color-accent)]/10 border border-blue-500/20">
                                            <ShieldCheck className="text-[var(--color-accent)] mb-2" size={24} />
                                            <h5 className="text-sm font-bold text-[var(--color-accent)]">Compliance OK</h5>
                                            <p className="text-[11px] text-blue-400 font-medium mt-1">No outstanding events detected.</p>
                                        </div>
                                        <div className="p-5 rounded-2xl bg-[var(--color-primary)]/10 border border-emerald-500/20">
                                            <CheckCircle2 className="text-[var(--color-primary)] mb-2" size={24} />
                                            <h5 className="text-sm font-bold text-[var(--color-primary)]">Verified</h5>
                                            <p className="text-[11px] text-emerald-400 font-medium mt-1">All labor hours signed off.</p>
                                        </div>
                                        <div className="p-5 rounded-2xl bg-[var(--color-warning)]/10 border border-amber-500/20">
                                            <Download className="text-[var(--color-warning)] mb-2" size={24} />
                                            <h5 className="text-sm font-bold text-[var(--color-warning)]">Manifest Ready</h5>
                                            <p className="text-[11px] text-amber-400 font-medium mt-1">Export batch certificate of analysis.</p>
                                        </div>
                                    </div>

                                    <Card className="p-6 bg-[var(--color-bg-elevated)] border-[var(--color-border-subtle)]">
                                        <h4 className="text-xs font-bold uppercase tracking-widest text-[var(--color-text-tertiary)] mb-6">Traceability History</h4>
                                        <div className="space-y-0.5">
                                            {[1, 2, 3].map(i => (
                                                <div key={i} className="flex items-center justify-between p-3 rounded-lg hover:bg-slate-50 dark:hover:bg-slate-900/50 transition-colors">
                                                    <div className="flex items-center gap-4">
                                                        <Clock size={14} className="text-[var(--color-text-tertiary)]" />
                                                        <span className="text-xs font-bold font-mono text-[var(--color-text-tertiary)]">#E-00452-Z</span>
                                                        <span className="text-xs font-medium">Batch Transfer: Veg-1 to Flow-A</span>
                                                    </div>
                                                    <ExternalLink size={14} className="text-[var(--color-text-tertiary)]" />
                                                </div>
                                            ))}
                                        </div>
                                    </Card>
                                </div>
                            )}
                        </motion.div>
                    </AnimatePresence>
                </div>
            </div>
        </div>
    );
}

function KPICard({ label, value, trend, status, icon: Icon }: any) {
    return (
        <Card className="p-5 bg-[var(--color-bg-elevated)] border-[var(--color-border-subtle)] relative group overflow-hidden">
            <div className="flex items-start justify-between">
                <div>
                    <div className="flex items-center gap-2 mb-1">
                        <Icon size={14} className="text-[var(--color-text-tertiary)]" />
                        <span className="text-[10px] font-bold text-[var(--color-text-tertiary)] uppercase tracking-widest">{label}</span>
                    </div>
                    <div className="text-2xl font-black italic tracking-tighter text-[var(--color-text-primary)]">
                        {value}
                    </div>
                </div>
                <div className={cn(
                    "px-1.5 py-0.5 rounded text-[9px] font-bold uppercase",
                    status === 'success' ? "bg-[var(--color-primary)]/10 text-[var(--color-primary)]" : "bg-slate-100 dark:bg-slate-800 text-[var(--color-text-tertiary)]"
                )}>
                    {trend}
                </div>
            </div>
            <div className="absolute bottom-0 left-0 w-full h-0.5 bg-slate-100 dark:bg-slate-800" />
        </Card>
    );
}

function TimelineEvent({ status, title, date, desc, icon: Icon }: any) {
    return (
        <div className="relative group">
            <div className={cn(
                "absolute -left-[1.35rem] w-3 h-3 rounded-full border-2 transition-all group-hover:scale-125 z-10",
                status === 'completed' ? "bg-[var(--color-primary)] border-emerald-600 shadow-sm" :
                    status === 'current' ? "bg-white bg-[var(--color-bg-primary)] border-emerald-500 animate-pulse" :
                        "bg-slate-200 dark:bg-slate-800 border-slate-300 dark:border-slate-700"
            )} />
            <div className="space-y-1">
                <div className="flex items-center gap-2">
                    <Icon size={12} className={status === 'completed' ? "text-[var(--color-primary)]" : "text-[var(--color-text-tertiary)]"} />
                    <h5 className={cn(
                        "text-[11px] font-black uppercase tracking-widest",
                        status === 'completed' ? "text-[var(--color-text-primary)]" : "text-[var(--color-text-tertiary)]"
                    )}>{title}</h5>
                </div>
                <p className="text-[10px] font-bold text-[var(--color-primary)] tracking-tighter uppercase">{date}</p>
                <p className="text-[10px] text-[var(--color-text-tertiary)] font-medium leading-relaxed">{desc}</p>
            </div>
        </div>
    );
}

function TabButton({ active, label, icon: Icon, count, onClick }: any) {
    return (
        <button
            onClick={onClick}
            className={cn(
                "flex items-center gap-2 px-1 py-4 text-xs font-bold uppercase tracking-[0.15em] transition-all relative",
                active ? "text-[var(--color-primary)]" : "text-[var(--color-text-tertiary)] hover:text-slate-800 dark:hover:text-slate-200"
            )}
        >
            <Icon size={16} />
            {label}
            {count !== undefined && (
                <span className={cn(
                    "ml-1 px-1.5 py-0.5 rounded-full text-[9px]",
                    active ? "bg-[var(--color-primary)] text-white" : "bg-slate-100 dark:bg-slate-800 text-[var(--color-text-tertiary)]"
                )}>
                    {count}
                </span>
            )}
            {active && (
                <motion.div
                    layoutId="batch-tab"
                    className="absolute bottom-0 left-0 right-0 h-0.5 bg-[var(--color-primary)]"
                />
            )}
        </button>
    );
}

function ActionButton({ label, icon: Icon }: any) {
    return (
        <button className="flex flex-col items-center justify-center p-4 rounded-xl bg-[var(--color-bg-tertiary)]/50 border border-[var(--color-border-subtle)]/50 hover:border-emerald-500/50 hover:bg-[var(--color-primary)]/10 transition-all group">
            <Icon size={20} className="text-[var(--color-text-tertiary)] group-hover:text-[var(--color-primary)] mb-2 transition-colors" />
            <span className="text-[10px] font-bold uppercase tracking-widest text-[var(--color-text-tertiary)] group-hover:text-[var(--color-primary)]">{label}</span>
        </button>
    );
}

function IntegrationItem({ name, status, time }: any) {
    return (
        <div className="flex items-center justify-between p-3 rounded-xl bg-[var(--color-bg-tertiary)]/50 border border-[var(--color-border-subtle)]/50">
            <div className="flex items-center gap-3">
                <div className="w-1.5 h-1.5 rounded-full bg-[var(--color-primary)]" />
                <div>
                    <p className="text-[11px] font-bold text-[var(--color-text-primary)]">{name}</p>
                    <p className="text-[9px] text-[var(--color-text-tertiary)] font-medium uppercase tracking-tighter">{time}</p>
                </div>
            </div>
            <span className="text-[10px] font-bold text-[var(--color-primary)] uppercase tracking-widest">{status}</span>
        </div>
    );
}

function Layers({ size, className }: any) { return <Sun size={size} className={className} />; }
function LayoutDashboard({ size, className }: any) { return <Activity size={size} className={className} />; }

