import React from 'react';
import { Head, Link, useForm } from '@inertiajs/react';
import { Play, ArrowLeft, AlertCircle } from 'lucide-react';
import GameLayout from '@/layouts/GameLayout';

export default function LiveEnterPin({ game }: any) {
    const { data, setData, post, processing, errors } = useForm({
        game_pin: '',
    });

    const submit = (e: React.FormEvent) => {
        e.preventDefault();
        post(route('arena.join-live', game.id));
    };

    return (
        <GameLayout title={`Gabung - ${game.title}`}>
            <Head title={`Gabung - ${game.title}`} />
            <div className="h-full flex flex-col items-center justify-center p-6 relative">                <div className="z-10 w-full max-w-md flex flex-col items-center gap-6">
                    <div className="text-center space-y-2">
                        <h2 className="text-xl text-primary-400 font-bold uppercase tracking-wider">Live Arena</h2>
                        <h1 className="text-3xl font-black text-text-light">{game.title}</h1>
                    </div>

                    <div className="glass p-8 rounded-3xl border border-primary-500/30 w-full shadow-[0_0_30px_rgba(var(--color-primary-500),0.1)]">
                        <form onSubmit={submit} className="flex flex-col gap-6">
                            <div>
                                <label htmlFor="game_pin" className="block text-sm font-medium text-text-muted mb-2 text-center">
                                    Masukkan PIN Game dari layar Guru
                                </label>
                                <input
                                    id="game_pin"
                                    type="text"
                                    name="game_pin"
                                    value={data.game_pin}
                                    onChange={(e) => setData('game_pin', e.target.value.toUpperCase())}
                                    className="w-full bg-surface-dark/50 border border-border/50 text-white rounded-xl px-4 py-4 text-center text-4xl font-black tracking-widest focus:ring-2 focus:ring-primary-500/50 focus:border-primary-500/50 uppercase placeholder:text-text-muted/30"
                                    placeholder="XXXXXX"
                                    maxLength={6}
                                    required
                                    autoComplete="off"
                                />
                                {errors.game_pin && (
                                    <div className="mt-3 flex items-center gap-1.5 text-sm text-red-400 bg-red-400/10 px-3 py-2 rounded-lg justify-center">
                                        <AlertCircle className="w-4 h-4" />
                                        {errors.game_pin}
                                    </div>
                                )}
                            </div>

                            <button 
                                type="submit"
                                disabled={processing || data.game_pin.length < 6}
                                className="btn-primary w-full rounded-xl py-4 text-lg font-bold shadow-[0_0_20px_rgba(var(--color-primary-500),0.3)] disabled:opacity-50 disabled:cursor-not-allowed transition-all"
                            >
                                {processing ? 'Menghubungkan...' : 'Gabung Sekarang'}
                            </button>
                        </form>
                    </div>

                    <Link href={route('games.index')} className="text-text-muted hover:text-white transition-colors flex items-center gap-1 mt-4">
                        <ArrowLeft className="w-4 h-4" />
                        Kembali
                    </Link>
                </div>
            </div>
        </GameLayout>
    );
}
