import { Controller, Get, Req } from '@nestjs/common';
import { TurnService } from './turn.service';

@Controller('turn')
export class TurnController {
  constructor(private readonly turn: TurnService) {}

  /**
   * GET /api/turn/credentials
   *
   * Protected by your existing global JwtAuthGuard — only logged-in users
   * can obtain relay credentials.
   */
  @Get('credentials')
  credentials(@Req() req: any) {
    const userId = req.user?.id ?? req.user?.sub ?? 'anon';
    const iceServers = this.turn.issue(userId);
    return {
      iceServers,
      // clients should refetch a little before this
      expiresIn: 4 * 3600,
    };
  }
}
