// src/app/admin/users/page.tsx
import { requireAuthOrRedirect } from '@/lib/authServer';
import axiosServer from '@/lib/axiosServer';
import UserTableSection from '@/components/admin/UserTableSection';


interface UserDTO {
  id: number;
  username: string;
  displayName: string;
  firstName: string;
  lastName: string;
  email: string;
  role: string;
  clientName: string;
  clientId?: number;
  enabled: boolean;
}

export default async function UserManagementPage() {
    const user = await requireAuthOrRedirect();

    try {
      const res = await axiosServer.get('/admin/users', {
        headers: {
          Authorization: `Bearer ${user.token}`,
        },
      });

      const users: UserDTO[] = res.data;

      return <UserTableSection initialUsers={users} />;
    } catch (error) {
      console.error('Failed to fetch users:', error);
      throw new Error(`Failed to load users: ${error instanceof Error ? error.message : 'Unknown error'}`);
    }
  }
