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

@ApiTags('Admin - Merchant')
@Controller('admin/merchant')
@UseGuards(AuthGuard('jwt'), RolesGuard)
@Roles('admin')
@ApiBearerAuth()
export class AdminMerchantController {
  constructor(private readonly adminMerchantService: AdminMerchantService) {}

  @Get('stats')
  @ApiOperation({ summary: 'Merchant redemption stats overview' })
  async getRedemptionStats() {
    const result = await this.adminMerchantService.getRedemptionStats();
    return { code: 0, data: result };
  }

  @Get('stores/ranking')
  @ApiOperation({ summary: 'Top stores ranked by redemption volume' })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  async getStoreRanking(@Query('limit') limit?: string) {
    const result = await this.adminMerchantService.getStoreRanking(
      limit ? parseInt(limit, 10) : 20,
    );
    return { code: 0, data: result };
  }

  @Get('realtime-feed')
  @ApiOperation({ summary: 'Recent redemption activity feed' })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  async getRealtimeFeed(@Query('limit') limit?: string) {
    const result = await this.adminMerchantService.getRealtimeFeed(
      limit ? parseInt(limit, 10) : 50,
    );
    return { code: 0, data: result };
  }

  @Post('stores/:id/flag')
  @ApiOperation({ summary: 'Flag a store as abnormal for investigation' })
  async flagStore(
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: FlagStoreDto,
  ) {
    const store = await this.adminMerchantService.flagStore(id, dto.reason);
    return { code: 0, data: store, message: 'Store flagged for investigation' };
  }
}
