import { Body, Controller, Get, Param, Post, UseGuards, Query, Patch, Delete } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { PromosService } from './promos.service';
import { CreatePromotionDto } from './promos.dto';
import { JwtGuard } from '../../common/jwt.guard';

@ApiTags('Promotions')
@Controller('promos')
export class PromosController {
  constructor(private service: PromosService) {}

  @Get()
  @ApiOperation({ summary: 'Get active promotions' })
  getActive() {
    return this.service.getActive();
  }

  @Get('all')
  @UseGuards(JwtGuard)
  @ApiBearerAuth('JWT-auth')
  @ApiOperation({ summary: 'Get all promos with filters (admin)' })
  getAll(
    @Query('search') search?: string,
    @Query('active') active?: string,
    @Query('page') page?: string,
    @Query('pageSize') pageSize?: string,
  ) {
    return this.service.getAll({
      search,
      active: active === 'true' ? true : active === 'false' ? false : undefined,
      page: page ? parseInt(page) : 1,
      pageSize: pageSize ? parseInt(pageSize) : 10,
    });
  }

  @Get(':id')
  @UseGuards(JwtGuard)
  @ApiBearerAuth('JWT-auth')
  @ApiOperation({ summary: 'Get promo by ID' })
  getById(@Param('id') id: string) {
    return this.service.getById(id);
  }

  @Get('validate/:code')
  @ApiOperation({ summary: 'Validate a promo code' })
  validate(@Param('code') code: string) {
    return this.service.validate(code);
  }

  @Post()
  @UseGuards(JwtGuard)
  @ApiBearerAuth('JWT-auth')
  @ApiOperation({ summary: 'Create promotion (admin)' })
  create(@Body() dto: CreatePromotionDto) {
    return this.service.create(dto);
  }

  @Patch(':id')
  @UseGuards(JwtGuard)
  @ApiBearerAuth('JWT-auth')
  @ApiOperation({ summary: 'Update promo (admin)' })
  update(@Param('id') id: string, @Body() dto: Partial<CreatePromotionDto>) {
    return this.service.update(id, dto);
  }

  @Delete(':id')
  @UseGuards(JwtGuard)
  @ApiBearerAuth('JWT-auth')
  @ApiOperation({ summary: 'Delete promo (admin)' })
  delete(@Param('id') id: string) {
    return this.service.delete(id);
  }
}
