import { registerWidgetFetcher } from '../registry.js';
import { query } from '@zen/core/database';
import { fail } from '@zen/core/shared/logger';

registerWidgetFetcher('users', async () => {
  try {
    const result = await query(`
      SELECT
        COUNT(*) AS total,
        COUNT(CASE WHEN created_at >= NOW() - INTERVAL '30 days' THEN 1 END) AS new_this_month
      FROM zen_auth_users
    `);
    return {
      totalUsers: parseInt(result.rows[0].total) || 0,
      newThisMonth: parseInt(result.rows[0].new_this_month) || 0,
    };
  } catch (error) {
    fail(`Users widget data error: ${error.message}`);
    return { totalUsers: 0, newThisMonth: 0 };
  }
});
