import { Controller, Get, Post, Put, Delete, Body, Param, Query, UseGuards, Request } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { MemberService } from '../../services/api/MemberService';
import { CreateMemberDto, UpdateProfileDto, ChangePasswordDto, ResetPasswordDto, SignDto } from '../../dto/api/MemberDto';

@ApiTags('前台-会员管理')
@ApiBearerAuth()
@Controller('api/member')
export class MemberController {
  constructor(private readonly memberService: MemberService) {}

  @Post('register')
  @ApiOperation({ summary: '会员注册' })
  @ApiResponse({ status: 201, description: '注册成功' })
  async register(@Body() createMemberDto: CreateMemberDto) {
    return await this.memberService.register(createMemberDto);
  }

  @Post('login')
  @ApiOperation({ summary: '会员登录' })
  @ApiResponse({ status: 200, description: '登录成功' })
  async login(@Body() loginDto: { username: string; password: string; ip?: string; address?: string; device?: string }) {
    return await this.memberService.login(loginDto);
  }

  @Get('profile')
  @ApiOperation({ summary: '获取个人资料' })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getProfile(@Request() req: any) {
    const memberId = req.user.member_id;
    return await this.memberService.getProfile(memberId);
  }

  @Put('profile')
  @ApiOperation({ summary: '更新个人资料' })
  @ApiResponse({ status: 200, description: '更新成功' })
  async updateProfile(@Request() req: any, @Body() updateProfileDto: UpdateProfileDto) {
    const memberId = req.user.member_id;
    return await this.memberService.updateProfile(memberId, updateProfileDto);
  }

  @Post('change-password')
  @ApiOperation({ summary: '修改密码' })
  @ApiResponse({ status: 200, description: '修改成功' })
  async changePassword(@Request() req: any, @Body() changePasswordDto: ChangePasswordDto) {
    const memberId = req.user.member_id;
    return await this.memberService.changePassword(memberId, changePasswordDto);
  }

  @Post('reset-password')
  @ApiOperation({ summary: '重置密码' })
  @ApiResponse({ status: 200, description: '重置成功' })
  async resetPassword(@Body() resetPasswordDto: ResetPasswordDto) {
    return await this.memberService.resetPassword(resetPasswordDto);
  }

  @Post('sign')
  @ApiOperation({ summary: '会员签到' })
  @ApiResponse({ status: 200, description: '签到成功' })
  async sign(@Request() req: any, @Body() signDto: SignDto) {
    const memberId = req.user.member_id;
    return await this.memberService.sign(memberId, signDto);
  }

  @Get('points/history')
  @ApiOperation({ summary: '获取积分历史' })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getPointsHistory(@Request() req: any, @Query() query: { page?: number; limit?: number }) {
    const memberId = req.user.member_id;
    return await this.memberService.getPointsHistory(memberId, query);
  }

  @Get('balance/history')
  @ApiOperation({ summary: '获取余额历史' })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getBalanceHistory(@Request() req: any, @Query() query: { page?: number; limit?: number }) {
    const memberId = req.user.member_id;
    return await this.memberService.getBalanceHistory(memberId, query);
  }

  @Get('address')
  @ApiOperation({ summary: '获取地址列表' })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getAddressList(@Request() req: any) {
    const memberId = req.user.member_id;
    return await this.memberService.getAddressList(memberId);
  }

  @Post('address')
  @ApiOperation({ summary: '添加地址' })
  @ApiResponse({ status: 201, description: '添加成功' })
  async addAddress(@Request() req: any, @Body() addressDto: any) {
    const memberId = req.user.member_id;
    return await this.memberService.addAddress(memberId, addressDto);
  }

  @Put('address/:id')
  @ApiOperation({ summary: '更新地址' })
  @ApiResponse({ status: 200, description: '更新成功' })
  async updateAddress(@Request() req: any, @Param('id') id: number, @Body() addressDto: any) {
    const memberId = req.user.member_id;
    return await this.memberService.updateAddress(memberId, id, addressDto);
  }

  @Delete('address/:id')
  @ApiOperation({ summary: '删除地址' })
  @ApiResponse({ status: 200, description: '删除成功' })
  async deleteAddress(@Request() req: any, @Param('id') id: number) {
    const memberId = req.user.member_id;
    return await this.memberService.deleteAddress(memberId, id);
  }

  @Post('address/:id/default')
  @ApiOperation({ summary: '设置默认地址' })
  @ApiResponse({ status: 200, description: '设置成功' })
  async setDefaultAddress(@Request() req: any, @Param('id') id: number) {
    const memberId = req.user.member_id;
    return await this.memberService.setDefaultAddress(memberId, id);
  }

  @Get('level')
  @ApiOperation({ summary: '获取会员等级信息' })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getMemberLevel(@Request() req: any) {
    const memberId = req.user.member_id;
    return await this.memberService.getMemberLevel(memberId);
  }

  @Get('logout')
  @ApiOperation({ summary: '会员登出' })
  @ApiResponse({ status: 200, description: '登出成功' })
  async logout(@Request() req: any) {
    const memberId = req.user.member_id;
    return await this.memberService.logout(memberId);
  }
} 