import {
  Controller,
  Post,
  Get,
  Body,
  UseGuards,
  Request,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { AuthService } from './auth.service';
import { LoginDto } from './dto/login.dto';
import { Public } from './decorators/public.decorator';

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

  @Public()
  @UseGuards(AuthGuard('local'))
  @Post('login')
  async login(@Body() loginDto: LoginDto, @Request() req) {
    // 从请求头或请求体获取租户ID
    const tenantId = req.headers['x-tenant-id']
      ? parseInt(req.headers['x-tenant-id'], 10)
      : req.user?.tenantId;

    return this.authService.login(req.user, tenantId);
  }

  @UseGuards(AuthGuard('jwt'))
  @Get('user-info')
  async getUserInfo(@Request() req) {
    return this.authService.getUserInfo(req.user.userId);
  }

  @UseGuards(AuthGuard('jwt'))
  @Post('logout')
  async logout() {
    return { message: '登出成功' };
  }
}
