import { Controller, Get, Query } from '@nestjs/common';
import {
  AudienceService,
  AudienceOverview,
  DemographicsMetrics,
  DeviceMetrics,
  BrowserMetrics,
  OSMetrics,
  GeoMetrics,
  LanguageMetrics,
  NewVsReturningMetrics,
} from './audience.service';
import { AudienceQueryDto, GeoQueryDto, DeviceQueryDto } from './dto/audience-query.dto';

@Controller('audience')
export class AudienceController {
  constructor(private readonly audienceService: AudienceService) {}

  /**
   * Get audience overview
   * GET /audience/overview?startDate=2024-01-01&endDate=2024-01-31
   */
  @Get('overview')
  async getOverview(@Query() query: AudienceQueryDto): Promise<AudienceOverview> {
    return this.audienceService.getOverview(query);
  }

  /**
   * Get demographics breakdown (new vs returning)
   * GET /audience/demographics?startDate=2024-01-01&endDate=2024-01-31
   */
  @Get('demographics')
  async getDemographics(@Query() query: AudienceQueryDto): Promise<DemographicsMetrics[]> {
    return this.audienceService.getDemographics(query);
  }

  /**
   * Get device breakdown
   * GET /audience/devices?startDate=2024-01-01&endDate=2024-01-31
   */
  @Get('devices')
  async getDevices(@Query() query: DeviceQueryDto): Promise<DeviceMetrics[]> {
    return this.audienceService.getDevices(query);
  }

  /**
   * Get browser breakdown
   * GET /audience/browsers?startDate=2024-01-01&endDate=2024-01-31
   */
  @Get('browsers')
  async getBrowsers(@Query() query: AudienceQueryDto): Promise<BrowserMetrics[]> {
    return this.audienceService.getBrowsers(query);
  }

  /**
   * Get OS breakdown
   * GET /audience/operating-systems?startDate=2024-01-01&endDate=2024-01-31
   */
  @Get('operating-systems')
  async getOperatingSystems(@Query() query: AudienceQueryDto): Promise<OSMetrics[]> {
    return this.audienceService.getOperatingSystems(query);
  }

  /**
   * Get geography breakdown
   * GET /audience/geography?startDate=2024-01-01&endDate=2024-01-31&granularity=country
   */
  @Get('geography')
  async getGeography(@Query() query: GeoQueryDto): Promise<GeoMetrics[]> {
    return this.audienceService.getGeography(query);
  }

  /**
   * Get language breakdown
   * GET /audience/languages?startDate=2024-01-01&endDate=2024-01-31
   */
  @Get('languages')
  async getLanguages(@Query() query: AudienceQueryDto): Promise<LanguageMetrics[]> {
    return this.audienceService.getLanguages(query);
  }

  /**
   * Get new vs returning users over time
   * GET /audience/new-vs-returning?startDate=2024-01-01&endDate=2024-01-31
   */
  @Get('new-vs-returning')
  async getNewVsReturning(@Query() query: AudienceQueryDto): Promise<NewVsReturningMetrics[]> {
    return this.audienceService.getNewVsReturning(query);
  }

  /**
   * Get network/organization type breakdown (gov-detection orgType)
   * GET /audience/network-type?startDate=...&endDate=...
   */
  @Get('network-type')
  async getNetworkType(@Query() query: AudienceQueryDto) {
    return this.audienceService.getNetworkType(query);
  }

  /**
   * Get proxy/connection type breakdown (VPN, Tor, Datacenter, Direct)
   * GET /audience/proxy-type?startDate=...&endDate=...
   */
  @Get('proxy-type')
  async getProxyType(@Query() query: AudienceQueryDto) {
    return this.audienceService.getProxyType(query);
  }

  /**
   * Get top ASN organizations
   * GET /audience/organizations?startDate=...&endDate=...
   */
  @Get('organizations')
  async getOrganizations(@Query() query: AudienceQueryDto) {
    return this.audienceService.getOrganizations(query);
  }

  /**
   * Get government traffic alert summary
   * GET /audience/gov-alert?startDate=...&endDate=...
   */
  @Get('gov-alert')
  async getGovAlert(@Query() query: AudienceQueryDto) {
    return this.audienceService.getGovAlert(query);
  }

  /**
   * Get IP classification list — sessions grouped by org/ASN with detection signals
   * GET /audience/ip-classifications?startDate=...&endDate=...
   */
  @Get('ip-classifications')
  async getIPClassifications(@Query() query: AudienceQueryDto) {
    return this.audienceService.getIPClassifications(query);
  }
}
