import { Controller, Post, Body, UseGuards, Get } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { MerchantAuthService } from './auth.service';
import {
  LoginDto,
  MerchantRegisterDto,
  SendCodeDto,
} from './dto/auth.dto';
import { SellerJwtAuthGuard } from '@/common/guards/seller-jwt-auth.guard';
import { CurrentSeller } from '@/common/decorators/current-seller.decorator';

@ApiTags('商家认证')
@Controller('merchant/auth')
export class MerchantAuthController {
  constructor(private readonly authService: MerchantAuthService) {}

  @Post('send-code')
  @ApiOperation({ summary: '发送商家验证码' })
  async sendCode(@Body() dto: SendCodeDto) {
    return this.authService.sendCode(dto.phone);
  }

  @Post('login')
  @ApiOperation({ summary: '商家登录（验证码或密码）' })
  async login(@Body() dto: LoginDto) {
    return this.authService.login(dto);
  }

  @Post('register')
  @ApiOperation({ summary: '商家注册（验证码）' })
  async register(@Body() dto: MerchantRegisterDto) {
    return this.authService.register(dto);
  }

  @Post('refresh')
  @ApiOperation({ summary: '刷新令牌' })
  async refresh(@Body('refreshToken') refreshToken: string) {
    return this.authService.refresh(refreshToken);
  }

  @Get('profile')
  @UseGuards(SellerJwtAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: '获取当前商家信息' })
  async getProfile(@CurrentSeller('sub') sellerId: number) {
    return this.authService.findById(sellerId);
  }
}
