import {
  Controller,
  Post,
  Body,
  Get,
  //Put,
  Param,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import { RoleAccessService } from './role_access.service';
import { Public } from '../auth/public.decorator';
// import { console } from 'inspector';
import {
  ApiBearerAuth,
  ApiTags,
  ApiBody,
  ApiParam,
  ApiQuery,
  ApiOperation,
} from '@nestjs/swagger';

@ApiTags('roleAccess')
@ApiBearerAuth('access-token')
@Controller('roleAccess')
export class RoleAccessController {
  constructor(private readonly roleAccessService: RoleAccessService) {}

  @Public()
  @Post('add_role_access')
  @ApiOperation({ summary: 'Create User with Role Access' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        email: { type: 'string', format: 'email' },
        password: { type: 'string', minLength: 6 },
        fullName: { type: 'string' },
        phone: { type: 'string' },
        role: {
          type: 'string',
          enum: [
            'admin',
            'operation',
            'trainer',
            'associate',
            'recruiter',
            'seeker',
          ],
        },
        moduleIds: {
          type: 'array',
          items: { type: 'number' },
          description: 'Required if role is operation',
        },
      },
      required: ['email', 'password', 'fullName', 'role'],
    },
  })
  async createUserWithRole(@Body() body: any) {
    try {
      // console.log('Received body:', body);
      return await this.roleAccessService.createUserWithRoleAccess(body);
    } catch (error) {
      throw new HttpException(
        { message: error.message || 'Error creating user with role access' },
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

  // role_access.controller.ts
  @Public()
  @Get('user_modules/:userId')
  @ApiOperation({ summary: 'Get User Modules' })
  @ApiParam({ name: 'userId', type: Number })
  async getUserModules(@Param('userId') userId: number) {
    try {
      const modules = await this.roleAccessService.getModulesByUser(
        Number(userId),
      );
      return { success: true, data: modules };
    } catch (error) {
      throw new HttpException(
        { success: false, message: error.message },
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }
}
