import { Controller, Get, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiQuery } from '@nestjs/swagger';
import { IsDateString, IsUUID } from 'class-validator';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
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 { AvailabilityService } from '../core/availability.service';

class AvailableDatesQuery {
  @IsUUID() serviceId: string;
  @IsUUID() staffId: string;
  @IsDateString() from: string;
  @IsDateString() to: string;
}

class AvailableSlotsQuery {
  @IsUUID() serviceId: string;
  @IsUUID() staffId: string;
  @IsDateString() date: string;
}

@ApiTags('Booking / Availability')
@ApiBearerAuth()
@Controller('booking/schedule')
@UseGuards(JwtAuthGuard, ModuleGuard)
@RequireModule('booking')
export class BookingAvailabilityController {
  constructor(private readonly availability: AvailabilityService) {}

  @Get('available-dates')
  @ApiOperation({ summary: 'Get dates with available slots' })
  @ApiQuery({ name: 'serviceId', type: String })
  @ApiQuery({ name: 'staffId', type: String })
  @ApiQuery({ name: 'from', type: String, example: '2026-03-01' })
  @ApiQuery({ name: 'to', type: String, example: '2026-03-31' })
  async getAvailableDates(@CurrentUser() user: JwtPayload, @Query() query: AvailableDatesQuery) {
    return this.availability.getAvailableDates({
      clubId: user.clubId,
      serviceId: query.serviceId,
      staffId: query.staffId,
      from: new Date(query.from),
      to: new Date(query.to),
    });
  }

  @Get('available-slots')
  @ApiOperation({ summary: 'Get available time slots for a date' })
  @ApiQuery({ name: 'serviceId', type: String })
  @ApiQuery({ name: 'staffId', type: String })
  @ApiQuery({ name: 'date', type: String, example: '2026-03-15' })
  async getAvailableSlots(@CurrentUser() user: JwtPayload, @Query() query: AvailableSlotsQuery) {
    return this.availability.getAvailableSlots({
      clubId: user.clubId,
      serviceId: query.serviceId,
      staffId: query.staffId,
      date: new Date(query.date),
    });
  }
}
