import { Body, Controller, Delete, Get, Param, Patch, Post, Query, Req, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { User } from 'generated/prisma/client';
import { RolesGuard } from 'src/auth/guards/roles.guard';
import { Roles } from 'src/auth/decorators/roles.decorator';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { BootstrapTableRequestDto } from 'src/commons/dto/bootstrap-table-request.dto';
import { UserService } from './user.service';

@Controller('user')
@UseGuards(AuthGuard('jwt'), RolesGuard)
export class UserController {
  constructor(private readonly userService: UserService) { }

  @Get('me')
  getProfile(@Req() req: any) {
    console.log('UserController: Accessing user profile with request');
    const user: User = req.user; // This will be set by the JwtStrategy's validate method
    return {
      message: `This is the user profile for ${user.email}`,
      user: req.user // Access the user object set by the JWT strategy
    };
  }

  @Post()
  @Roles('SUPER_ADMIN')
  create(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto);
  }

  @Get()
  @Roles('SUPER_ADMIN')
  findAll(@Query() query: BootstrapTableRequestDto) {
    return this.userService.findAll(query);
  }

  @Get(':id')
  @Roles('SUPER_ADMIN')
  findOne(@Param('id') id: string) {
    return this.userService.findOne(id);
  }

  @Patch(':id')
  @Roles('SUPER_ADMIN')
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(id, updateUserDto);
  }

  @Delete(':id')
  @Roles('SUPER_ADMIN')
  remove(@Param('id') id: string) {
    return this.userService.remove(id);
  }
}
