import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Param,
  Body,
  Query,
  UseGuards,
  ParseUUIDPipe,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiResponse, 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 { BookingServicesService } from '../services/booking-services.service';
import {
  CreateBookingServiceDto,
  UpdateBookingServiceDto,
  CreateServiceCategoryDto,
  UpdateServiceCategoryDto,
  ReorderServicesDto,
} from '../dto/create-service.dto';

@ApiTags('Booking / Services')
@ApiBearerAuth()
@Controller('booking/services')
@UseGuards(JwtAuthGuard, ModuleGuard)
@RequireModule('booking')
export class BookingServicesController {
  constructor(private readonly servicesService: BookingServicesService) {}

  // ─── Services ───────────────────────────────────────────────

  @Get()
  @ApiOperation({ summary: 'List booking services' })
  @ApiQuery({ name: 'activeOnly', required: false, type: Boolean })
  async findAll(@CurrentUser() user: JwtPayload, @Query('activeOnly') activeOnly?: string) {
    return this.servicesService.findAll(user.clubId, activeOnly === 'true');
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get service by ID' })
  async findById(@Param('id', ParseUUIDPipe) id: string) {
    return this.servicesService.findById(id);
  }

  @Post()
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN, UserRole.MANAGER)
  @ApiOperation({ summary: 'Create a booking service' })
  @ApiResponse({ status: 201 })
  async create(@CurrentUser() user: JwtPayload, @Body() dto: CreateBookingServiceDto) {
    return this.servicesService.create(user.clubId, dto);
  }

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

  @Delete(':id')
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN)
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Delete a service' })
  async delete(@Param('id', ParseUUIDPipe) id: string) {
    await this.servicesService.delete(id);
  }

  @Patch('reorder')
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN, UserRole.MANAGER)
  @ApiOperation({ summary: 'Reorder services' })
  async reorder(@CurrentUser() user: JwtPayload, @Body() dto: ReorderServicesDto) {
    return this.servicesService.reorder(user.clubId, dto);
  }
}

@ApiTags('Booking / Service Categories')
@ApiBearerAuth()
@Controller('booking/service-categories')
@UseGuards(JwtAuthGuard, ModuleGuard)
@RequireModule('booking')
export class BookingServiceCategoriesController {
  constructor(private readonly servicesService: BookingServicesService) {}

  @Get()
  @ApiOperation({ summary: 'List service categories' })
  async findAll(@CurrentUser() user: JwtPayload) {
    return this.servicesService.findAllCategories(user.clubId);
  }

  @Post()
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN)
  @ApiOperation({ summary: 'Create a category' })
  async create(@CurrentUser() user: JwtPayload, @Body() dto: CreateServiceCategoryDto) {
    return this.servicesService.createCategory(user.clubId, dto);
  }

  @Patch(':id')
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN)
  @ApiOperation({ summary: 'Update a category' })
  async update(@Param('id', ParseUUIDPipe) id: string, @Body() dto: UpdateServiceCategoryDto) {
    return this.servicesService.updateCategory(id, dto);
  }

  @Delete(':id')
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN)
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Delete a category' })
  async delete(@Param('id', ParseUUIDPipe) id: string) {
    await this.servicesService.deleteCategory(id);
  }
}
