import { Controller, Post, Body, Req, HttpCode, HttpStatus } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { AuthService } from '../../application/services/auth.service';
import { Public } from '../../shared/guards/admin-auth.guard';

class LoginDto { username: string; password: string; }

@ApiTags('Auth')
@Controller('auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  @Post('login')
  @Public()
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: '管理员登录' })
  async login(@Body() dto: LoginDto, @Req() req: any) {
    return this.authService.login(dto.username, dto.password, req.ip, req.headers['user-agent']);
  }

  @Post('logout')
  @ApiBearerAuth()
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: '退出登录' })
  async logout(@Req() req: any) {
    await this.authService.logout(req.admin.id);
    return { success: true };
  }
}
