/** -----------------------------
 *  Client-Events (Client → Server)
 *  ----------------------------- */
export enum RealtimeClientEvent {
    // Session / Config
    SESSION_UPDATE = "session.update",

    // Input-Audio-Buffer (Client → Server)
    INPUT_AUDIO_BUFFER_APPEND = "input_audio_buffer.append",
    INPUT_AUDIO_BUFFER_COMMIT = "input_audio_buffer.commit",
    INPUT_AUDIO_BUFFER_CLEAR = "input_audio_buffer.clear",

    // WebRTC-spezifischer Output-Buffer (Client → Server)
    OUTPUT_AUDIO_BUFFER_CLEAR = "output_audio_buffer.clear",

    // Conversation-Items (Client → Server)
    CONVERSATION_ITEM_CREATE = "conversation.item.create",
    CONVERSATION_ITEM_DELETE = "conversation.item.delete",
    CONVERSATION_ITEM_RETRIEVE = "conversation.item.retrieve",
    CONVERSATION_ITEM_TRUNCATE = "conversation.item.truncate",

    // Inferenzsteuerung
    RESPONSE_CREATE = "response.create",
    RESPONSE_CANCEL = "response.cancel",
}

/** ----------------------------
 *  Server-Events (Server → Client)
 *  ---------------------------- */
export enum RealtimeServerEvent {
    // Session / Conversation
    SESSION_CREATED = "session.created",
    SESSION_UPDATED = "session.updated",
    CONVERSATION_CREATED = "conversation.created",

    // Conversation-Items (Server → Client)
    CONVERSATION_ITEM_DONE = "conversation.item.done",
    CONVERSATION_ITEM_RETRIEVED = "conversation.item.retrieved",
    CONVERSATION_ITEM_DELETED = "conversation.item.deleted",
    CONVERSATION_ITEM_TRUNCATED = "conversation.item.truncated",
    CONVERSATION_ITEM_INPUT_AUDIO_TRANSCRIPTION_COMPLETED = "conversation.item.input_audio_transcription.completed",
    CONVERSATION_ITEM_INPUT_AUDIO_TRANSCRIPTION_FAILED = "conversation.item.input_audio_transcription.failed",

    // Input-Audio-Buffer (Server → Client)
    INPUT_AUDIO_BUFFER_CLEARED = "input_audio_buffer.cleared",
    INPUT_AUDIO_BUFFER_COMMITTED = "input_audio_buffer.committed",
    INPUT_AUDIO_BUFFER_SPEECH_STARTED = "input_audio_buffer.speech_started",
    INPUT_AUDIO_BUFFER_SPEECH_STOPPED = "input_audio_buffer.speech_stopped",

    // WebRTC-spezifischer Output-Buffer (Server → Client)
    OUTPUT_AUDIO_BUFFER_CLEARED = "output_audio_buffer.cleared",
    OUTPUT_AUDIO_BUFFER_STARTED = "output_audio_buffer.started",
    OUTPUT_AUDIO_BUFFER_STOPPED = "output_audio_buffer.stopped",

    // Rate Limits
    RATE_LIMITS_UPDATED = "rate_limits.updated",

    // Response-Lifecycle (Server → Client)
    RESPONSE_CREATED = "response.created",
    RESPONSE_OUTPUT_ITEM_ADDED = "response.output_item.added",
    RESPONSE_OUTPUT_ITEM_DONE = "response.output_item.done",
    RESPONSE_CONTENT_PART_ADDED = "response.content_part.added",
    RESPONSE_CONTENT_PART_DONE = "response.content_part.done",

    // Streaming-Deltas
    RESPONSE_TEXT_DELTA = "response.output_text.delta",
    RESPONSE_TEXT_DONE = "response.output_text.done",
    RESPONSE_AUDIO_DELTA = "response.output_audio.delta",
    RESPONSE_AUDIO_DONE = "response.output_audio.done",
    RESPONSE_AUDIO_TRANSCRIPT_DELTA = "response.output_audio_transcript.delta",
    RESPONSE_AUDIO_TRANSCRIPT_DONE = "response.output_audio_transcript.done",
    RESPONSE_FUNCTION_CALL_ARGUMENTS_DELTA = "response.function_call_arguments.delta",
    RESPONSE_FUNCTION_CALL_ARGUMENTS_DONE = "response.function_call_arguments.done",

    // Abschluss / Abbruch / Fehler
    RESPONSE_DONE = "response.done",
    RESPONSE_CANCELLED = "response.cancelled",
    ERROR = "error",

    // Graph Synchronisation
    GRAPH_CURSOR = "graph_cursor",
}

/** Optional: gemeinsame Typen */
export type AnyRealtimeEvent = RealtimeClientEvent | RealtimeServerEvent
export function extractSessionId(path: string): string | null {
    const pathRegex = /^\/develop\/chats\/([^\/]+)(?:\/|$)/
    const match = path.match(pathRegex)
    return match && match[1] !== "new" ? match[1] : null
}
