import { Controller, Get, Query, Header } from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiQuery } from '@nestjs/swagger';
import { AnalyticsService } from './analytics.service';
import { CurrentTenant } from '../../common/decorators/current-tenant.decorator';
import { RequirePermission } from '../../common/decorators/require-permission.decorator';

@ApiTags('analytics')
@ApiBearerAuth()
@Controller('analytics')
export class AnalyticsController {
  constructor(private readonly analyticsService: AnalyticsService) {}

  @Get('summary')
  @RequirePermission('analytics.view')
  @ApiOperation({ summary: 'Get analytics summary for time range' })
  @ApiQuery({ name: 'range', required: false, type: Number, description: 'Hours to analyze (default: 24)' })
  async getSummary(
    @CurrentTenant() licenseId: string,
    @Query('range') range?: string,
  ) {
    const rangeHours = parseInt(range || '24', 10);
    return this.analyticsService.getSummary(licenseId, rangeHours);
  }

  @Get('timeseries')
  @RequirePermission('analytics.view')
  @ApiOperation({ summary: 'Get timeseries data for charts' })
  @ApiQuery({ name: 'range', required: false, type: Number })
  @ApiQuery({ name: 'granularity', required: false, enum: ['raw', 'hourly'] })
  async getTimeseries(
    @CurrentTenant() licenseId: string,
    @Query('range') range?: string,
    @Query('granularity') granularity?: 'raw' | 'hourly',
  ) {
    const rangeHours = parseInt(range || '24', 10);
    const gran = granularity || 'hourly';
    return this.analyticsService.getTimeseries(licenseId, rangeHours, gran);
  }

  @Get('wipes/performance')
  @RequirePermission('analytics.view')
  @ApiOperation({ summary: 'Get wipe performance metrics' })
  @ApiQuery({ name: 'range', required: false, type: Number, description: 'Hours (default: 720 = 30 days)' })
  async getWipePerformance(
    @CurrentTenant() licenseId: string,
    @Query('range') range?: string,
  ) {
    const rangeHours = parseInt(range || '720', 10);
    return this.analyticsService.getWipePerformance(licenseId, rangeHours);
  }

  @Get('maps')
  @RequirePermission('analytics.view')
  @ApiOperation({ summary: 'Get map usage analytics' })
  @ApiQuery({ name: 'range', required: false, type: Number })
  async getMapAnalytics(
    @CurrentTenant() licenseId: string,
    @Query('range') range?: string,
  ) {
    const rangeHours = parseInt(range || '720', 10);
    return this.analyticsService.getMapAnalytics(licenseId, rangeHours);
  }

  @Get('players')
  @RequirePermission('analytics.view')
  @ApiOperation({ summary: 'Get player analytics' })
  @ApiQuery({ name: 'range', required: false, type: Number })
  @ApiQuery({ name: 'metric', required: false, enum: ['sessions', 'retention'] })
  async getPlayerAnalytics(
    @CurrentTenant() licenseId: string,
    @Query('range') range?: string,
    @Query('metric') metric?: 'sessions' | 'retention',
  ) {
    const rangeHours = parseInt(range || '720', 10);
    const m = metric || 'sessions';
    return this.analyticsService.getPlayerAnalytics(licenseId, rangeHours, m);
  }

  @Get('retention')
  @RequirePermission('analytics.view')
  @ApiOperation({ summary: 'Get player retention across wipes' })
  @ApiQuery({ name: 'wipe_count', required: false, type: Number, description: 'Number of recent wipes (default: 5)' })
  async getRetention(
    @CurrentTenant() licenseId: string,
    @Query('wipe_count') wipeCount?: string,
  ) {
    const count = parseInt(wipeCount || '5', 10);
    return this.analyticsService.getRetention(licenseId, count);
  }

  @Get('export')
  @RequirePermission('analytics.view')
  @ApiOperation({ summary: 'Export analytics data as CSV' })
  @ApiQuery({ name: 'range', required: false, type: Number })
  @Header('Content-Type', 'text/csv')
  @Header('Content-Disposition', 'attachment; filename="analytics-export.csv"')
  async exportData(
    @CurrentTenant() licenseId: string,
    @Query('range') range?: string,
  ) {
    const rangeHours = parseInt(range || '24', 10);
    return this.analyticsService.exportData(licenseId, rangeHours);
  }
}
