import {
  Controller,
  Get,
  Patch,
  Delete,
  Body,
  Param,
  UseGuards,
} from '@nestjs/common';
import { UsersService } from './users.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { AbilitiesGuard } from '../auth/guards/abilities.guard';
import { CanRead, CanUpdate, CanDelete, CheckAbilities } from '../auth/decorators/check-ability.decorator';
import { Action } from '../auth/abilities/ability.factory';
import { UpdateUserDto, ApproveUserDto } from './dto';

@Controller('users')
@UseGuards(JwtAuthGuard, AbilitiesGuard)
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  @Get()
  @CanRead('User')
  findAll() {
    return this.usersService.findAll();
  }

  @Get('pending')
  @CanRead('User')
  getPendingUsers() {
    return this.usersService.getPendingUsers();
  }

  @Get(':id')
  @CanRead('User')
  findOne(@Param('id') id: string) {
    return this.usersService.findOne(id);
  }

  @Patch(':id')
  @CanUpdate('User')
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.usersService.update(id, updateUserDto);
  }

  @Patch(':id/approve')
  @CheckAbilities({ action: Action.Approve, subject: 'User' })
  approve(@Param('id') id: string, @Body() approveUserDto: ApproveUserDto) {
    return this.usersService.approve(id, approveUserDto);
  }

  @Delete(':id')
  @CanDelete('User')
  remove(@Param('id') id: string) {
    return this.usersService.remove(id);
  }
}
