import { Head, Link } from '@inertiajs/react';
import { BookOpen, CheckCircle2, ChevronRight, Clock, Lock, Map } from 'lucide-react';
import AppLayout from '@/layouts/app-layout';
import type { BreadcrumbItem } from '@/types';

type Course = {
    id: number;
    title: string;
    slug: string;
    thumbnail: string | null;
    level: string;
    price_type: string;
    price: number;
    duration_minutes: number;
    order: number;
    enrollment_status: 'active' | 'completed' | null;
    progress_percent: number;
};

type Path = { id: number; title: string; slug: string; description: string | null; thumbnail: string | null };
type Stats = { total: number; completed: number; progress_percent: number };
type Props = { path: Path; courses: Course[]; stats: Stats };

function levelLabel(level: string) {
    const map: Record<string, string> = { beginner: 'Pemula', intermediate: 'Menengah', advanced: 'Lanjutan' };
    return map[level] ?? level;
}

export default function LearningPathShow({ path, courses, stats }: Props) {
    return (
        <>
            <Head title={path.title} />

            <div className="flex flex-col gap-6 p-4 md:p-6 max-w-3xl">
                {/* Premium Pastel Blue Header Card */}
                <div className="relative overflow-hidden rounded-3xl border border-blue-100/70 bg-gradient-to-br from-blue-50/60 via-blue-50/30 to-indigo-50/40 p-6 shadow-xs dark:border-blue-950/40 dark:from-blue-950/20 dark:to-indigo-950/10 animate-in fade-in duration-300">
                    {/* Decorative glowing background bubbles */}
                    <div className="absolute -right-10 -top-10 h-36 w-36 rounded-full bg-blue-400/10 blur-2xl" />
                    <div className="absolute -left-10 -bottom-10 h-36 w-36 rounded-full bg-indigo-400/10 blur-2xl" />

                    <div className="relative flex flex-col md:flex-row md:items-center gap-5">
                        {path.thumbnail ? (
                            <div className="h-16 w-16 shrink-0 overflow-hidden rounded-2xl border border-blue-200/50 shadow-xs dark:border-blue-900/30">
                                <img src={path.thumbnail} alt={path.title} className="h-full w-full object-cover" />
                            </div>
                        ) : (
                            <div className="flex h-16 w-16 shrink-0 items-center justify-center rounded-2xl bg-blue-100/80 text-blue-600 border border-blue-200/50 shadow-xs dark:bg-blue-950/50 dark:text-blue-400 dark:border-blue-900/30">
                                <Map className="h-8 w-8" />
                            </div>
                        )}
                        <div className="flex-1 min-w-0">
                            <div className="flex items-center gap-2 flex-wrap">
                                <span className="rounded-full bg-blue-100/80 px-2.5 py-0.5 text-[10px] font-bold text-blue-700 uppercase tracking-wider dark:bg-blue-950 dark:text-blue-400">
                                    Paket Belajar
                                </span>
                            </div>
                            <h1 className="mt-1 text-2xl font-black text-slate-800 dark:text-slate-100 leading-tight">
                                {path.title}
                            </h1>
                            {path.description && (
                                <p className="mt-2 text-sm text-slate-600 dark:text-slate-300 leading-relaxed">
                                    {path.description}
                                </p>
                            )}
                            <div className="mt-4 flex items-center gap-4 text-xs font-semibold text-slate-500 dark:text-slate-400">
                                <span className="flex items-center gap-1 bg-white/70 dark:bg-zinc-900/40 px-2.5 py-1 rounded-lg border border-blue-100/50 dark:border-blue-950/50 shadow-2xs">
                                    <BookOpen className="h-3.5 w-3.5 text-blue-500" />
                                    {stats.total} kelas
                                </span>
                                <span className="flex items-center gap-1 bg-white/70 dark:bg-zinc-900/40 px-2.5 py-1 rounded-lg border border-blue-100/50 dark:border-blue-950/50 shadow-2xs">
                                    <CheckCircle2 className="h-3.5 w-3.5 text-emerald-500" />
                                    {stats.completed} selesai
                                </span>
                            </div>
                        </div>
                    </div>
                </div>

                {/* Progress bar */}
                {stats.total > 0 && (
                    <div className="rounded-2xl border border-blue-100/40 bg-blue-50/10 p-4 dark:border-blue-950/20 dark:bg-zinc-950/10">
                        <div className="mb-2 flex justify-between items-center text-xs font-bold text-slate-600 dark:text-slate-300">
                            <span>Progress Keseluruhan</span>
                            <span className="text-blue-600 dark:text-blue-400">{stats.progress_percent}% selesai</span>
                        </div>
                        <div className="h-2.5 w-full overflow-hidden rounded-full bg-slate-100 dark:bg-zinc-800">
                            <div
                                className="h-full rounded-full bg-gradient-to-r from-blue-500 to-indigo-500 transition-all duration-500 shadow-xs"
                                style={{ width: `${stats.progress_percent}%` }}
                            />
                        </div>
                    </div>
                )}

                {/* Course list */}
                <div className="flex flex-col mt-2">
                    {courses.map((course, index) => {
                        const isCompleted = course.enrollment_status === 'completed';
                        const isActive = course.enrollment_status === 'active';
                        const isNotEnrolled = !course.enrollment_status;

                        return (
                            <div key={course.id} className="relative flex gap-5">
                                {/* Timeline line */}
                                {index < courses.length - 1 && (
                                    <div className="absolute left-5 top-11 bottom-0 w-0.5 bg-blue-100/60 dark:bg-blue-950/50" />
                                )}

                                {/* Step icon */}
                                <div className="relative z-10 flex h-10 w-10 shrink-0 items-center justify-center rounded-full border-2 bg-white shadow-xs transition-colors duration-350"
                                     style={{
                                         borderColor: isCompleted ? '#10B981' : (isActive ? '#3B82F6' : '#DBEAFE'),
                                     }}
                                >
                                    {isCompleted ? (
                                        <CheckCircle2 className="h-5 w-5 text-emerald-500" />
                                    ) : isActive ? (
                                        <span className="h-3 w-3 rounded-full bg-blue-500 animate-pulse" />
                                    ) : (
                                        <span className="text-xs font-bold text-blue-400">{index + 1}</span>
                                    )}
                                </div>

                                {/* Premium Card */}
                                <div className={`mb-5 flex-1 overflow-hidden rounded-2xl border border-blue-50 bg-white shadow-3xs transition-all duration-300 hover:shadow-md hover:border-blue-200/80 dark:border-blue-950/30 dark:bg-card ${isCompleted ? 'opacity-90 border-emerald-100 dark:border-emerald-950/30' : ''}`}>
                                    <Link href={course.enrollment_status ? `/courses/${course.slug}/learn` : `/courses/${course.slug}`} className="flex flex-col sm:flex-row sm:items-center gap-4 p-4 group">
                                        {course.thumbnail ? (
                                            <img
                                                src={course.thumbnail}
                                                alt={course.title}
                                                className="h-20 w-full sm:h-16 sm:w-28 rounded-xl object-cover shrink-0 border border-blue-100/40 shadow-4xs group-hover:scale-[1.02] transition-transform duration-300"
                                            />
                                        ) : (
                                            <div className="flex h-20 w-full sm:h-16 sm:w-28 shrink-0 items-center justify-center rounded-xl bg-blue-50/40 text-blue-400 dark:bg-zinc-900/30">
                                                <BookOpen className="h-7 w-7 opacity-70" />
                                            </div>
                                        )}

                                        <div className="flex-1 min-w-0">
                                            <div className="flex items-start justify-between gap-3">
                                                <p className="font-bold text-slate-800 dark:text-slate-100 text-sm sm:text-base leading-snug group-hover:text-blue-600 transition-colors">
                                                    {course.title}
                                                </p>
                                                <ChevronRight className="h-5 w-5 shrink-0 text-slate-400 group-hover:text-blue-500 transition-colors self-center" />
                                            </div>

                                            <div className="mt-2 flex flex-wrap items-center gap-2 text-xs font-semibold text-slate-500 dark:text-slate-400">
                                                <span className="rounded-full bg-blue-50 dark:bg-blue-950/30 px-2 py-0.5 text-[10px] text-blue-700 dark:text-blue-400 border border-blue-100/50 dark:border-blue-900/20">
                                                    {levelLabel(course.level)}
                                                </span>
                                                {course.duration_minutes > 0 && (
                                                    <>
                                                        <span className="text-blue-200 dark:text-blue-900">•</span>
                                                        <span className="flex items-center gap-1">
                                                            <Clock className="h-3.5 w-3.5 text-slate-400" />
                                                            {course.duration_minutes} mnt
                                                        </span>
                                                    </>
                                                )}
                                                {isNotEnrolled && (
                                                    <>
                                                        <span className="text-blue-200 dark:text-blue-900">•</span>
                                                        {course.price_type === 'free' ? (
                                                            <span className="flex items-center gap-0.5 text-emerald-600 dark:text-emerald-400">
                                                                ✦ Gratis
                                                            </span>
                                                        ) : (
                                                            <span className="flex items-center gap-0.5 text-blue-600 dark:text-blue-400">
                                                                <Lock className="h-3 w-3" />
                                                                {course.price ? `Rp ${course.price.toLocaleString('id-ID')}` : 'Berbayar'}
                                                            </span>
                                                        )}
                                                    </>
                                                )}
                                            </div>

                                            {isActive && (
                                                <div className="mt-3 bg-blue-50/30 dark:bg-blue-950/10 p-2 rounded-xl border border-blue-100/30 dark:border-blue-950/20">
                                                    <div className="flex justify-between text-[10px] font-bold text-blue-600 dark:text-blue-400 mb-1">
                                                        <span>Progress belajar</span>
                                                        <span>{course.progress_percent}%</span>
                                                    </div>
                                                    <div className="h-1.5 w-full overflow-hidden rounded-full bg-slate-100 dark:bg-zinc-800">
                                                        <div
                                                            className="h-full rounded-full bg-blue-500 transition-all"
                                                            style={{ width: `${course.progress_percent}%` }}
                                                        />
                                                    </div>
                                                </div>
                                            )}

                                            {isCompleted && (
                                                <div className="mt-2 flex items-center gap-1 text-xs text-emerald-600 font-bold dark:text-emerald-400">
                                                    <CheckCircle2 className="h-4 w-4" />
                                                    <span>Selesai dipelajari</span>
                                                </div>
                                            )}
                                        </div>
                                    </Link>
                                </div>
                            </div>
                        );
                    })}
                </div>
            </div>
        </>
    );
}

LearningPathShow.layout = (page: React.ReactNode) => {
    const props = (page as React.ReactElement)?.props as Props | undefined;
    const breadcrumbs: BreadcrumbItem[] = [
        { title: 'Paket Belajar', href: '/learning-paths' },
        { title: props?.path?.title || 'Detail', href: props?.path?.slug ? `/learning-paths/${props.path.slug}` : '#' },
    ];
    return <AppLayout breadcrumbs={breadcrumbs}>{page}</AppLayout>;
};
