import { Controller, Get, HttpException, HttpStatus, Logger, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { AdminUsersService } from './modules/admin-users/admin-users.service';
import { AdminJwtAuthGuard } from './modules/admin-auth/guards/admin-jwt-auth.guard';

@ApiTags('Admin')
@Controller('admin')
export class AdminController {
  private readonly logger = new Logger(AdminController.name);

  constructor(
    private readonly adminUsersService: AdminUsersService
  ) {}

  @Get('dashboard')
  @ApiBearerAuth()
  @UseGuards(AdminJwtAuthGuard)
  async getDashboard() {
    this.logger.log('AdminController: getDashboard - fetching dashboard data');
    
    try {
      // Fetch various statistics for the dashboard
      const [userAnalytics] = await Promise.all([
        this.adminUsersService.getUserAnalytics(),
        // Add more analytics services here as they are created
        // e.g., this.adminPaymentsService.getPaymentAnalytics(),
        // this.adminSubscriptionsService.getSubscriptionAnalytics(),
      ]);

      const dashboardData = {
        users: userAnalytics,
        // Add more sections as services are created
        lastUpdated: new Date()
      };

      this.logger.log('AdminController: getDashboard - returning dashboard data');

      return {
        statusCode: HttpStatus.OK,
        data: dashboardData
      };
    } catch (error) {
      this.logger.error(`AdminController: getDashboard error - ${error.message}`, error.stack);
      
      throw new HttpException(
        {
          statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
          message: 'Failed to fetch dashboard data',
          error: error.message
        },
        HttpStatus.INTERNAL_SERVER_ERROR
      );
    }
  }

  @Get('health')
  async healthCheck() {
    this.logger.log('AdminController: healthCheck - checking admin system health');
    
    return {
      statusCode: HttpStatus.OK,
      message: 'Admin system is healthy',
      timestamp: new Date(),
      uptime: process.uptime()
    };
  }
}