import {
  Controller,
  Get,
  Post,
  Patch,
  Param,
  Body,
  Query,
  UseGuards,
  ParseUUIDPipe,
} 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 { GroupEventsService } from '../services/group-events.service';
import { BookingPermissions } from '../core/booking-permissions';
import {
  CreateGroupEventDto,
  UpdateGroupEventDto,
  RepeatGroupEventDto,
  FindGroupEventsQueryDto,
} from '../dto/create-group-event.dto';

@ApiTags('Booking / Group Events')
@ApiBearerAuth()
@Controller('booking/group-events')
@UseGuards(JwtAuthGuard, ModuleGuard)
@RequireModule('booking')
export class BookingGroupEventsController {
  constructor(
    private readonly groupEventsService: GroupEventsService,
    private readonly permissions: BookingPermissions,
  ) {}

  @Get()
  @ApiOperation({ summary: 'List group events' })
  async findAll(@CurrentUser() user: JwtPayload, @Query() query: FindGroupEventsQueryDto) {
    const roleFilter = this.permissions.filterGroupEventsByRole(user.sub, user.role);
    return this.groupEventsService.findAll(user.clubId, query, roleFilter);
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get group event with participants' })
  async findById(@Param('id', ParseUUIDPipe) id: string) {
    return this.groupEventsService.findById(id);
  }

  @Post()
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN, UserRole.MANAGER)
  @ApiOperation({ summary: 'Create a group event' })
  async create(@CurrentUser() user: JwtPayload, @Body() dto: CreateGroupEventDto) {
    return this.groupEventsService.create(user.clubId, dto, user.sub);
  }

  @Patch(':id')
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN, UserRole.MANAGER)
  @ApiOperation({ summary: 'Update a group event' })
  async update(@Param('id', ParseUUIDPipe) id: string, @Body() dto: UpdateGroupEventDto) {
    return this.groupEventsService.update(id, dto);
  }

  @Post(':id/cancel')
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN, UserRole.MANAGER)
  @ApiOperation({ summary: 'Cancel a group event (cancels all bookings)' })
  async cancel(@CurrentUser() user: JwtPayload, @Param('id', ParseUUIDPipe) id: string) {
    return this.groupEventsService.cancel(id, user.sub);
  }

  @Post(':id/repeat')
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN, UserRole.MANAGER)
  @ApiOperation({ summary: 'Create recurring series from this event' })
  async repeat(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: RepeatGroupEventDto,
  ) {
    return this.groupEventsService.repeat(id, dto, user.sub);
  }

  @Post('series/:seriesId/cancel')
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN)
  @ApiOperation({ summary: 'Cancel all future events in a series' })
  @ApiQuery({ name: 'from', required: false, type: String })
  async cancelSeries(
    @CurrentUser() user: JwtPayload,
    @Param('seriesId') seriesId: string,
    @Query('from') from?: string,
  ) {
    const fromDate = from ? new Date(from) : new Date();
    return this.groupEventsService.cancelSeries(seriesId, fromDate, user.sub);
  }
}
