import {
  Controller,
  Get,
  Post,
  Param,
  Query,
  Body,
  UseGuards,
  ParseUUIDPipe,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { AdminCouponService } from '../../../application/services/admin-coupon.service';
import { RolesGuard, Roles } from '../guards/roles.guard';
import { ListCouponsQueryDto, RejectCouponDto } from '../dto/coupon.dto';

@ApiTags('Admin - Coupons')
@Controller('admin/coupons')
@UseGuards(AuthGuard('jwt'), RolesGuard)
@Roles('admin')
@ApiBearerAuth()
export class AdminCouponController {
  constructor(private readonly adminCouponService: AdminCouponService) {}

  @Get()
  @ApiOperation({ summary: 'List coupons with filters (admin)' })
  async listCoupons(@Query() query: ListCouponsQueryDto) {
    const result = await this.adminCouponService.listCoupons({
      page: query.page,
      limit: query.limit,
      status: query.status,
      issuerId: query.issuerId,
      category: query.category,
      search: query.search,
    });
    return { code: 0, data: result };
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get coupon detail with rules and metrics (admin)' })
  async getCouponDetail(@Param('id', ParseUUIDPipe) id: string) {
    const result = await this.adminCouponService.getCouponDetail(id);
    return { code: 0, data: result };
  }

  @Post(':id/approve')
  @ApiOperation({ summary: 'Approve a draft coupon for launch (admin)' })
  async approveCoupon(@Param('id', ParseUUIDPipe) id: string) {
    const coupon = await this.adminCouponService.approveCoupon(id);
    return { code: 0, data: coupon, message: 'Coupon approved and set to active' };
  }

  @Post(':id/reject')
  @ApiOperation({ summary: 'Reject a draft coupon (admin)' })
  async rejectCoupon(
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: RejectCouponDto,
  ) {
    const coupon = await this.adminCouponService.rejectCoupon(id, dto.reason);
    return { code: 0, data: coupon, message: 'Coupon rejected' };
  }

  @Post(':id/suspend')
  @ApiOperation({ summary: 'Suspend an active coupon (admin)' })
  async suspendCoupon(@Param('id', ParseUUIDPipe) id: string) {
    const coupon = await this.adminCouponService.suspendCoupon(id);
    return { code: 0, data: coupon, message: 'Coupon suspended' };
  }
}
