import { Controller, Get, Query, UseGuards, BadRequestException } from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiQuery } from '@nestjs/swagger';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../../auth/guards/roles.guard';
import { Roles, UserRole } from '../../../common/decorators/roles.decorator';
import { CurrentUser, JwtPayload } from '../../../common/decorators/current-user.decorator';
import { RequireModule } from '../../../common/decorators/require-module.decorator';
import { ModuleGuard } from '../../../common/guards/module.guard';
import { BookingAnalyticsService } from '../services/analytics.service';

@ApiTags('Booking Analytics')
@ApiBearerAuth()
@Controller('booking/analytics')
@UseGuards(JwtAuthGuard, RolesGuard, ModuleGuard)
@RequireModule('booking')
@Roles(UserRole.CLUB_ADMIN, UserRole.SUPER_ADMIN, UserRole.MANAGER)
export class BookingAnalyticsController {
  constructor(private readonly analytics: BookingAnalyticsService) {}

  @Get('summary')
  @ApiOperation({ summary: 'Сводка по записям за период' })
  @ApiQuery({ name: 'from', required: true, type: String })
  @ApiQuery({ name: 'to', required: true, type: String })
  async getSummary(
    @CurrentUser() user: JwtPayload,
    @Query('from') from: string,
    @Query('to') to: string,
  ) {
    const { fromDate, toDate } = this.parseDates(from, to);
    return this.analytics.getSummary(user.clubId, fromDate, toDate);
  }

  @Get('staff-utilization')
  @ApiOperation({ summary: 'Загрузка тренеров за период' })
  @ApiQuery({ name: 'from', required: true, type: String })
  @ApiQuery({ name: 'to', required: true, type: String })
  async getStaffUtilization(
    @CurrentUser() user: JwtPayload,
    @Query('from') from: string,
    @Query('to') to: string,
  ) {
    const { fromDate, toDate } = this.parseDates(from, to);
    return this.analytics.getStaffUtilization(user.clubId, fromDate, toDate);
  }

  @Get('popular-services')
  @ApiOperation({ summary: 'Популярные услуги' })
  @ApiQuery({ name: 'from', required: true, type: String })
  @ApiQuery({ name: 'to', required: true, type: String })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  async getPopularServices(
    @CurrentUser() user: JwtPayload,
    @Query('from') from: string,
    @Query('to') to: string,
    @Query('limit') limit?: string,
  ) {
    const { fromDate, toDate } = this.parseDates(from, to);
    return this.analytics.getPopularServices(
      user.clubId,
      fromDate,
      toDate,
      limit ? parseInt(limit, 10) : undefined,
    );
  }

  @Get('daily-trends')
  @ApiOperation({ summary: 'Тренды по дням' })
  @ApiQuery({ name: 'from', required: true, type: String })
  @ApiQuery({ name: 'to', required: true, type: String })
  async getDailyTrends(
    @CurrentUser() user: JwtPayload,
    @Query('from') from: string,
    @Query('to') to: string,
  ) {
    const { fromDate, toDate } = this.parseDates(from, to);
    return this.analytics.getDailyTrends(user.clubId, fromDate, toDate);
  }

  private parseDates(from: string, to: string) {
    const fromDate = new Date(from);
    const toDate = new Date(to);
    if (isNaN(fromDate.getTime()) || isNaN(toDate.getTime())) {
      throw new BadRequestException('Invalid date format. Use ISO 8601 (e.g., 2026-01-01)');
    }
    if (fromDate > toDate) {
      throw new BadRequestException('"from" must be before "to"');
    }
    return { fromDate, toDate };
  }
}
