'use client';

import React, { useState, Suspense } from 'react';
import Link from 'next/link';
import { useRouter, useSearchParams } from 'next/navigation';
import { Mail, Lock, LogIn, ShieldCheck, ArrowRight, CheckCircle2 } from 'lucide-react';
import { useAuth } from '@/context/AuthContext';

function LoginForm() {
  const router = useRouter();
  const searchParams = useSearchParams();
  const redirectParam = searchParams.get('redirect') || '';
  const { login } = useAuth();
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const [role, setRole] = useState<'user' | 'admin'>('user');

  const handleSubmit = (e: React.FormEvent) => {
    e.preventDefault();
    login(email.trim(), role);
    if (redirectParam) {
      router.push(redirectParam);
    } else if (role === 'admin' || email.includes('admin')) {
      router.push('/admin/dashboard');
    } else {
      router.push('/user/dashboard');
    }
  };

  const handleQuickAdmin = () => {
    login('admin@hostneko.com', 'admin');
    if (redirectParam) {
      router.push(redirectParam);
    } else {
      router.push('/admin/dashboard');
    }
  };

  const handleQuickTeamRafi = () => {
    login('teamrafi69@gmail.com', 'user');
    if (redirectParam) {
      router.push(redirectParam);
    } else {
      router.push('/user/dashboard');
    }
  };

  return (
    <div className="w-full max-w-md bg-white rounded-3xl p-8 sm:p-10 shadow-xl border border-gray-100 space-y-6">
      {/* Header */}
      <div className="text-center space-y-2">
        <div className="w-12 h-12 bg-indigo-100 text-indigo-600 rounded-2xl flex items-center justify-center mx-auto shadow-sm">
          <LogIn className="w-6 h-6" />
        </div>
        <h1 className="text-2xl font-black text-gray-900">Sign In to HN HostNeko</h1>
        <p className="text-xs text-gray-500">Access your purchased scripts, license keys, and downloads.</p>
      </div>

      {/* Standard Form */}
      <form onSubmit={handleSubmit} className="space-y-4">
        <div>
          <label className="block text-xs font-bold text-gray-700 mb-1">Email Address</label>
          <div className="relative">
            <input
              type="email"
              required
              placeholder="name@example.com"
              value={email}
              onChange={(e) => setEmail(e.target.value)}
              className="w-full text-xs sm:text-sm py-2.5 pl-10 pr-4 bg-gray-50 border border-gray-200 rounded-xl focus:bg-white focus:outline-none focus:ring-2 focus:ring-indigo-600"
            />
            <Mail className="w-4 h-4 text-gray-400 absolute left-3.5 top-1/2 -translate-y-1/2" />
          </div>
        </div>

        <div>
          <label className="block text-xs font-bold text-gray-700 mb-1">Password</label>
          <div className="relative">
            <input
              type="password"
              required
              placeholder="••••••••"
              value={password}
              onChange={(e) => setPassword(e.target.value)}
              className="w-full text-xs sm:text-sm py-2.5 pl-10 pr-4 bg-gray-50 border border-gray-200 rounded-xl focus:bg-white focus:outline-none focus:ring-2 focus:ring-indigo-600"
            />
            <Lock className="w-4 h-4 text-gray-400 absolute left-3.5 top-1/2 -translate-y-1/2" />
          </div>
        </div>

        {/* Role Select */}
        <div className="flex items-center justify-between text-xs pt-1">
          <label className="flex items-center gap-2 text-gray-600 cursor-pointer">
            <input
              type="checkbox"
              checked={role === 'admin'}
              onChange={(e) => setRole(e.target.checked ? 'admin' : 'user')}
              className="rounded text-indigo-600"
            />
            <span>Log in as Administrator</span>
          </label>
          <Link href="#" className="text-indigo-600 hover:underline">
            Forgot?
          </Link>
        </div>

        <button
          type="submit"
          className="w-full py-3 px-4 bg-indigo-600 hover:bg-indigo-700 text-white rounded-xl font-bold text-sm shadow-md shadow-indigo-600/20 transition flex items-center justify-center gap-2 active:scale-95"
        >
          <span>Sign In</span>
          <ArrowRight className="w-4 h-4" />
        </button>
      </form>

      {/* Footer link */}
      <div className="text-center pt-2 border-t border-gray-100">
        <p className="text-xs text-gray-500">
          Don&apos;t have an account?{' '}
          <Link
            href={`/register${redirectParam ? `?redirect=${encodeURIComponent(redirectParam)}` : ''}`}
            className="text-indigo-600 font-bold hover:underline"
          >
            Create an Account
          </Link>
        </p>
      </div>
    </div>
  );
}

export default function LoginPage() {
  return (
    <div className="py-12 md:py-20 bg-gray-50 min-h-[85vh] flex items-center justify-center px-4">
      <Suspense fallback={<div className="text-sm text-gray-400 font-medium">Loading login...</div>}>
        <LoginForm />
      </Suspense>
    </div>
  );
}
