import React from 'react';
import { Head, useForm } from '@inertiajs/react';
import AuthLayout from '@/Layouts/AuthLayout';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/Components/ui/card";
import { Input } from "@/Components/ui/input";
import { Label } from "@/Components/ui/label";
import { Button } from "@/Components/ui/button";
import { ShieldCheck, Loader2 } from "lucide-react";
import { useTranslation } from '@/hooks/useTranslation';

export default function TwoFactorChallenge() {
    const { t } = useTranslation();
    const { data, setData, post, processing, errors } = useForm({
        code: '',
    });

    const submit = (e) => {
        e.preventDefault();
        post(route('two-factor.login'));
    };

    return (
        <AuthLayout>
            <Head title={t('Two-Factor Authentication')} />

            <Card className="w-full border-border/50 shadow-lg bg-card">
                <CardHeader className="space-y-1 text-center">
                    <div className="mx-auto h-12 w-12 rounded-full bg-primary/10 flex items-center justify-center text-primary mb-2">
                        <ShieldCheck className="h-6 w-6" />
                    </div>
                    <CardTitle className="text-2xl font-bold">{t('Two-Factor Authentication')}</CardTitle>
                    <CardDescription>
                        {t('Please confirm access to your account by entering the authentication code provided by your authenticator application.')}
                    </CardDescription>
                </CardHeader>

                <CardContent>
                    <form onSubmit={submit} className="space-y-4">
                        <div className="space-y-2 text-center">
                            <Label htmlFor="code" className="text-xs uppercase tracking-widest">{t('Authentication Code')}</Label>
                            <Input
                                id="code"
                                type="text"
                                inputMode="numeric"
                                placeholder="123456"
                                value={data.code}
                                onChange={(e) => setData('code', e.target.value.replace(/[^0-9]/g, ''))}
                                className={`text-center tracking-[0.5em] font-mono text-xl h-12 ${errors.code ? "border-destructive" : ""}`}
                                autoFocus
                                required
                            />
                            {errors.code && <p className="text-xs text-destructive font-bold">{errors.code}</p>}
                        </div>

                        <Button className="w-full gap-2 text-base font-bold shadow-md mt-4" type="submit" disabled={processing || data.code.length !== 6}>
                            {processing ? <Loader2 className="w-5 h-5 animate-spin" /> : null}
                            {t('Log in')}
                        </Button>
                    </form>
                </CardContent>
            </Card>
        </AuthLayout>
    );
}