'use strict';

/**
 * /api/user — per-user utility reads (Session 60, night2/F).
 *
 * GET /scan-meter (auth) — the Settings read meter: real usage from the
 * SAME rolling-24h store the scan limiter enforces. Metered tiers get
 * { used, limit, remaining }; unlimited tiers get { unlimited: true }
 * (the UI hides the meter).
 */

const express = require('express');
const { requireAuth } = require('../middleware/auth');
const { scanUsage } = require('../middleware/scanLimit');

const router = express.Router();

router.get('/scan-meter', requireAuth, (req, res) => {
  try {
    return res.json(scanUsage(req));
  } catch (err) {
    console.error('[user/scan-meter]', err.message);
    return res.status(200).json({ tier: req.user?.tier || 'free', unlimited: false, used: null, limit: null });
  }
});

module.exports = router;
