import type { ChatBackendStreamEvent } from '../types/chatStream';
import type { V1PauseEvent } from './v1CommandRuntime';

export type V1ChatOutcome = 'paused' | 'final' | 'cancelled' | 'error';
export type V1StreamingChatOutcome = V1ChatOutcome | 'streaming';

export type V1FinalEvent = {
  type: 'v1.final';
  message?: string;
};

export type V1ErrorEvent =
  | { type: 'error'; message: string }
  | { type: 'v1.error'; message: string };

export type V1CancelledEvent = {
  type: 'v1.cancelled';
  pause_id?: string;
};

export type V1ChatResponseEvent =
  | ChatBackendStreamEvent
  | V1PauseEvent
  | V1FinalEvent
  | V1ErrorEvent
  | V1CancelledEvent;

export type V1ChatHttpResponse = {
  turn_id: string;
  outcome: V1ChatOutcome;
  events: V1ChatResponseEvent[];
};

export type V1ChatStreamEnvelope = {
  turn_id: string;
  outcome: V1StreamingChatOutcome;
  events: V1ChatResponseEvent[];
};

export const isV1PauseEvent = (event: V1ChatResponseEvent): event is V1PauseEvent => (
  event.type === 'v1.command_pause'
);

export const isV1FinalEvent = (event: V1ChatResponseEvent): event is V1FinalEvent => (
  event.type === 'v1.final'
);

export const isV1ErrorEvent = (event: V1ChatResponseEvent): event is V1ErrorEvent => (
  event.type === 'error' || event.type === 'v1.error'
);
