import { Request, Response } from 'express';

import AdminService from '../services/adminService';

class AdminController {
  private adminService: AdminService;

  constructor() {
    this.adminService = new AdminService();
  }

  async getOverview(_req: Request, res: Response) {
    try {
      const totalUsers = await this.adminService.getTotalUsers();
      const revenue = await this.adminService.getRevenueTracking();
      const tradeVolume = await this.adminService.getTradeVolume();
      const apiSyncHealthy = await this.adminService.checkApiSyncHealth();
      res.json({ totalUsers, revenue, tradeVolume, apiSyncHealthy });
    } catch (error) {
      res.status(500).json({ error: 'Failed to fetch overview' });
    }
  }

  async getTotalUsers(_req: Request, res: Response) {
    try {
      const totalUsers = await this.adminService.getTotalUsers();
      res.json({ totalUsers });
    } catch (error) {
      res.status(500).json({ error: 'Failed to fetch total users' });
    }
  }

  async getRevenueTracking(_req: Request, res: Response) {
    try {
      const revenue = await this.adminService.getRevenueTracking();
      res.json({ revenue });
    } catch (error) {
      res.status(500).json({ error: 'Failed to fetch revenue' });
    }
  }

  async getTradeVolume(_req: Request, res: Response) {
    try {
      const tradeVolume = await this.adminService.getTradeVolume();
      res.json({ tradeVolume });
    } catch (error) {
      res.status(500).json({ error: 'Failed to fetch trade volume' });
    }
  }

  async getApiSyncHealth(_req: Request, res: Response) {
    try {
      const apiSyncHealthy = await this.adminService.checkApiSyncHealth();
      res.json({ apiSyncHealthy });
    } catch (error) {
      res.status(500).json({ error: 'Failed to fetch api sync health' });
    }
  }

  async getErrorLogs(_req: Request, res: Response) {
    try {
      const logs = await this.adminService.getErrorLogs();
      res.json({ logs });
    } catch (error) {
      res.status(500).json({ error: 'Failed to fetch error logs' });
    }
  }
}

export default AdminController;
