import AltivateLayout from '@/Layouts/AltivateLayout';
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 (
        <AltivateLayout>
            <Head title="My Learning" />

            <div className="min-h-screen bg-[#FAFAF8] pt-32 pb-20">
                <div className="container-x">
                    <div className="mb-12">
                        <h1 className="font-display text-[2.5rem] md:text-[3.5rem] leading-tight text-ink mb-4">
                            My Learning
                        </h1>
                        <p className="text-[#8C92AC] text-lg max-w-xl">
                            Welcome back! Pick up where you left off and continue your journey.
                        </p>
                    </div>

                    <div className="grid gap-8 md:grid-cols-2 lg:grid-cols-3">
                        {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 (
                                <div key={enrollment.id} className="group bg-white rounded-[2.5rem] border border-[#E8E6DF] overflow-hidden hover:border-primary/50 transition-all hover:shadow-xl hover:shadow-primary/5">
                                    <div className="aspect-[16/9] relative overflow-hidden">
                                        <img 
                                            src={course.media?.url || '/images/placeholder.jpg'} 
                                            alt={course.name}
                                            className="w-full h-full object-cover group-hover:scale-105 transition-transform duration-500"
                                        />
                                        <div className="absolute inset-0 bg-ink/20 opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center">
                                            <div className="h-14 w-14 rounded-full bg-white flex items-center justify-center text-primary shadow-xl">
                                                <PlayCircle className="h-8 w-8 fill-primary text-white" />
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div className="p-8">
                                        <div className="flex items-center gap-3 mb-4">
                                            <span className="bg-[#F5F3EF] px-3 py-1 rounded-full text-[10px] font-bold text-primary uppercase tracking-wider">
                                                Online Course
                                            </span>
                                            <span className="flex items-center gap-1.5 text-[10px] font-bold text-[#8C92AC] uppercase tracking-wider">
                                                <BookOpen className="h-3.5 w-3.5" /> {totalLessons} Lessons
                                            </span>
                                        </div>

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

                                        <div className="space-y-6">
                                            <div className="space-y-2">
                                                <div className="flex justify-between text-[11px] font-bold uppercase tracking-wider">
                                                    <span className="text-[#8C92AC]">Progress</span>
                                                    <span className="text-ink">{percentage}%</span>
                                                </div>
                                                <div className="h-1.5 w-full bg-[#F5F3EF] rounded-full overflow-hidden">
                                                    <div className="h-full bg-primary rounded-full transition-all duration-500" style={{ width: `${percentage}%` }} />
                                                </div>
                                            </div>

                                            <Link 
                                                href={route('learning.show', course.slug)}
                                                className="flex items-center justify-center gap-2 w-full h-12 bg-ink text-white rounded-2xl text-[13px] font-bold hover:bg-primary transition-all group/btn"
                                            >
                                                Continue Learning
                                                <ChevronRight className="h-4 w-4 group-hover:translate-x-1 transition-transform" />
                                            </Link>
                                        </div>
                                    </div>
                                </div>
                            );
                        })}

                        {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>
            </div>
        </AltivateLayout>
    );
}
