'use client';
import { useEffect, useState } from 'react';
import { useSession } from 'next-auth/react';
import { useRouter } from 'next/navigation';
import { Users, Shield, Activity, UserCheck } from 'lucide-react';
import Link from 'next/link';
export default function AdminDashboard() {
const { data: session, status } = useSession();
const router = useRouter();
const [stats, setStats] = useState({
totalUsers: 0,
activeUsers: 0,
inactiveUsers: 0,
adminUsers: 0,
});
const [loading, setLoading] = useState(true);
useEffect(() => {
if (status === 'unauthenticated') {
router.push('/login');
}
}, [status, router]);
useEffect(() => {
fetchStats();
}, []);
const fetchStats = async () => {
try {
const response = await fetch('/api/admin/users');
const data = await response.json();
if (data.success) {
const users = data.users;
setStats({
totalUsers: users.length,
activeUsers: users.filter((u: any) => u.is_active).length,
inactiveUsers: users.filter((u: any) => !u.is_active).length,
adminUsers: users.filter((u: any) => u.role === 'admin').length,
});
}
} catch (error) {
console.error('Failed to fetch stats:', error);
} finally {
setLoading(false);
}
};
if (status === 'loading' || loading) {
return (
<div className="min-h-screen bg-gray-50 flex items-center justify-center">
<div className="text-gray-600">Loading...</div>
</div>
);
}
return (
<div className="min-h-screen bg-gray-50 p-8">
<div className="max-w-7xl mx-auto">
<div className="mb-8">
<div className="flex items-center gap-3 mb-2">
<Shield className="h-8 w-8 text-blue-600" />
<h1 className="text-3xl font-bold text-gray-900">Admin Portal</h1>
</div>
<p className="text-gray-600">Manage users and system settings</p>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
<div className="bg-white rounded-lg shadow p-6">
<div className="flex items-center justify-between mb-4">
<div className="h-12 w-12 rounded-full bg-blue-100 flex items-center justify-center">
<Users className="h-6 w-6 text-blue-600" />
</div>
</div>
<h3 className="text-2xl font-bold text-gray-900">{stats.totalUsers}</h3>
<p className="text-sm text-gray-600">Total Users</p>
</div>
<div className="bg-white rounded-lg shadow p-6">
<div className="flex items-center justify-between mb-4">
<div className="h-12 w-12 rounded-full bg-green-100 flex items-center justify-center">
<UserCheck className="h-6 w-6 text-green-600" />
</div>
</div>
<h3 className="text-2xl font-bold text-gray-900">{stats.activeUsers}</h3>
<p className="text-sm text-gray-600">Active Users</p>
</div>
<div className="bg-white rounded-lg shadow p-6">
<div className="flex items-center justify-between mb-4">
<div className="h-12 w-12 rounded-full bg-yellow-100 flex items-center justify-center">
<Activity className="h-6 w-6 text-yellow-600" />
</div>
</div>
<h3 className="text-2xl font-bold text-gray-900">{stats.inactiveUsers}</h3>
<p className="text-sm text-gray-600">Inactive Users</p>
</div>
<div className="bg-white rounded-lg shadow p-6">
<div className="flex items-center justify-between mb-4">
<div className="h-12 w-12 rounded-full bg-purple-100 flex items-center justify-center">
<Shield className="h-6 w-6 text-purple-600" />
</div>
</div>
<h3 className="text-2xl font-bold text-gray-900">{stats.adminUsers}</h3>
<p className="text-sm text-gray-600">Admin Users</p>
</div>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
<Link
href="/admin/users"
className="bg-white rounded-lg shadow p-6 hover:shadow-lg transition-shadow"
>
<div className="flex items-center gap-4">
<div className="h-12 w-12 rounded-full bg-blue-100 flex items-center justify-center">
<Users className="h-6 w-6 text-blue-600" />
</div>
<div>
<h3 className="text-lg font-semibold text-gray-900">User Management</h3>
<p className="text-sm text-gray-600">View and manage all users</p>
</div>
</div>
</Link>
<Link
href="/dashboard"
className="bg-white rounded-lg shadow p-6 hover:shadow-lg transition-shadow"
>
<div className="flex items-center gap-4">
<div className="h-12 w-12 rounded-full bg-green-100 flex items-center justify-center">
<Activity className="h-6 w-6 text-green-600" />
</div>
<div>
<h3 className="text-lg font-semibold text-gray-900">Back to Dashboard</h3>
<p className="text-sm text-gray-600">Return to main application</p>
</div>
</div>
</Link>
</div>
</div>
</div>
);
}