export function requireAuth(req, res, next) {
  if (!req.session?.userId) {
    return res.status(401).json({ error: 'Authentication required' });
  }
  next();
}

export function requireAdmin(req, res, next) {
  if (!req.session?.userId) {
    return res.status(401).json({ error: 'Authentication required' });
  }
  if (req.session.role !== 'admin') {
    return res.status(403).json({ error: 'Admin access required' });
  }
  next();
}

export function publicUser(user) {
  return {
    id: user.id,
    username: user.username,
    balance: user.balance == null ? '0' : user.balance.toString(),
    osuBalance: user.osuBalance ?? 0,
    prBalance: user.prBalance == null ? '0' : String(user.prBalance),
    lastAdAt: user.lastAdAt || null,
    role: user.role,
    avatarUrl: user.avatarUrl || '',
    bio: user.bio || '',
    autoSellEnabled: Boolean(user.autoSellEnabled),
    autoSellThresholdCents:
      user.autoSellThresholdCents == null
        ? '1000'
        : user.autoSellThresholdCents.toString(),
    prestigeCount: user.prestigeCount ?? 0,
    prestigeChanceBonus: user.prestigeChanceBonus ?? 0,
    prestigeGainBonus: user.prestigeGainBonus ?? 0,
    prestigeWearBonus: user.prestigeWearBonus ?? 0,
    prestigeOpenBonus: user.prestigeOpenBonus ?? 0,
    prestigePrBonus: user.prestigePrBonus ?? 0,
    prestigeQualityBonus: user.prestigeQualityBonus ?? 0,
    prestigeAnimReduction: user.prestigeAnimReduction ?? 0,
    prestigeRefundChance: user.prestigeRefundChance ?? 0,
    prestigeRespinChance: user.prestigeRespinChance ?? 0,
    prestigeVaultSlots: user.prestigeVaultSlots ?? 0,
    prestigeOfflineYield: user.prestigeOfflineYield ?? 0,
    prestigeOnlineYield: user.prestigeOnlineYield ?? 0,
    lastConnection: user.lastConnection || null,
    playTimeSeconds: Number(user.playTimeSeconds ?? 0),
  };
}
