import { Context, Next } from 'hono';
import { AuthContext } from '../types/context';

export async function adminMiddleware(c: Context<AuthContext>, next: Next) {
  try {
    const user = c.get('user');

    if (!user) {
      return c.json({ message: 'Authentication required' }, 401);
    }

    // 检查用户是否有管理员角色
    const isAdmin = user.roles?.some((role: any) =>
      role.name === 'admin' || role.name === 'administrator'
    );

    if (!isAdmin) {
      return c.json({ message: 'Admin access required' }, 403);
    }

    await next();
  } catch (error) {
    console.error('Admin middleware error:', error);
    return c.json({ message: 'Admin verification failed' }, 403);
  }
}