import { Controller, Get, Patch, Post, Delete, Body, Param, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { UsersService } from './users.service';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import type { UserPayload } from '../../common/types';

@ApiTags('users')
@Controller('users')
@ApiBearerAuth()
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  // ── Profile ──

  @Get('me')
  @ApiOperation({ summary: '获取当前用户信息' })
  async getProfile(@CurrentUser() user: UserPayload) {
    return this.usersService.getProfile(String(user.id));
  }

  @Patch('me')
  @ApiOperation({ summary: '更新用户资料' })
  async updateProfile(@CurrentUser() user: UserPayload, @Body() body: any) {
    return this.usersService.updateProfile(String(user.id), body);
  }

  @Get('me/profile')
  @ApiOperation({ summary: '获取用户学习档案' })
  async getProfileDetail(@CurrentUser() user: UserPayload) {
    return this.usersService.getProfileDetail(String(user.id));
  }

  @Patch('me/profile')
  @ApiOperation({ summary: '更新用户学习档案' })
  async updateProfileDetail(@CurrentUser() user: UserPayload, @Body() body: any) {
    return this.usersService.updateProfileDetail(String(user.id), body);
  }

  @Patch('me/preferences')
  @ApiOperation({ summary: '更新用户偏好' })
  async updatePreferences(@CurrentUser() user: UserPayload, @Body() body: any) {
    return this.usersService.updatePreferences(String(user.id), body);
  }

  // ── Membership ──

  @Get('me/membership')
  @ApiOperation({ summary: '查询当前会员状态' })
  async myMembership(@CurrentUser() user: UserPayload) {
    return this.usersService.getMembership(String(user.id));
  }

  // ── Account Deletion ──

  @Post('me/deletion-request')
  @ApiOperation({ summary: '申请账号注销' })
  async requestDeletion(@CurrentUser() user: UserPayload) {
    return this.usersService.requestDeletion(String(user.id));
  }

  @Delete('me/deletion-request')
  @ApiOperation({ summary: '撤销注销申请' })
  async cancelDeletion(@CurrentUser() user: UserPayload) {
    return this.usersService.cancelDeletion(String(user.id));
  }

  // ── Device Management ──

  @Get('me/devices')
  @ApiOperation({ summary: '我的设备列表' })
  async myDevices(@CurrentUser() user: UserPayload) {
    return this.usersService.getDevices(String(user.id));
  }

  @Delete('me/devices/:deviceId')
  @ApiOperation({ summary: '远程登出设备' })
  async removeDevice(@CurrentUser() user: UserPayload, @Param('deviceId') deviceId: string) {
    return this.usersService.removeDevice(String(user.id), deviceId);
  }

  // ── Storage & Assets ──

  @Get('me/storage')
  @ApiOperation({ summary: '存储空间统计' })
  async getStorage(@CurrentUser() user: UserPayload) {
    return this.usersService.getStorage(String(user.id));
  }

  @Get('me/assets-summary')
  @ApiOperation({ summary: '学习资产摘要' })
  async getAssetsSummary(@CurrentUser() user: UserPayload) {
    return this.usersService.getAssetsSummary(String(user.id));
  }
}
