import {
  Controller, Get, Post, Param, Query, Body, UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard, Roles, RolesGuard, UserRole } from '@genex/common';
import { AdminUserService } from '../../../application/services/admin-user.service';
import { KycReviewDto, FreezeUserDto } from '../dto/admin-user.dto';

@ApiTags('Admin - User Management')
@Controller('admin/users')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.ADMIN)
@ApiBearerAuth()
export class AdminUserController {
  constructor(private readonly adminUserService: AdminUserService) {}

  @Get()
  @ApiOperation({ summary: 'List all users with filters (paginated)' })
  async listUsers(
    @Query('page') page = 1,
    @Query('limit') limit = 20,
    @Query('search') search?: string,
    @Query('kycLevel') kycLevel?: number,
    @Query('status') status?: string,
  ) {
    const data = await this.adminUserService.listUsers({
      page: +page,
      limit: +limit,
      search,
      kycLevel: kycLevel !== undefined ? +kycLevel : undefined,
      status,
    });
    return { code: 0, data };
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get user detail with KYC and wallet info' })
  async getUserDetail(@Param('id') id: string) {
    const data = await this.adminUserService.getUserDetail(id);
    return { code: 0, data };
  }

  @Post(':id/kyc-review')
  @ApiOperation({ summary: 'Approve or reject a user KYC submission' })
  async reviewKyc(
    @Param('id') id: string,
    @Body() dto: KycReviewDto,
  ) {
    const data = await this.adminUserService.reviewKyc(id, dto.action, dto.reason);
    return { code: 0, data };
  }

  @Post(':id/freeze')
  @ApiOperation({ summary: 'Freeze a user account' })
  async freezeUser(
    @Param('id') id: string,
    @Body() dto: FreezeUserDto,
  ) {
    const data = await this.adminUserService.freezeUser(id, dto.reason);
    return { code: 0, data };
  }

  @Post(':id/unfreeze')
  @ApiOperation({ summary: 'Unfreeze a user account' })
  async unfreezeUser(@Param('id') id: string) {
    const data = await this.adminUserService.unfreezeUser(id);
    return { code: 0, data };
  }

  @Get(':id/transactions')
  @ApiOperation({ summary: 'Get a user transactions via their wallet (paginated)' })
  async getUserTransactions(
    @Param('id') id: string,
    @Query('page') page = 1,
    @Query('limit') limit = 20,
  ) {
    const data = await this.adminUserService.getUserTransactions(id, +page, +limit);
    return { code: 0, data };
  }
}
