import { useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { useTranslation } from 'react-i18next'
import { useAuth } from '../hooks/useAuth'
import { GameButton } from './common/GameButton'
import './Login.css'

function Register() {
    const [email, setEmail] = useState('')
    const [password, setPassword] = useState('')
    const [confirmPassword, setConfirmPassword] = useState('')
    const [error, setError] = useState('')
    const [loading, setLoading] = useState(false)
    const { register } = useAuth()
    const navigate = useNavigate()
    const { t } = useTranslation()

    const validateEmail = (email: string) => {
        const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
        return re.test(email)
    }

    const getPasswordStrength = (password: string): { strength: string; color: string } => {
        if (password.length === 0) return { strength: '', color: '' }
        if (password.length < 6) return { strength: t('auth.strength.weak'), color: '#ff6b6b' }
        if (password.length < 10) return { strength: t('auth.strength.medium'), color: '#ffd93d' }
        return { strength: t('auth.strength.strong'), color: '#51cf66' }
    }

    const passwordStrength = getPasswordStrength(password)

    const handleSubmit = async (e: React.FormEvent) => {
        e.preventDefault()
        setError('')

        // Validation
        if (!validateEmail(email)) {
            setError(t('auth.errors.invalidEmail'))
            return
        }

        if (password.length < 6) {
            setError(t('auth.errors.passwordLength'))
            return
        }

        if (password !== confirmPassword) {
            setError(t('auth.errors.passwordMatch'))
            return
        }

        setLoading(true)

        try {
            await register(email, password)
            // Navigate to character selection after successful registration
            navigate('/characters')
        } catch (err: any) {
            setError(err.response?.data?.detail || t('auth.errors.registrationFailed'))
        } finally {
            setLoading(false)
        }
    }

    return (
        <div className="login-container">
            <div className="login-card game-panel">
                <h1 className="auth-title">{t('auth.registerTitle')}</h1>
                <p className="login-subtitle">{t('auth.registerSubtitle')}</p>

                <form onSubmit={handleSubmit} className="auth-form">
                    <div className="form-group">
                        <label htmlFor="email">{t('auth.email')}</label>
                        <input
                            type="email"
                            id="email"
                            className="game-input"
                            value={email}
                            onChange={(e) => setEmail(e.target.value)}
                            placeholder={t('auth.emailPlaceholder')}
                            required
                            disabled={loading}
                            autoComplete="email"
                        />
                    </div>

                    <div className="form-group">
                        <label htmlFor="password">{t('auth.password')}</label>
                        <input
                            type="password"
                            id="password"
                            className="game-input"
                            value={password}
                            onChange={(e) => setPassword(e.target.value)}
                            placeholder={t('auth.passwordPlaceholder')}
                            required
                            disabled={loading}
                            autoComplete="new-password"
                        />
                        {password && (
                            <div className="password-strength">
                                <span style={{ color: passwordStrength.color }}>
                                    {passwordStrength.strength}
                                </span>
                            </div>
                        )}
                    </div>

                    <div className="form-group">
                        <label htmlFor="confirmPassword">{t('auth.confirmPassword')}</label>
                        <input
                            type="password"
                            id="confirmPassword"
                            className="game-input"
                            value={confirmPassword}
                            onChange={(e) => setConfirmPassword(e.target.value)}
                            placeholder={t('auth.confirmPasswordPlaceholder')}
                            required
                            disabled={loading}
                            autoComplete="new-password"
                        />
                    </div>

                    {error && <div className="error-message">{error}</div>}

                    <GameButton
                        variant="primary"
                        size="lg"
                        className="auth-submit"
                        disabled={loading}
                        onClick={() => { }} // Form will handle it
                    >
                        {loading ? t('auth.submitting') : t('auth.submit')}
                    </GameButton>
                </form>

                <div className="login-toggle">
                    <GameButton
                        variant="secondary"
                        size="sm"
                        onClick={() => navigate('/login')}
                        disabled={loading}
                    >
                        {t('auth.loginLink')}
                    </GameButton>
                </div>


            </div>
        </div>
    )
}


export default Register
