import {
  Controller,
  Delete,
  Get,
  HttpCode,
  HttpStatus,
  Inject,
  NotFoundException,
  Param,
  UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '../auth/auth.guard.js';
import { AgentService } from './agent.service.js';

@Controller('api/sessions')
@UseGuards(AuthGuard)
export class SessionsController {
  constructor(@Inject(AgentService) private readonly agentService: AgentService) {}

  @Get()
  list() {
    const sessions = this.agentService.listSessions();
    return { sessions, total: sessions.length };
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    const info = this.agentService.getSessionInfo(id);
    if (!info) throw new NotFoundException('Session not found');
    return info;
  }

  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  async destroy(@Param('id') id: string) {
    const info = this.agentService.getSessionInfo(id);
    if (!info) throw new NotFoundException('Session not found');
    await this.agentService.destroySession(id);
  }
}
