'use client';

import { useState } from 'react';
import { Shield, Mail, Lock, ArrowRight, Zap } from 'lucide-react';
import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
import { Label } from '@/components/ui/label';

interface AdminUser { id: string; name: string; email: string; role: string; }

export function AdminLogin({ onLogin }: { onLogin: (admin: AdminUser) => void }) {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const [isLoading, setIsLoading] = useState(false);
  const [error, setError] = useState('');

  const doLogin = async (e: string, p: string) => {
    if (!e || !p) return;
    setIsLoading(true); setError('');
    try {
      const res = await fetch('/api/admin/auth', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ email: e, password: p })
      });
      const data = await res.json();
      if (data.success) onLogin(data.admin);
      else setError(data.error || 'Invalid credentials');
    } catch { setError('Network error. Please try again.'); }
    finally { setIsLoading(false); }
  };

  return (
    <div className="flex min-h-screen items-center justify-center bg-gradient-to-br from-slate-950 via-slate-900 to-slate-950 p-4">
      <div className="pointer-events-none fixed inset-0 overflow-hidden">
        <div className="absolute -left-40 -top-40 h-80 w-80 rounded-full bg-orange-500/10 blur-3xl animate-pulse" />
        <div className="absolute -bottom-40 -right-40 h-80 w-80 rounded-full bg-amber-500/10 blur-3xl animate-pulse" style={{ animationDelay: '1s' }} />
        <div className="absolute left-1/2 top-1/2 h-60 w-60 -translate-x-1/2 -translate-y-1/2 rounded-full bg-orange-600/5 blur-3xl animate-pulse" style={{ animationDelay: '2s' }} />
      </div>
      <div className="relative w-full max-w-md">
        <div className="mb-6 text-center">
          <div className="mx-auto mb-4 flex h-16 w-16 items-center justify-center rounded-2xl bg-gradient-to-br from-orange-500 to-amber-500 shadow-lg shadow-orange-500/25">
            <Shield className="h-8 w-8 text-white" strokeWidth={1.5} />
          </div>
          <h1 className="text-2xl font-bold text-white">Needyfy Admin</h1>
          <p className="mt-1 text-sm text-slate-400">Super Admin Control Panel</p>
        </div>
        <div className="rounded-2xl border border-white/10 bg-white/5 p-6 shadow-2xl backdrop-blur-xl">
          <form onSubmit={(e) => { e.preventDefault(); doLogin(email, password); }} className="space-y-4">
            {error && <div className="rounded-lg border border-red-500/20 bg-red-500/10 p-3 text-center text-sm text-red-400">{error}</div>}
            <div className="space-y-2">
              <Label className="text-xs font-medium text-slate-300">Email Address</Label>
              <div className="relative">
                <Mail className="absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-slate-500" />
                <Input type="email" placeholder="admin@needyfy.com" value={email} onChange={(e) => setEmail(e.target.value)} className="border-white/10 bg-white/5 pl-9 text-white placeholder:text-slate-600 focus:border-orange-500/50 focus:ring-orange-500/20" required />
              </div>
            </div>
            <div className="space-y-2">
              <Label className="text-xs font-medium text-slate-300">Password</Label>
              <div className="relative">
                <Lock className="absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-slate-500" />
                <Input type="password" placeholder="Enter password" value={password} onChange={(e) => setPassword(e.target.value)} className="border-white/10 bg-white/5 pl-9 text-white placeholder:text-slate-600 focus:border-orange-500/50 focus:ring-orange-500/20" required />
              </div>
            </div>
            <Button type="submit" className="w-full bg-gradient-to-r from-orange-600 to-amber-500 font-semibold shadow-lg shadow-orange-500/25" size="lg" disabled={isLoading}>
              {isLoading ? <span className="flex items-center gap-2"><span className="h-4 w-4 animate-spin rounded-full border-2 border-white border-t-transparent" />Signing in...</span> : <span className="flex items-center gap-2"><ArrowRight className="h-4 w-4" />Sign In</span>}
            </Button>
            <button type="button" onClick={() => { setEmail('admin@needyfy.com'); setPassword('admin123'); doLogin('admin@needyfy.com', 'admin123'); }} className="flex w-full items-center justify-center gap-2 rounded-lg border border-dashed border-white/20 bg-white/5 px-4 py-2.5 text-sm text-slate-400 transition-colors hover:border-orange-500/40 hover:bg-orange-500/10 hover:text-orange-400">
              <Zap className="h-3.5 w-3.5" />Quick Demo Login
            </button>
          </form>
        </div>
        <p className="mt-4 text-center text-xs text-slate-600">Needyfy Super Admin &middot; Protected Access</p>
      </div>
    </div>
  );
}
