import { Controller, Get, Put, Body, Param, Query, UseGuards, Req } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { UserProfileService } from '../../../application/services/user-profile.service';
import { UpdateProfileDto } from '../dto/update-profile.dto';
import { UpdateSettingsDto } from '../dto/update-settings.dto';

@ApiTags('Users')
@Controller('users')
export class UserController {
  constructor(private readonly profileService: UserProfileService) {}

  @Get('me')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get current user profile' })
  async getMyProfile(@Req() req: any) {
    const profile = await this.profileService.getProfile(req.user.id);
    return { code: 0, data: profile };
  }

  @Put('me')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Update current user profile' })
  async updateMyProfile(@Req() req: any, @Body() dto: UpdateProfileDto) {
    const profile = await this.profileService.updateProfile(req.user.id, dto);
    return { code: 0, data: profile };
  }

  @Get('me/settings')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get current user settings' })
  async getMySettings(@Req() req: any) {
    const settings = await this.profileService.getSettings(req.user.id);
    return { code: 0, data: settings };
  }

  @Put('me/settings')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Update current user settings (language, currency, notification preferences)' })
  async updateMySettings(@Req() req: any, @Body() dto: UpdateSettingsDto) {
    const settings = await this.profileService.updateSettings(req.user.id, dto);
    return { code: 0, data: settings };
  }

  @Get(':id')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get user profile by ID' })
  async getProfile(@Param('id') id: string) {
    const profile = await this.profileService.getProfile(id);
    return { code: 0, data: profile };
  }
}
