'use client';

import React from 'react';
import { CheckCircle2, AlertTriangle, Clock, Droplets, Info } from 'lucide-react';
import Link from 'next/link';

interface TimelineTasting {
    id: string;
    bottle_id: string;
    bottle_name: string;
    tasted_at: string;
    rating: number;
    tags: string[];
    category?: string;
}

interface SessionTimelineProps {
    tastings: TimelineTasting[];
    sessionStart?: string;
}

// Keywords that indicate a "Peat Bomb"
const SMOKY_KEYWORDS = ['rauch', 'torf', 'smoke', 'peat', 'islay', 'ash', 'lagerfeuer', 'campfire', 'asphalte'];

export default function SessionTimeline({ tastings, sessionStart }: SessionTimelineProps) {
    if (!tastings || tastings.length === 0) {
        return (
            <div className="p-8 text-center bg-zinc-50 dark:bg-zinc-900/50 rounded-3xl border border-dashed border-zinc-200 dark:border-zinc-800">
                <Clock size={32} className="mx-auto text-zinc-300 mb-3" />
                <p className="text-zinc-500 text-sm font-medium italic">Noch keine Dram-Historie vorhanden.</p>
            </div>
        );
    }

    // Sort by tasted_at
    const sortedTastings = [...tastings].sort((a, b) =>
        new Date(a.tasted_at).getTime() - new Date(b.tasted_at).getTime()
    );

    const firstTastingTime = sessionStart ? new Date(sessionStart).getTime() : new Date(sortedTastings[0].tasted_at).getTime();

    const checkIsSmoky = (tasting: TimelineTasting) => {
        const textToSearch = (tasting.tags.join(' ') + ' ' + (tasting.category || '')).toLowerCase();
        return SMOKY_KEYWORDS.some(keyword => textToSearch.includes(keyword));
    };

    return (
        <div className="relative pl-6 space-y-6 before:absolute before:inset-0 before:left-[9px] before:w-[2px] before:bg-zinc-200 dark:before:bg-zinc-800 before:h-full mb-12">
            {sortedTastings.map((tasting, index) => {
                const tastedDate = new Date(tasting.tasted_at);
                const currentTime = tastedDate.getTime();
                const diffMinutes = Math.round((currentTime - firstTastingTime) / (1000 * 60));
                const wallClockTime = tastedDate.toLocaleTimeString('de-DE', { hour: '2-digit', minute: '2-digit' });
                const isSmoky = checkIsSmoky(tasting);

                const wasPreviousSmoky = index > 0 && checkIsSmoky(sortedTastings[index - 1]);
                const timeSinceLastDram = index > 0
                    ? Math.round((currentTime - new Date(sortedTastings[index - 1].tasted_at).getTime()) / (1000 * 60))
                    : 0;

                return (
                    <div key={tasting.id} className="relative group">
                        {/* Dot */}
                        <div className={`absolute -left-[22px] top-4 w-5 h-5 rounded-full border-[3px] border-zinc-100 dark:border-black shadow-sm z-10 flex items-center justify-center ${isSmoky ? 'bg-amber-600' : 'bg-zinc-400'}`}>
                            {isSmoky && <Droplets size={8} className="text-white fill-white" />}
                        </div>

                        <div className="bg-white dark:bg-zinc-900 p-4 rounded-2xl border border-zinc-200 dark:border-zinc-800 shadow-sm hover:shadow-md transition-shadow group-hover:border-amber-500/30">
                            <div className="flex justify-between items-start gap-3">
                                <div className="min-w-0">
                                    <div className="flex items-center gap-2 mb-1">
                                        <span className="text-[10px] font-black text-amber-600 uppercase tracking-widest leading-none">Dram #{index + 1}</span>
                                        <span className="text-[10px] font-bold text-zinc-400 dark:text-zinc-600 uppercase tracking-tight leading-none">
                                            {wallClockTime} ({index === 0 ? 'Start' : `+${diffMinutes}m`})
                                        </span>
                                        {isSmoky && (
                                            <span className="bg-amber-100 dark:bg-amber-900/30 text-amber-700 dark:text-amber-400 text-[8px] font-black px-1.5 py-0.5 rounded-md uppercase tracking-tighter">Peat Bomb</span>
                                        )}
                                    </div>
                                    <Link
                                        href={`/bottles/${tasting.bottle_id}`}
                                        className="text-sm font-bold text-zinc-800 dark:text-zinc-100 hover:text-amber-600 truncate block mt-0.5"
                                    >
                                        {tasting.bottle_name}
                                    </Link>
                                    <div className="mt-2 flex flex-wrap gap-1">
                                        {tasting.tags.slice(0, 2).map(tag => (
                                            <span key={tag} className="text-[9px] text-zinc-500 dark:text-zinc-500 bg-zinc-100 dark:bg-zinc-800/50 px-2 py-0.5 rounded-full">
                                                {tag}
                                            </span>
                                        ))}
                                    </div>
                                </div>
                                <div className="shrink-0 flex flex-col items-end">
                                    <div className="text-lg font-black text-zinc-900 dark:text-white leading-none">{tasting.rating}</div>
                                    <div className="text-[9px] font-bold text-zinc-400 uppercase tracking-tighter mt-1">Punkte</div>
                                </div>
                            </div>

                            {wasPreviousSmoky && timeSinceLastDram < 20 && (
                                <div className="mt-4 p-2 bg-amber-50 dark:bg-amber-900/10 border border-amber-200/50 dark:border-amber-900/30 rounded-xl flex items-center gap-2 animate-in slide-in-from-top-1">
                                    <AlertTriangle size={12} className="text-amber-600 shrink-0" />
                                    <p className="text-[9px] text-amber-800 dark:text-amber-400 font-bold leading-tight">
                                        Achtung: Gaumen war noch torf-belegt (nur {timeSinceLastDram}m Abstand).
                                    </p>
                                </div>
                            )}
                        </div>
                    </div>
                );
            })}
        </div>
    );
}
