import { Head, Link } from '@inertiajs/react';
import { Calendar, Clock, Radio, Users } from 'lucide-react';
import AppLayout from '@/layouts/app-layout';
import type { BreadcrumbItem } from '@/types';

type LiveClass = {
    id: number;
    title: string;
    description: string | null;
    scheduled_at: string;
    duration_minutes: number;
    is_published: boolean;
    instructor: { id: number; name: string } | null;
    course: { id: number; title: string; slug: string } | null;
};

type Props = { liveClasses: LiveClass[] };

const breadcrumbs: BreadcrumbItem[] = [{ title: 'Live Class', href: '/live-classes' }];

function formatSchedule(dateStr: string) {
    const d = new Date(dateStr);
    return {
        date: d.toLocaleDateString('id-ID', { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' }),
        time: d.toLocaleTimeString('id-ID', { hour: '2-digit', minute: '2-digit' }),
    };
}

function isUpcoming(dateStr: string) {
    return new Date(dateStr) > new Date();
}

export default function LiveClassesIndex({ liveClasses }: Props) {
    const upcoming = liveClasses.filter((lc) => isUpcoming(lc.scheduled_at));
    const past = liveClasses.filter((lc) => !isUpcoming(lc.scheduled_at));

    return (
        <>
            <Head title="Live Class" />

            <div className="mx-auto max-w-6xl flex flex-col gap-6 px-5 py-8">
                <div className="flex items-center gap-3">
                    <div className="flex h-9 w-9 items-center justify-center rounded-lg bg-red-500/10">
                        <Radio className="h-5 w-5 text-red-500" />
                    </div>
                    <div>
                        <h1 className="text-xl font-bold">Live Class</h1>
                        <p className="text-sm text-muted-foreground">Sesi langsung bersama instruktur</p>
                    </div>
                </div>

                {liveClasses.length === 0 ? (
                    <div className="rounded-xl border border-dashed p-12 text-center">
                        <Radio className="mx-auto mb-3 h-10 w-10 text-muted-foreground/40" />
                        <p className="text-muted-foreground">Belum ada jadwal live class.</p>
                    </div>
                ) : (
                    <div className="flex flex-col gap-8">
                        {upcoming.length > 0 && (
                            <section>
                                <h2 className="mb-3 font-semibold text-sm uppercase tracking-wide text-muted-foreground">Akan Datang</h2>
                                <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
                                    {upcoming.map((lc) => (
                                        <LiveClassCard key={lc.id} liveClass={lc} />
                                    ))}
                                </div>
                            </section>
                        )}

                        {past.length > 0 && (
                            <section>
                                <h2 className="mb-3 font-semibold text-sm uppercase tracking-wide text-muted-foreground">Telah Berlalu</h2>
                                <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3 opacity-70">
                                    {past.map((lc) => (
                                        <LiveClassCard key={lc.id} liveClass={lc} past />
                                    ))}
                                </div>
                            </section>
                        )}
                    </div>
                )}
            </div>
        </>
    );
}

function LiveClassCard({ liveClass, past = false }: { liveClass: LiveClass; past?: boolean }) {
    const { date, time } = formatSchedule(liveClass.scheduled_at);

    return (
        <Link
            href={`/live-classes/${liveClass.id}`}
            className="flex flex-col gap-3 rounded-xl border bg-card p-4 transition-shadow hover:shadow-sm"
        >
            <div className="flex items-start justify-between gap-2">
                <div className="flex h-9 w-9 shrink-0 items-center justify-center rounded-lg bg-red-500/10">
                    <Radio className={`h-5 w-5 ${past ? 'text-muted-foreground' : 'text-red-500'}`} />
                </div>
                {!past && (
                    <span className="rounded-full bg-red-100 px-2 py-0.5 text-xs font-medium text-red-600 dark:bg-red-900/30 dark:text-red-400">
                        Upcoming
                    </span>
                )}
            </div>

            <div>
                <p className="font-semibold leading-tight">{liveClass.title}</p>
                {liveClass.course && (
                    <p className="mt-0.5 text-xs text-muted-foreground">{liveClass.course.title}</p>
                )}
            </div>

            <div className="mt-auto flex flex-col gap-1 text-xs text-muted-foreground">
                <div className="flex items-center gap-1.5">
                    <Calendar className="h-3.5 w-3.5" />
                    {date}
                </div>
                <div className="flex items-center gap-1.5">
                    <Clock className="h-3.5 w-3.5" />
                    {time} · {liveClass.duration_minutes} menit
                </div>
                <div className="flex items-center gap-1.5">
                    <Users className="h-3.5 w-3.5" />
                    {liveClass.instructor?.name ?? '-'}
                </div>
            </div>
        </Link>
    );
}

LiveClassesIndex.layout = (page: React.ReactNode) => <AppLayout breadcrumbs={breadcrumbs}>{page}</AppLayout>;
