import React from 'react';
import { Head, Link, usePage } from '@inertiajs/react';
import { Trophy, Globe, Target, BookOpen, ArrowLeft } from 'lucide-react';
import StudentLayout from '@/layouts/StudentLayout';
import { cn } from '@/lib/utils';
import type { PageProps } from '@/types';

interface GlobalEntry {
    rank: number;
    student_id: number;
    student_name: string;
    total_points: number;
    quiz_count: number;
    average_grade: number;
    is_current_user: boolean;
}

interface GlobalLeaderboardProps extends PageProps {
    leaderboard: GlobalEntry[];
}

export default function GlobalLeaderboard() {
    const { leaderboard } = usePage<GlobalLeaderboardProps>().props;

    const getRankBadge = (rank: number) => {
        if (rank === 1) return 'bg-yellow-500/20 text-yellow-400 border-yellow-500/30';
        if (rank === 2) return 'bg-slate-400/20 text-slate-300 border-slate-400/30';
        if (rank === 3) return 'bg-amber-700/20 text-amber-600 border-amber-700/30';
        return 'bg-surface border-border/50 text-text-muted';
    };

    return (
        <>
            <Head title="Leaderboard Global" />

            <div className="mx-auto max-w-4xl space-y-6">
                {/* Back */}
                <Link
                    href="/dashboard"
                    className="inline-flex items-center gap-1 text-sm text-text-muted hover:text-text transition-colors"
                >
                    <ArrowLeft className="h-4 w-4" /> Kembali
                </Link>

                {/* Header */}
                <div className="glass rounded-2xl p-8 border border-border/40 text-center">
                    <div className="inline-flex items-center justify-center w-16 h-16 rounded-full bg-emerald-500/20 mb-4">
                        <Globe className="h-8 w-8 text-emerald-400" />
                    </div>
                    <h1 className="text-2xl font-black text-text-light">Leaderboard Global</h1>
                    <p className="text-sm text-text-muted mt-2">
                        Peringkat berdasarkan total poin dari semua quiz
                    </p>
                </div>

                {/* Leaderboard Table */}
                <div className="glass rounded-2xl border border-border/40 overflow-hidden">
                    <div className="overflow-x-auto">
                        <table className="w-full text-sm">
                            <thead className="bg-surface-hover/30 text-xs uppercase text-text-muted">
                                <tr>
                                    <th className="px-6 py-4 text-left">Peringkat</th>
                                    <th className="px-6 py-4 text-left">Nama Siswa</th>
                                    <th className="px-6 py-4 text-center">Quiz</th>
                                    <th className="px-6 py-4 text-center">Rata-rata</th>
                                    <th className="px-6 py-4 text-right">Total Poin</th>
                                </tr>
                            </thead>
                            <tbody className="divide-y divide-border/40">
                                {leaderboard.length > 0 ? (
                                    leaderboard.map((entry) => (
                                        <tr
                                            key={entry.student_id}
                                            className={cn(
                                                'hover:bg-surface-hover/20 transition-colors',
                                                entry.is_current_user && 'bg-emerald-500/5'
                                            )}
                                        >
                                            <td className="px-6 py-4">
                                                <div
                                                    className={cn(
                                                        'inline-flex items-center justify-center w-8 h-8 rounded-full border text-xs font-bold',
                                                        getRankBadge(entry.rank)
                                                    )}
                                                >
                                                    {entry.rank}
                                                </div>
                                            </td>
                                            <td className="px-6 py-4">
                                                <p className={cn('font-medium', entry.is_current_user ? 'text-emerald-400' : 'text-text-light')}>
                                                    {entry.student_name}
                                                    {entry.is_current_user && (
                                                        <span className="ml-2 text-xs text-emerald-500">(Kamu)</span>
                                                    )}
                                                </p>
                                            </td>
                                            <td className="px-6 py-4 text-center">
                                                <span className="inline-flex items-center gap-1 text-text-muted">
                                                    <BookOpen className="h-3.5 w-3.5" />
                                                    {entry.quiz_count}
                                                </span>
                                            </td>
                                            <td className="px-6 py-4 text-center">
                                                <span className="inline-flex items-center gap-1 text-text-muted">
                                                    <Target className="h-3.5 w-3.5" />
                                                    {entry.average_grade}%
                                                </span>
                                            </td>
                                            <td className="px-6 py-4 text-right">
                                                <span className="font-bold text-emerald-400">
                                                    {entry.total_points.toLocaleString()}
                                                </span>
                                            </td>
                                        </tr>
                                    ))
                                ) : (
                                    <tr>
                                        <td colSpan={5} className="px-6 py-12 text-center text-text-muted">
                                            <Trophy className="mx-auto h-12 w-12 opacity-20 mb-3" />
                                            <p>Belum ada data leaderboard.</p>
                                        </td>
                                    </tr>
                                )}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </>
    );
}

GlobalLeaderboard.layout = (page: any) => <StudentLayout title="Leaderboard Global">{page}</StudentLayout>;
