import { auth } from '../../../../auth';
import { redirect } from 'next/navigation';
import { fetchJson } from '../../../lib/api';
import UserAdminClient from './UserAdminClient';

type User = {
  id: number;
  username: string;
  email: string;
  firstName: string | null;
  lastName: string | null;
  role: string;
  createdAt: string;
};

export default async function AdminUsersPage() {
  const session = await auth();
  if (!session || (session.user as any)?.role !== 'admin') {
    redirect('/');
  }

  const users = await fetchJson<User[]>('/api/users');

  return (
    <main className="p-6 max-w-4xl mx-auto">
      <h1 className="text-2xl font-bold mb-6">Användarhantering</h1>
      <UserAdminClient users={users} currentUserId={session.user.id} />
    </main>
  );
}
