/**
 * Dashboard Controller
 *
 * Provides dashboard analytics and KPI endpoints
 */

import { Controller, Get, UseGuards, Request } from '@nestjs/common';
import { AnalyticsService } from '../services/analytics.service';
import { JwtAuthGuard } from '../guards/jwt-auth.guard';
import { FeatureFlagGuard } from '../guards/feature-flag.guard';
import { RequiresFeature } from '../decorators/requires-feature.decorator';

@Controller('dashboard')
@UseGuards(JwtAuthGuard, FeatureFlagGuard)
@RequiresFeature('dashboard')
export class DashboardController {
  constructor(private readonly analyticsService: AnalyticsService) {}

  /**
   * Get dashboard KPIs
   * GET /api/v1/dashboard/kpis
   */
  @Get('kpis')
  async getKPIs(@Request() req: any) {
    const organizationId = req.user.organizationId;
    return this.analyticsService.calculateKPIs(organizationId);
  }

  /**
   * Get bookings chart data (6 months)
   * GET /api/v1/dashboard/bookings-chart
   */
  @Get('bookings-chart')
  async getBookingsChart(@Request() req: any) {
    const organizationId = req.user.organizationId;
    return this.analyticsService.getBookingsChartData(organizationId);
  }

  /**
   * Get top 5 trade lanes
   * GET /api/v1/dashboard/top-trade-lanes
   */
  @Get('top-trade-lanes')
  async getTopTradeLanes(@Request() req: any) {
    const organizationId = req.user.organizationId;
    return this.analyticsService.getTopTradeLanes(organizationId);
  }

  /**
   * Get dashboard alerts
   * GET /api/v1/dashboard/alerts
   */
  @Get('alerts')
  async getAlerts(@Request() req: any) {
    const organizationId = req.user.organizationId;
    return this.analyticsService.getAlerts(organizationId);
  }

  /**
   * Get CSV Booking KPIs
   * GET /api/v1/dashboard/csv-booking-kpis
   */
  @Get('csv-booking-kpis')
  async getCsvBookingKPIs(@Request() req: any) {
    const organizationId = req.user.organizationId;
    return this.analyticsService.getCsvBookingKPIs(organizationId);
  }

  /**
   * Get Top Carriers
   * GET /api/v1/dashboard/top-carriers
   */
  @Get('top-carriers')
  async getTopCarriers(@Request() req: any) {
    const organizationId = req.user.organizationId;
    return this.analyticsService.getTopCarriers(organizationId, 5);
  }
}
