import {
  Controller,
  Get,
  Put,
  Body,
  Param,
  Headers,
} from '@nestjs/common';
import { UserService } from '../../application/services/user.service';
import { UpdateProfileDto } from '../../application/dtos/user.dto';

@Controller('users')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Get('me')
  async getCurrentUser(@Headers('x-user-id') userId: string) {
    const user = await this.userService.findById(userId);
    return {
      success: true,
      data: {
        id: user.id,
        type: user.type,
        phone: user.phone,
        nickname: user.nickname,
        avatar: user.avatar,
        createdAt: user.createdAt,
        lastActiveAt: user.lastActiveAt,
      },
    };
  }

  @Get(':id')
  async getUser(@Param('id') id: string) {
    const user = await this.userService.findById(id);
    return {
      success: true,
      data: {
        id: user.id,
        type: user.type,
        nickname: user.nickname,
        avatar: user.avatar,
        createdAt: user.createdAt,
      },
    };
  }

  @Put('me')
  async updateProfile(
    @Headers('x-user-id') userId: string,
    @Body() dto: UpdateProfileDto,
  ) {
    const user = await this.userService.updateProfile(userId, dto);
    return {
      success: true,
      data: {
        id: user.id,
        nickname: user.nickname,
        avatar: user.avatar,
      },
    };
  }
}
