'use client';

import { useState } from 'react';
import { useAuth } from '@/contexts/AuthContext';
import { useRouter } from 'next/navigation';
import { useEffect } from 'react';
import { LockClosedIcon } from '@heroicons/react/24/outline';

export default function LoginPage() {
    const [auth, setAuth] = useState('');
    const [password, setPassword] = useState('');
    const [localError, setLocalError] = useState('');

    const { login, isLoading, error, token } = useAuth();
    const router = useRouter();

    useEffect(() => {
        if (token) {
            router.push('/admin/dashboard');
        }
    }, [token, router]);

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

        if (!auth || !password) {
            setLocalError('Please fill in all fields');
            return;
        }

        try {
            await login({ auth, password });
        } catch (err) {
            // Error is handled by context
        }
    };

    return (
        <div className="min-h-screen flex items-center justify-center bg-gray-100 dark:bg-gray-950 py-12 px-4 sm:px-6 lg:px-8">
            <div className="max-w-md w-full">
                <div className="bg-white dark:bg-gray-900 rounded-2xl shadow-xl ring-1 ring-gray-200 dark:ring-gray-800 p-8 space-y-8">
                    <div>
                        <div className="mx-auto h-14 w-14 rounded-2xl bg-indigo-600 flex items-center justify-center mb-4">
                            <LockClosedIcon className="h-7 w-7 text-white" />
                        </div>
                        <h2 className="text-center text-2xl font-bold text-gray-900 dark:text-gray-100">
                            پنل ادمین اپروایجنسی
                        </h2>
                        <p className="mt-2 text-center text-sm text-gray-500 dark:text-gray-400">
                            برای ادامه وارد حساب کاربری خود شوید
                        </p>
                    </div>

                    <form className="space-y-5" onSubmit={handleSubmit}>
                        <div className="space-y-4">
                            <div>
                                <label htmlFor="auth" className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                                    ایمیل
                                </label>
                                <input
                                    id="auth"
                                    name="auth"
                                    type="text"
                                    required
                                    className="appearance-none relative block w-full px-3 py-3 border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-800 placeholder-gray-400 dark:placeholder-gray-500 text-gray-900 dark:text-gray-100 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm transition-colors"
                                    placeholder="example@email.com"
                                    value={auth}
                                    onChange={(e) => setAuth(e.target.value)}
                                    disabled={isLoading}
                                    dir="ltr"
                                />
                            </div>
                            <div>
                                <label htmlFor="password" className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                                    رمز عبور
                                </label>
                                <input
                                    id="password"
                                    name="password"
                                    type="password"
                                    required
                                    className="appearance-none relative block w-full px-3 py-3 border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-800 placeholder-gray-400 dark:placeholder-gray-500 text-gray-900 dark:text-gray-100 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm transition-colors"
                                    placeholder="••••••••"
                                    value={password}
                                    onChange={(e) => setPassword(e.target.value)}
                                    disabled={isLoading}
                                    dir="ltr"
                                />
                            </div>
                        </div>

                        {(error || localError) && (
                            <div className="rounded-lg bg-red-50 dark:bg-red-900/20 p-4">
                                <h3 className="text-sm font-medium text-red-800 dark:text-red-300 text-center">
                                    {localError || error}
                                </h3>
                            </div>
                        )}

                        <div>
                            <button
                                type="submit"
                                disabled={isLoading}
                                className="group relative w-full flex justify-center py-3 px-4 border border-transparent text-sm font-medium rounded-lg text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 dark:focus:ring-offset-gray-900 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
                            >
                                {isLoading ? 'کمی صبر کنید...' : 'ورود'}
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    );
}