import { useState } from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { useAuth } from '@/contexts/AuthContext';
import { useLanguage } from '@/contexts/LanguageContext';
import { Apple, Eye, EyeOff } from 'lucide-react';
import { FaGoogle } from 'react-icons/fa';

const SignIn = () => {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const [showPassword, setShowPassword] = useState(false);
  const [rememberMe, setRememberMe] = useState(false);
  const [isLoading, setIsLoading] = useState(false);
  const [error, setError] = useState('');

  const { login, user } = useAuth();
  const { t } = useLanguage();
  const navigate = useNavigate();

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

    try {
      await login(email, password);
      // Decide destination based on role
      const cached = localStorage.getItem('karibeo-user');
      const u = user ?? (cached ? JSON.parse(cached) : null);
      const role = u?.role;
      if (role === 'super_admin' || role === 'admin') {
        navigate('/dashboard/admin');
      } else {
        navigate('/dashboard');
      }
    } catch (err: any) {
      setError(err.message);
    } finally {
      setIsLoading(false);
    }
  };

  const handleSocialLogin = (provider: string) => {
    // Simulate social login
    console.log(`Login with ${provider}`);
  };

  return (
    <div className="min-h-screen flex">
      {/* Left Side - Form */}
      <div className="flex-1 flex items-center justify-center p-8 bg-white">
        <div className="w-full max-w-md space-y-8">
          {/* Header */}
          <div className="text-center">
            <h1 className="text-3xl font-bold text-gray-900 mb-2">
              {t('welcomeBack')} <span className="text-primary italic">{t('signIn')}</span> {t('toContinue')}
            </h1>
            <p className="text-gray-600 text-sm leading-relaxed">
              {t('unlockContent')}
            </p>
          </div>

          {/* Social Login Buttons */}
          <div className="space-y-3">
            <Button
              variant="outline"
              onClick={() => handleSocialLogin('apple')}
              className="w-full h-12 bg-gray-900 text-white hover:bg-gray-800 border-gray-900"
            >
              <Apple className="w-5 h-5 mr-3" />
              {t('signUpWithApple')}
            </Button>
            
            <Button
              variant="outline"
              onClick={() => handleSocialLogin('google')}
              className="w-full h-12 bg-gray-100 text-gray-700 hover:bg-gray-200"
            >
              <FaGoogle className="w-5 h-5 mr-3" />
              {t('signUpWithGoogle')}
            </Button>
            
            <p className="text-xs text-gray-500 text-center">
              {t('privacyNotice')}
            </p>
          </div>

          {/* Divider */}
          <div className="relative">
            <div className="absolute inset-0 flex items-center">
              <div className="w-full border-t border-gray-300" />
            </div>
            <div className="relative flex justify-center text-sm">
              <span className="px-4 bg-white text-gray-500">Or</span>
            </div>
          </div>

          {/* Login Form */}
          <form onSubmit={handleSubmit} className="space-y-6">
            {error && (
              <div className="bg-red-50 text-red-500 p-3 rounded-lg text-sm">
                {error}
              </div>
            )}

            <div className="space-y-4">
              <div>
                <label className="block text-sm font-medium text-gray-700 mb-2">
                  {t('enterEmail')} <span className="text-red-500">*</span>
                </label>
                <Input
                  type="email"
                  value={email}
                  onChange={(e) => setEmail(e.target.value)}
                  placeholder={t('enterValidEmail')}
                  className="h-12"
                  required
                />
              </div>

              <div>
                <label className="block text-sm font-medium text-gray-700 mb-2">
                  {t('password')} <span className="text-red-500">*</span>
                </label>
                <div className="relative">
                  <Input
                    type={showPassword ? 'text' : 'password'}
                    value={password}
                    onChange={(e) => setPassword(e.target.value)}
                    placeholder={t('enterPassword')}
                    className="h-12 pr-12"
                    required
                  />
                  <button
                    type="button"
                    onClick={() => setShowPassword(!showPassword)}
                    className="absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-400 hover:text-gray-600"
                  >
                    {showPassword ? <EyeOff className="w-5 h-5" /> : <Eye className="w-5 h-5" />}
                  </button>
                </div>
              </div>

              <div className="flex items-center">
                <input
                  type="checkbox"
                  id="remember"
                  checked={rememberMe}
                  onChange={(e) => setRememberMe(e.target.checked)}
                  className="w-4 h-4 text-primary border-gray-300 rounded focus:ring-primary"
                />
                <label htmlFor="remember" className="ml-2 text-sm text-gray-700">
                  {t('rememberMe')} next time
                </label>
              </div>
            </div>

            <Button
              type="submit"
              disabled={isLoading}
              className="w-full h-12 bg-primary hover:bg-primary-dark text-white font-semibold"
            >
              {isLoading ? t('loading') : t('signIn')}
            </Button>

            <div className="text-center space-y-2">
              <p className="text-sm text-gray-600">
                Don't have an account?{' '}
                <Link to="/sign-up" className="text-primary hover:underline font-medium">
                  {t('signUp')}
                </Link>
              </p>
              <Link to="/forgot-password" className="text-sm text-primary hover:underline">
                Remind {t('password')}
              </Link>
            </div>
          </form>
        </div>
      </div>

      {/* Right Side - Image & Content */}
      <div className="hidden lg:flex flex-1 bg-gray-100 items-center justify-center p-8">
        <div className="text-center max-w-md">
          <h2 className="text-4xl font-bold text-gray-900 mb-4 leading-tight">
            Effortlessly organize your workspace with ease.
          </h2>
          <p className="text-gray-600 mb-8">
            It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.
          </p>
          <div className="bg-gray-300 rounded-lg h-64 flex items-center justify-center">
            <span className="text-6xl font-bold text-gray-500">793x552</span>
          </div>
        </div>
      </div>
    </div>
  );
};

export default SignIn;