import { Link, Head } from '@inertiajs/react';
import { Gamepad2, Calendar, ArrowRight, Play, Timer, Trophy } from 'lucide-react';
import StudentLayout from '@/layouts/StudentLayout';
import LearningContentFilters from '@/components/LearningContentFilters';
import type { PageProps } from '@/types';

interface Game {
    id: number;
    title: string;
    created_at: string;
    game_mode: string;
    subject: { id: number; name: string } | null;
    teacher: { id: number; name: string } | null;
    settings: { timer_seconds: number };
}

interface Props extends PageProps {
    games: Game[];
    subjects: Array<{ id: number; name: string }>;
    selectedSubject: number | null;
}

export default function GamesList({ games, subjects, selectedSubject }: Props) {
    return (
        <>
            <Head title="Quiz Arena" />

            <div className="space-y-6">
                <div className="flex items-center justify-between">
                    <div>
                        <h2 className="text-lg font-bold flex items-center gap-2">
                            <Gamepad2 className="h-6 w-6 text-emerald-400" />
                            Quiz Arena
                        </h2>
                        <p className="text-sm text-text-muted mt-1">Selesaikan quiz gamifikasi dan kalahkan rekor tertinggimu!</p>
                    </div>
                </div>

                <LearningContentFilters baseUrl="/games" subjects={subjects} selectedSubject={selectedSubject} showPeriod={false} />
                <p className="text-xs text-text-muted" role="status">{games.length} quiz ditampilkan</p>

                {games.length > 0 ? (
                    <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
                        {games.map((game) => (
                            <div 
                                key={game.id} 
                                className="glass group flex flex-col justify-between rounded-2xl p-5 hover:scale-[1.01] transition-all border border-border/40"
                            >
                                <div className="space-y-4">
                                    {/* Icon & Subject */}
                                    <div className="flex items-center justify-between">
                                        <div className="flex h-10 w-10 items-center justify-center rounded-xl bg-emerald-500/10 text-emerald-400">
                                            <Gamepad2 className="h-5 w-5" />
                                        </div>
                                        <span className="inline-flex items-center rounded-md bg-emerald-500/10 px-2.5 py-0.5 text-xs font-medium text-emerald-400 ring-1 ring-inset ring-emerald-500/20">
                                            {game.subject?.name ?? 'Mata Pelajaran'}
                                        </span>
                                    </div>

                                    {/* Title & Info */}
                                    <div>
                                        <h3 className="font-bold text-text-light group-hover:text-emerald-400 transition-colors line-clamp-2">
                                            {game.title}
                                        </h3>
                                        <div className="flex items-center gap-2 mt-1.5 flex-wrap">
                                            {game.game_mode === 'faction' && (
                                                <span className="inline-flex items-center rounded-md bg-blue-500/10 px-2 py-0.5 text-[10px] font-medium text-blue-400 ring-1 ring-inset ring-blue-500/20">
                                                    ⚔️ Faction War
                                                </span>
                                            )}
                                            {game.game_mode === 'live' && (
                                                <span className="inline-flex items-center rounded-md bg-orange-500/10 px-2 py-0.5 text-[10px] font-medium text-orange-400 ring-1 ring-inset ring-orange-500/20">
                                                    🔥 Live
                                                </span>
                                            )}
                                            {game.game_mode === 'team_live' && (
                                                <span className="inline-flex items-center rounded-md bg-emerald-500/10 px-2 py-0.5 text-[10px] font-medium text-emerald-400 ring-1 ring-inset ring-emerald-500/20">
                                                    Team Quest · 1 HP/Kelompok
                                                </span>
                                            )}
                                            <p className="text-xs text-text-muted flex items-center gap-1">
                                                <Timer className="h-3.5 w-3.5 text-amber-400" /> {game.settings?.timer_seconds ?? 60} dtk/soal
                                            </p>
                                        </div>
                                    </div>
                                </div>

                                {/* Bottom Info */}
                                <div className="mt-5 border-t border-border/30 pt-4 flex items-center justify-between">
                                    <div className="space-y-1">
                                        <p className="text-[10px] text-text-muted flex items-center gap-1">
                                            <Calendar className="h-3 w-3" /> {new Date(game.created_at).toLocaleDateString('id-ID', { day: 'numeric', month: 'short', year: 'numeric' })}
                                        </p>
                                        <p className="text-[10px] text-text-muted flex items-center gap-1">
                                            Oleh: {game.teacher?.name ?? '-'}
                                        </p>
                                    </div>

                                    <Link
                                        href={game.game_mode === 'team_live' ? route('team-quest.join.form') : route('arena.lobby', game.id)}
                                        className="flex items-center gap-2 rounded-lg bg-emerald-500/20 text-emerald-400 px-3 py-2 text-sm font-semibold hover:bg-emerald-500 hover:text-white transition-colors"
                                        title={game.game_mode === 'team_live' ? 'Gabung Team Quest' : 'Masuk Arena'}
                                    >
                                        <Play className="h-4 w-4" fill="currentColor" /> Main
                                    </Link>
                                </div>
                            </div>
                        ))}
                    </div>
                ) : (
                    <div className="glass rounded-2xl p-12 text-center text-text-muted py-20">
                        <Trophy className="mb-3 h-12 w-12 opacity-30 mx-auto" />
                        <h4 className="font-semibold text-lg text-text-light">{selectedSubject ? 'Tidak Ada Quiz yang Sesuai' : 'Belum Ada Quiz'}</h4>
                        <p className="text-sm mt-1">{selectedSubject ? 'Tidak ada quiz untuk mata pelajaran ini. Pilih mapel lain atau Semua Mata Pelajaran.' : 'Belum ada quiz yang diterbitkan untuk kelas Anda saat ini.'}</p>
                    </div>
                )}
            </div>
        </>
    );
}

GamesList.layout = (page: any) => <StudentLayout title="Quiz Arena">{page}</StudentLayout>;
