<?php
/**
 * Compacts queued actor context to the durable fields needed for continuation.
 */

defined( 'ABSPATH' ) || exit;

class Niwa_AI_Admin_Autonomy_Context_Compactor {

    private const ALLOWED_KEYS = array(
        'attachment_image_url',
        'attachment_image_urls',
        'attachment_image_attachment_id',
        'attachment_image_attachment_ids',
        'attachment_documents',
        'attachment_placeholders',
        'document_excerpts',
        'sticker_descriptions',
        'incoming_media',
        'reply_image_urls',
        'caption',
        'forward_marker',
        'location_text',
        'contact_text',
        'is_album',
        'album_size',
        'is_text_fragment',
        'fragment_count',
        'is_edited',
        'edited_marker',
        'image_warning',
        'image_error',
        'skill_import_content',
        'skill_import_archive_base64',
        'skill_import_filename',
        'force_action',
        'remember_as',
        'suppress_image_analysis',
        'remote_jid',
        'phone_jid',
        'chat_jid',
        'sender_jid',
        'whatsapp_jid',
        'gateway_chat_jid',
        'untrusted_external_input',
        'tool_policy',
        'inbound_email',
    );

    public function compact( $context ) {
        $context = is_array( $context ) ? $context : array();
        $payload = array();

        foreach ( self::ALLOWED_KEYS as $key ) {
            if ( array_key_exists( $key, $context ) ) {
                $payload[ $key ] = $context[ $key ];
            }
        }

        return $payload;
    }
}
