import { Controller, Get, Post, Request, UseGuards } from '@nestjs/common';
import { AuthService } from './auth.service';
import { LocalAuthGuard } from './local-auth.guard';
import { CombinedAuthGuard } from './combined-auth.guard';
import { Public } from './public.decorator';

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

  @Public()
  @UseGuards(LocalAuthGuard)
  @Post('login')
  async login(@Request() req) {
    return this.authService.login(req.user);
  }

  @UseGuards(CombinedAuthGuard)
  @Get('profile')
  getProfile(@Request() req) {
    return req.user;
  }

  @UseGuards(CombinedAuthGuard)
  @Get('api-key')
  async getApiKey(@Request() req) {
    const apiKey = await this.authService.getOrCreateApiKey(req.user.id);
    return { apiKey };
  }

  @UseGuards(CombinedAuthGuard)
  @Post('api-key/regenerate')
  async regenerateApiKey(@Request() req) {
    const apiKey = await this.authService.regenerateApiKey(req.user.id);
    return { apiKey };
  }
}
