import { Injectable, signal } from '@angular/core';
import { AgentCardData, AgentStatus, AgentStatusUpdate, TaskProgressUpdate } from '../models/agent.model';

/**
 * Agent Status Service — stub for future SignalR integration.
 * Per spec Section 7.4: Connects to /hubs/agent-status for real-time updates.
 *
 * TODO: Implement SignalR hub connection when backend is ready.
 * TODO: Wire up NgRx store or signals for reactive state management.
 */
@Injectable({ providedIn: 'root' })
export class AgentStatusService {
  /** Stub: list of agents (will come from SignalR) */
  private readonly _agents = signal<AgentCardData[]>([]);

  readonly agents = this._agents.asReadonly();

  /** Stub: update an agent's status */
  updateStatus(update: AgentStatusUpdate): void {
    this._agents.update(agents =>
      agents.map(agent =>
        agent.id === update.agentId
          ? { ...agent, status: update.status }
          : agent
      )
    );
  }

  /** Stub: update an agent's task progress */
  updateTaskProgress(progress: TaskProgressUpdate): void {
    this._agents.update(agents =>
      agents.map(agent =>
        agent.id === progress.agentId
          ? {
              ...agent,
              taskProgress: progress.progress,
              ...(progress.taskName ? { currentTask: progress.taskName } : {}),
              ...(progress.elapsed ? { taskElapsed: progress.elapsed } : {}),
            }
          : agent
      )
    );
  }

  // TODO: connect() — Initialize SignalR connection
  // TODO: disconnect() — Clean up SignalR connection
}