const express = require('express');
const db = require('../db');
const { authenticate, requireAdmin } = require('../middleware/auth');

const router = express.Router();

router.get('/', async (req, res) => {
  try {
    const [customers, suppliers, projects, paymentNodes, paymentRecords] = await Promise.all([
      db.query('SELECT COUNT(*) as count FROM customers'),
      db.query('SELECT COUNT(*) as count FROM suppliers'),
      db.query('SELECT COUNT(*) as count FROM projects'),
      db.query('SELECT COUNT(*) as count FROM payment_nodes'),
      db.query('SELECT COUNT(*) as count FROM payment_records')
    ]);

    res.json({
      success: true,
      data: {
        summary: {
          customers: parseInt(customers.rows[0].count) || 0,
          suppliers: parseInt(suppliers.rows[0].count) || 0,
          projects: parseInt(projects.rows[0].count) || 0,
          payment_nodes: parseInt(paymentNodes.rows[0].count) || 0,
          payment_records: parseInt(paymentRecords.rows[0].count) || 0
        },
        timestamp: new Date().toISOString()
      }
    });
  } catch (error) {
    res.json({ success: false, message: '获取财务统计失败' });
  }
});

module.exports = router;