import { Injectable, NotFoundException } from '@nestjs/common';
import { SessionRepository } from '../../infrastructure/repositories/session.repository';
import { TaskRepository } from '../../infrastructure/repositories/task.repository';
import { AgentSession } from '../../domain/entities/agent-session.entity';
import { AgentTask } from '../../domain/entities/agent-task.entity';

export interface SessionHistoryResult {
  session: AgentSession;
  tasks: AgentTask[];
}

@Injectable()
export class GetSessionHistoryUseCase {
  constructor(
    private readonly sessionRepository: SessionRepository,
    private readonly taskRepository: TaskRepository,
  ) {}

  async execute(sessionId: string): Promise<SessionHistoryResult> {
    const session = await this.sessionRepository.findById(sessionId);
    if (!session) {
      throw new NotFoundException(`Session ${sessionId} not found`);
    }

    const tasks = await this.taskRepository.findBySessionId(sessionId);

    return { session, tasks };
  }

  async listByTenant(tenantId: string): Promise<AgentSession[]> {
    return this.sessionRepository.findByTenant(tenantId);
  }
}
