import UserLayout from '@/Layouts/UserLayout';
import { Head, Link } from '@inertiajs/react';
import { BookOpen, Clock, ChevronRight, PlayCircle, Trophy } from 'lucide-react';

interface Lesson {
    id: number;
}

interface Module {
    id: number;
    lessons: Lesson[];
}

interface Product {
    id: number;
    name: string;
    slug: string;
    image_path: string | null;
    media?: { url: string };
    modules: Module[];
}

interface Enrollment {
    id: number;
    product: Product;
    status: string;
    completed_at: string | null;
}

interface Props {
    enrollments: Enrollment[];
    userProgress: { course_lesson_id: number }[];
}

export default function Dashboard({ enrollments, userProgress }: Props) {
    return (
        <UserLayout>
            <Head title="Learning Portal" />

            <div className="max-w-4xl">
                <div className="mb-10">
                    <h1 className="font-display text-3xl md:text-4xl leading-tight text-ink mb-2">
                        My Learning
                    </h1>
                    <p className="text-[#8C92AC] text-[15px] max-w-xl">
                        Welcome back! Pick up where you left off and continue your journey.
                    </p>
                </div>

                    <div className="grid gap-6">
                        {enrollments.map((enrollment) => {
                            const course = enrollment.product;
                            const totalLessons = course.modules.reduce((acc, m) => acc + m.lessons.length, 0);
                            const courseLessonIds = course.modules.flatMap(m => m.lessons.map(l => l.id));
                            const completedInCourse = userProgress.filter(p => courseLessonIds.includes(p.course_lesson_id)).length;
                            const percentage = totalLessons > 0 ? Math.round((completedInCourse / totalLessons) * 100) : 0;
                            
                            return (
                                <Link 
                                    key={enrollment.id} 
                                    href={route('learning.show', course.slug)}
                                    className="group bg-white rounded-xl border border-[#E8E6DF] hover:border-primary/50 transition-all flex items-center p-3 gap-4 cursor-pointer"
                                >
                                    <img 
                                        src={course.media?.url || '/images/placeholder.jpg'} 
                                        alt={course.name}
                                        className="h-16 w-16 rounded-lg object-cover shrink-0 bg-[#F5F3EF]"
                                    />
                                    
                                    <div className="flex-1 min-w-0 flex items-center justify-between gap-4">
                                        <div className="flex-1 min-w-0">
                                            <div className="flex items-center gap-2 mb-1">
                                                <span className="bg-[#F5F3EF] px-2 py-0.5 rounded-full text-[9px] font-bold text-primary uppercase tracking-wider">
                                                    Course
                                                </span>
                                                <span className="text-[9px] font-semibold text-[#8C92AC]">
                                                    {totalLessons} Lessons
                                                </span>
                                            </div>

                                            <h3 className="font-display text-base text-ink mb-1.5 group-hover:text-primary transition-colors truncate">
                                                {course.name}
                                            </h3>

                                            <div className="flex items-center gap-3 max-w-xs">
                                                <div className="h-1 flex-1 bg-[#F5F3EF] rounded-full overflow-hidden">
                                                    <div className="h-full bg-primary rounded-full transition-all duration-500" style={{ width: `${percentage}%` }} />
                                                </div>
                                                <span className="text-[9px] font-bold text-ink whitespace-nowrap">{percentage}% completed</span>
                                            </div>
                                        </div>

                                        <div className="shrink-0 text-[#C0BDBA] group-hover:text-primary transition-colors pr-1">
                                            <ChevronRight className="h-5 w-5" />
                                        </div>
                                    </div>
                                </Link>
                            );
                        })}

                        {enrollments.length === 0 && (
                            <div className="col-span-full py-20 text-center bg-white border-2 border-dashed border-[#E8E6DF] rounded-[3rem]">
                                <div className="h-20 w-20 bg-[#F5F3EF] rounded-[2.5rem] flex items-center justify-center mx-auto mb-6">
                                    <Trophy className="h-10 w-10 text-[#C0BDBA]" />
                                </div>
                                <h3 className="font-display text-2xl text-ink mb-2">No courses yet</h3>
                                <p className="text-[#8C92AC] mb-8">You haven't enrolled in any courses yet. Explore our store to get started.</p>
                                <Link 
                                    href={route('store.index')}
                                    className="inline-flex h-14 items-center px-8 bg-primary text-white rounded-2xl text-[15px] font-bold hover:brightness-110 transition-all"
                                >
                                    Browse Courses
                                </Link>
                            </div>
                        )}
                    </div>
                </div>
        </UserLayout>
    );
}
