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 { WebhooksService } from './webhooks.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../auth/guards/roles.guard';
import { ModuleGuard } from '../../common/guards/module.guard';
import { Roles, UserRole } from '../../common/decorators/roles.decorator';
import { RequireModule } from '../../common/decorators/require-module.decorator';
import {
  CurrentUser,
  JwtPayload,
} from '../../common/decorators/current-user.decorator';
import {
  CreateWebhookDto,
  UpdateWebhookDto,
  FindWebhooksDto,
  FindWebhookLogsDto,
} from './dto';

@ApiTags('Webhooks')
@ApiBearerAuth()
@Controller('webhooks')
@UseGuards(JwtAuthGuard, ModuleGuard, RolesGuard)
@RequireModule('webhooks')
@Roles(UserRole.CLUB_ADMIN)
export class WebhooksController {
  constructor(private readonly webhooksService: WebhooksService) {}

  @Get()
  @ApiOperation({ summary: 'List webhook subscriptions' })
  @ApiResponse({ status: 200, description: 'Paginated list of webhook subscriptions' })
  async findAll(
    @CurrentUser() user: JwtPayload,
    @Query() query: FindWebhooksDto,
  ) {
    return this.webhooksService.findAll(user.clubId, query);
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get webhook subscription by ID' })
  @ApiParam({ name: 'id', description: 'Webhook subscription UUID' })
  @ApiResponse({ status: 200, description: 'Webhook subscription details' })
  @ApiResponse({ status: 404, description: 'Subscription not found' })
  async findById(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    return this.webhooksService.findById(user.clubId, id);
  }

  @Post()
  @ApiOperation({ summary: 'Create a webhook subscription' })
  @ApiResponse({
    status: 201,
    description: 'Webhook subscription created (secret is returned only once)',
  })
  async create(
    @CurrentUser() user: JwtPayload,
    @Body() dto: CreateWebhookDto,
  ) {
    return this.webhooksService.create(user.clubId, dto);
  }

  @Patch(':id')
  @ApiOperation({ summary: 'Update a webhook subscription' })
  @ApiParam({ name: 'id', description: 'Webhook subscription UUID' })
  @ApiResponse({ status: 200, description: 'Webhook subscription updated' })
  @ApiResponse({ status: 404, description: 'Subscription not found' })
  async update(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: UpdateWebhookDto,
  ) {
    return this.webhooksService.update(user.clubId, id, dto);
  }

  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Delete a webhook subscription' })
  @ApiParam({ name: 'id', description: 'Webhook subscription UUID' })
  @ApiResponse({ status: 204, description: 'Webhook subscription deleted' })
  @ApiResponse({ status: 404, description: 'Subscription not found' })
  async remove(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    return this.webhooksService.remove(user.clubId, id);
  }

  @Get(':id/logs')
  @ApiOperation({ summary: 'Get delivery logs for a webhook subscription' })
  @ApiParam({ name: 'id', description: 'Webhook subscription UUID' })
  @ApiResponse({ status: 200, description: 'Paginated list of delivery logs' })
  async findLogs(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
    @Query() query: FindWebhookLogsDto,
  ) {
    return this.webhooksService.findLogs(user.clubId, id, query);
  }

  @Post(':id/test')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: 'Send a test webhook delivery' })
  @ApiParam({ name: 'id', description: 'Webhook subscription UUID' })
  @ApiResponse({ status: 200, description: 'Test delivery result' })
  @ApiResponse({ status: 404, description: 'Subscription not found' })
  async testDelivery(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    return this.webhooksService.testDelivery(user.clubId, id);
  }
}
