import { Controller, Get, Param, NotFoundException } from '@nestjs/common';
import { TenantId } from '@it0/common';
import { SessionRepository } from '../../../infrastructure/repositories/session.repository';
import { TaskRepository } from '../../../infrastructure/repositories/task.repository';

@Controller('api/v1/agent/sessions')
export class SessionController {
  constructor(
    private readonly sessionRepository: SessionRepository,
    private readonly taskRepository: TaskRepository,
  ) {}

  // TODO 15: List sessions
  @Get()
  async listSessions(@TenantId() tenantId: string) {
    return this.sessionRepository.findByTenant(tenantId);
  }

  // TODO 16: Get session details
  @Get(':sessionId')
  async getSession(@Param('sessionId') sessionId: string) {
    const session = await this.sessionRepository.findById(sessionId);
    if (!session) {
      throw new NotFoundException(`Session ${sessionId} not found`);
    }
    return session;
  }

  // TODO 17: Get session history
  @Get(':sessionId/history')
  async getSessionHistory(@Param('sessionId') sessionId: string) {
    return this.taskRepository.findBySessionId(sessionId);
  }
}
