import { Head, Link } from '@inertiajs/react';
import {
    BookOpen,
    CheckCircle2,
    ChevronRight,
    Clock,
    Lock,
    Map,
} from 'lucide-react';
import GuestLayout from '@/layouts/guest-layout';

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 max-w-3xl flex-col gap-6 p-4 md:p-6">
                {/* Premium Pastel Blue Header Card */}
                <div className="relative animate-in 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 duration-300 fade-in dark:border-blue-950/40 dark:from-blue-950/20 dark:to-indigo-950/10">
                    {/* Decorative glowing background bubbles */}
                    <div className="absolute -top-10 -right-10 h-36 w-36 rounded-full bg-blue-400/10 blur-2xl" />
                    <div className="absolute -bottom-10 -left-10 h-36 w-36 rounded-full bg-indigo-400/10 blur-2xl" />

                    <div className="relative flex flex-col gap-5 md:flex-row md:items-center">
                        {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 border border-blue-200/50 bg-blue-100/80 text-blue-600 shadow-xs dark:border-blue-900/30 dark:bg-blue-950/50 dark:text-blue-400">
                                <Map className="h-8 w-8" />
                            </div>
                        )}
                        <div className="min-w-0 flex-1">
                            <div className="flex flex-wrap items-center gap-2">
                                <span className="rounded-full bg-blue-100/80 px-2.5 py-0.5 text-[10px] font-bold tracking-wider text-blue-700 uppercase dark:bg-blue-950 dark:text-blue-400">
                                    Paket Belajar
                                </span>
                            </div>
                            <h1 className="mt-1 text-2xl leading-tight font-black text-slate-800 dark:text-slate-100">
                                {path.title}
                            </h1>
                            {path.description && (
                                <p className="mt-2 text-sm leading-relaxed text-slate-600 dark:text-slate-300">
                                    {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 rounded-lg border border-blue-100/50 bg-white/70 px-2.5 py-1 shadow-2xs dark:border-blue-950/50 dark:bg-zinc-900/40">
                                    <BookOpen className="h-3.5 w-3.5 text-blue-500" />
                                    {stats.total} kelas
                                </span>
                                <span className="flex items-center gap-1 rounded-lg border border-blue-100/50 bg-white/70 px-2.5 py-1 shadow-2xs dark:border-blue-950/50 dark:bg-zinc-900/40">
                                    <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 items-center justify-between 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 shadow-xs transition-all duration-500"
                                style={{ width: `${stats.progress_percent}%` }}
                            />
                        </div>
                    </div>
                )}

                {/* Course list */}
                <div className="mt-2 flex flex-col">
                    {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 top-11 bottom-0 left-5 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 animate-pulse rounded-full bg-blue-500" />
                                    ) : (
                                        <span className="text-xs font-bold text-blue-400">
                                            {index + 1}
                                        </span>
                                    )}
                                </div>

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

                                        <div className="min-w-0 flex-1">
                                            <div className="flex items-start justify-between gap-3">
                                                <p className="text-sm leading-snug font-bold text-slate-800 transition-colors group-hover:text-blue-600 sm:text-base dark:text-slate-100">
                                                    {course.title}
                                                </p>
                                                <ChevronRight className="h-5 w-5 shrink-0 self-center text-slate-400 transition-colors group-hover:text-blue-500" />
                                            </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 border border-blue-100/50 bg-blue-50 px-2 py-0.5 text-[10px] text-blue-700 dark:border-blue-900/20 dark:bg-blue-950/30 dark:text-blue-400">
                                                    {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 rounded-xl border border-blue-100/30 bg-blue-50/30 p-2 dark:border-blue-950/20 dark:bg-blue-950/10">
                                                    <div className="mb-1 flex justify-between text-[10px] font-bold text-blue-600 dark:text-blue-400">
                                                        <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 font-bold text-emerald-600 dark:text-emerald-400">
                                                    <CheckCircle2 className="h-4 w-4" />
                                                    <span>
                                                        Selesai dipelajari
                                                    </span>
                                                </div>
                                            )}
                                        </div>
                                    </Link>
                                </div>
                            </div>
                        );
                    })}
                </div>
            </div>
        </>
    );
}

function LearningPathShowWrapper({ children }: { children: React.ReactNode }) {
    return <GuestLayout>{children}</GuestLayout>;
}

LearningPathShow.layout = (page: React.ReactNode) => (
    <LearningPathShowWrapper>{page}</LearningPathShowWrapper>
);
