/**
 * adminDashboardRoutes.js - Admin Dashboard API Endpoints
 * Purpose: Dashboard statistics, analytics, and metrics endpoints
 * Routes: 6 endpoints for dashboard data and charts
 */

const express = require('express');
const router = express.Router();
const DashboardService = require('../services/DashboardService');

// GET /api/admin/dashboard - Main dashboard statistics
router.get('/dashboard', async (req, res) => {
  try {
    const pool = req.app.locals.pool;
    const dashboardService = new DashboardService(pool);

    const stats = await dashboardService.getDashboardStats();

    res.json({
      success: true,
      message: 'Dashboard statistics retrieved successfully',
      data: stats
    });
  } catch (error) {
    console.error('Dashboard error:', error);
    res.status(500).json({
      success: false,
      error: 'Dashboard Error',
      message: error.message
    });
  }
});

// GET /api/admin/dashboard/users - User statistics
router.get('/dashboard/users', async (req, res) => {
  try {
    const pool = req.app.locals.pool;
    const dashboardService = new DashboardService(pool);

    const userStats = await dashboardService.getUserStats();

    res.json({
      success: true,
      message: 'User statistics retrieved successfully',
      data: userStats
    });
  } catch (error) {
    console.error('User stats error:', error);
    res.status(500).json({
      success: false,
      error: 'Stats Error',
      message: error.message
    });
  }
});

// GET /api/admin/dashboard/revenue - Revenue statistics
router.get('/dashboard/revenue', async (req, res) => {
  try {
    const pool = req.app.locals.pool;
    const dashboardService = new DashboardService(pool);

    const revenueStats = await dashboardService.getRevenueStats();
    const dailyChart = await dashboardService.getDailyRevenueChart();

    res.json({
      success: true,
      message: 'Revenue statistics retrieved successfully',
      data: {
        stats: revenueStats,
        dailyChart: dailyChart
      }
    });
  } catch (error) {
    console.error('Revenue stats error:', error);
    res.status(500).json({
      success: false,
      error: 'Stats Error',
      message: error.message
    });
  }
});

// GET /api/admin/dashboard/orders - Order statistics
router.get('/dashboard/orders', async (req, res) => {
  try {
    const pool = req.app.locals.pool;
    const dashboardService = new DashboardService(pool);

    const orderStats = await dashboardService.getOrderStats();

    res.json({
      success: true,
      message: 'Order statistics retrieved successfully',
      data: orderStats
    });
  } catch (error) {
    console.error('Order stats error:', error);
    res.status(500).json({
      success: false,
      error: 'Stats Error',
      message: error.message
    });
  }
});

// GET /api/admin/dashboard/services - Service statistics
router.get('/dashboard/services', async (req, res) => {
  try {
    const pool = req.app.locals.pool;
    const dashboardService = new DashboardService(pool);

    const serviceStats = await dashboardService.getServiceStats();
    const topServices = await dashboardService.getTopServices(10);
    const categoryDist = await dashboardService.getCategoryDistribution();

    res.json({
      success: true,
      message: 'Service statistics retrieved successfully',
      data: {
        stats: serviceStats,
        topServices: topServices,
        categoryDistribution: categoryDist
      }
    });
  } catch (error) {
    console.error('Service stats error:', error);
    res.status(500).json({
      success: false,
      error: 'Stats Error',
      message: error.message
    });
  }
});

// GET /api/admin/dashboard/growth - User growth chart
router.get('/dashboard/growth', async (req, res) => {
  try {
    const pool = req.app.locals.pool;
    const dashboardService = new DashboardService(pool);

    const growthData = await dashboardService.getUserGrowthChart();
    const platformMetrics = await dashboardService.getPlatformMetrics();

    res.json({
      success: true,
      message: 'Growth data retrieved successfully',
      data: {
        growthChart: growthData,
        metrics: platformMetrics
      }
    });
  } catch (error) {
    console.error('Growth data error:', error);
    res.status(500).json({
      success: false,
      error: 'Data Error',
      message: error.message
    });
  }
});

module.exports = router;
