import {
  Body,
  Get,
  Controller,
  Post,
  UsePipes,
  ValidationPipe,
  UseGuards,
  Put,
  Param,
} from '@nestjs/common';

import { JwtAuthGuard } from '../../../../libs/auth-core-lib/src/guards/jwt-auth-guard';
import { UserRegistrationRequestDto } from '../dto/user-registration.req.dto';
import { User } from './user.entity';
import { UserService } from './user.service';
import { ApiDefaultResponses } from '../common.utils';
import { MessagePattern, RpcException } from '@nestjs/microservices';

@Controller()
export class UserController {
  constructor(private userDbService: UserService) {}

  @MessagePattern({ cmd: 'user_register' })
  async doUserRegistration(data: { user: any }): Promise<User> {
    try {
      const { user } = data;

      return await this.userDbService.doUserRegistration(user);
    } catch (err) {
      console.log(err);
      throw new RpcException('User registration failed!');
    }
  }

  @MessagePattern({ cmd: 'user_get_all' })
  async getAllUsers(): Promise<User[]> {
    return await this.userDbService.getAllUsers();
  }

  @MessagePattern({ cmd: 'user_update' })
  async updateUser(data: { id: number; userData: any }): Promise<any> {
    const { id, userData } = data;
    console.log('ok! here is calling!');
    return await this.userDbService.updateUser(id, userData);
  }

  @MessagePattern({ cmd: 'user_get_by_id' })
  async getUserById(data: { id: number }) {
    try {
      const { id } = data;
      return await this.userDbService.getUserById(id);
    } catch (err) {
      console.log(err);
      throw new RpcException('User fetching failed!');
    }
  }
}
