import { Body, Controller, Get, Param, Patch, Post, Req, UseGuards } from '@nestjs/common';
import type { Request } from 'express';
import { AuthenticatedUser } from '../auth/auth.types';
import { AuthGuard } from '../common/auth.guard';
import { RequirePermission } from '../common/permission.decorator';
import { PermissionGuard } from '../common/permission.guard';
import { CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { RolesService } from './roles.service';

@UseGuards(AuthGuard, PermissionGuard)
@Controller('roles')
export class RolesController {
  constructor(private readonly rolesService: RolesService) {}

  @Get()
  @RequirePermission('roles', 'view')
  findAll() {
    return this.rolesService.findAll();
  }

  @Post()
  @RequirePermission('roles', 'manage')
  create(
    @Req() request: Request & { user: AuthenticatedUser },
    @Body() dto: CreateRoleDto,
  ) {
    return this.rolesService.create(dto, request.user, request.ip);
  }

  @Patch(':id')
  @RequirePermission('roles', 'manage')
  update(
    @Req() request: Request & { user: AuthenticatedUser },
    @Param('id') id: string,
    @Body() dto: UpdateRoleDto,
  ) {
    return this.rolesService.update(id, dto, request.user, request.ip);
  }
}
