import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  ParseUUIDPipe,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiBearerAuth,
  ApiResponse,
  ApiParam,
} from '@nestjs/swagger';
import { ClubsService } from './clubs.service';
import { ProvisioningService, Club } from '../provisioning/provisioning.service';
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 { FindClubsDto, UpdateClubDto } from './dto';
import { CreateClubDto } from '../provisioning/dto/create-club.dto';

@ApiTags('Clubs')
@ApiBearerAuth()
@Controller('clubs')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.SUPER_ADMIN)
export class ClubsController {
  constructor(
    private readonly clubsService: ClubsService,
    private readonly provisioningService: ProvisioningService,
  ) {}

  @Get()
  @ApiOperation({ summary: 'List all clubs with pagination and search' })
  @ApiResponse({ status: 200, description: 'Paginated list of clubs' })
  async findAll(@Query() query: FindClubsDto) {
    return this.clubsService.findAll(query);
  }

  @Post()
  @ApiOperation({ summary: 'Provision a new club environment' })
  @ApiResponse({ status: 201, description: 'Club created successfully' })
  @ApiResponse({ status: 409, description: 'Club with this slug already exists' })
  async create(@Body() dto: CreateClubDto): Promise<Club> {
    return this.provisioningService.provisionClub(dto);
  }

  @Get('settings')
  @Roles(UserRole.CLUB_ADMIN, UserRole.SUPER_ADMIN)
  @ApiOperation({ summary: 'Get settings for the current user club' })
  @ApiResponse({ status: 200, description: 'Club settings' })
  async getSettings(@CurrentUser() user: JwtPayload) {
    return this.clubsService.findById(user.clubId);
  }

  @Patch('settings')
  @Roles(UserRole.CLUB_ADMIN, UserRole.SUPER_ADMIN)
  @ApiOperation({ summary: 'Update settings for the current user club' })
  @ApiResponse({ status: 200, description: 'Club settings updated' })
  async updateSettings(
    @CurrentUser() user: JwtPayload,
    @Body() dto: UpdateClubDto,
  ) {
    return this.clubsService.update(user.clubId, dto);
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get club by ID with license and modules' })
  @ApiParam({ name: 'id', description: 'Club UUID' })
  @ApiResponse({ status: 200, description: 'Club details' })
  @ApiResponse({ status: 404, description: 'Club not found' })
  async findById(@Param('id', ParseUUIDPipe) id: string) {
    return this.clubsService.findById(id);
  }

  @Patch(':id')
  @ApiOperation({ summary: 'Update club details' })
  @ApiParam({ name: 'id', description: 'Club UUID' })
  @ApiResponse({ status: 200, description: 'Club updated' })
  @ApiResponse({ status: 404, description: 'Club not found' })
  async update(
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: UpdateClubDto,
  ) {
    return this.clubsService.update(id, dto);
  }

  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Soft-delete a club (set isActive = false)' })
  @ApiParam({ name: 'id', description: 'Club UUID' })
  @ApiResponse({ status: 204, description: 'Club deactivated' })
  @ApiResponse({ status: 404, description: 'Club not found' })
  async remove(@Param('id', ParseUUIDPipe) id: string) {
    return this.clubsService.softDelete(id);
  }
}
