import { Controller, Get } from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { BaseController } from '../../common/base';
import { UsersService } from './users.service';
import { CreateUserDto, UpdateUserDto } from './dto/user.dto';
import { CurrentUser, Roles } from '../../common/decorators';
import {
  ApiResponse,
  createSuccessResponse,
} from '../../common/types/api-response.type';
import { User } from '@prisma/client/wasm';

import { plainToInstance } from 'class-transformer';
import { UserResponseDto } from './dto/user.dto';

interface AuthenticatedUser {
  id: string;
  email: string;
  roles: string[];
  permissions: string[];
}

@ApiTags('Users')
@ApiBearerAuth()
@Controller('users')
export class UsersController extends BaseController<
  User,
  CreateUserDto,
  UpdateUserDto
> {
  constructor(private readonly usersService: UsersService) {
    super(usersService, 'User');
  }

  @Get('me')
  async getMe(
    @CurrentUser() user: AuthenticatedUser,
  ): Promise<ApiResponse<UserResponseDto>> {
    const fullUser = await this.usersService.findOneWithRoles(user.id);
    return createSuccessResponse(
      plainToInstance(UserResponseDto, fullUser),
      'User profile retrieved successfully',
    );
  }

  // Override create to require admin role
  @Roles('admin')
  async create(
    ...args: Parameters<
      BaseController<User, CreateUserDto, UpdateUserDto>['create']
    >
  ) {
    return super.create(...args);
  }

  // Override delete to require admin role
  @Roles('admin')
  async delete(
    ...args: Parameters<
      BaseController<User, CreateUserDto, UpdateUserDto>['delete']
    >
  ) {
    return super.delete(...args);
  }
}
