<?php
/**
 * Lightweight Telegram message normalization before actor enqueue/execution.
 */

defined( 'ABSPATH' ) || exit;

class Niwa_AI_Admin_Autonomy_Telegram_Message_Preprocessor {

    public function media_kinds( $message ) {
        $message = is_array( $message ) ? $message : array();
        if ( empty( $message['media_refs'] ) || ! is_array( $message['media_refs'] ) ) {
            return array();
        }

        $kinds = array();
        foreach ( $message['media_refs'] as $ref ) {
            if ( is_array( $ref ) && ! empty( $ref['kind'] ) ) {
                $kinds[] = (string) $ref['kind'];
            }
        }

        return $kinds;
    }

    public function apply_stock_manager_media_policy( $message ) {
        $message   = is_array( $message ) ? $message : array();
        $user_role = isset( $message['user_role'] ) ? (string) $message['user_role'] : 'admin';

        if ( 'stock_manager' !== $user_role || empty( $message['media_refs'] ) || ! is_array( $message['media_refs'] ) ) {
            return $message;
        }

        $message['media_refs'] = array_values( array_filter(
            $message['media_refs'],
            array( $this, 'is_allowed_stock_manager_media_ref' )
        ) );

        return $message;
    }

    public function is_media_only_message( $message ) {
        if ( '' !== trim( (string) ( $message['text_only'] ?? $message['text'] ?? '' ) ) && '' === (string) ( $message['caption'] ?? '' ) ) {
            return false;
        }

        foreach ( $this->media_refs( $message ) as $ref ) {
            $kind = (string) ( $ref['kind'] ?? '' );
            if ( 'image' === $kind ) {
                return true;
            }
            if ( 'document' === $kind && 0 === strpos( (string) ( $ref['mime_hint'] ?? '' ), 'image/' ) ) {
                return true;
            }
        }

        return false;
    }

    public function find_first_voice_ref( $message ) {
        foreach ( $this->media_refs( $message ) as $ref ) {
            if (
                empty( $ref['file_id'] )
                || '' !== trim( (string) ( $ref['audio_transcript'] ?? '' ) )
                || array_key_exists( 'audio_transcription_error', $ref )
            ) {
                continue;
            }
            if ( Niwa_AI_Admin_Autonomy_Audio_Utils::is_audio_media_ref( $ref ) ) {
                return $ref;
            }
        }

        return null;
    }

    public function find_first_skill_document_ref( $message ) {
        foreach ( $this->media_refs( $message ) as $ref ) {
            if ( empty( $ref['file_id'] ) || 'document' !== (string) ( $ref['kind'] ?? '' ) ) {
                continue;
            }
            if ( 0 === strpos( (string) ( $ref['mime_hint'] ?? '' ), 'image/' ) ) {
                continue;
            }
            $name = (string) ( $ref['file_name'] ?? '' );
            if ( '' === $name ) {
                continue;
            }
            $ext = strtolower( pathinfo( $name, PATHINFO_EXTENSION ) );
            if ( in_array( $ext, array( 'md', 'zip', 'skill' ), true ) ) {
                return $ref;
            }
        }

        return null;
    }

    public function find_first_spreadsheet_ref( $message ) {
        foreach ( $this->media_refs( $message ) as $ref ) {
            if ( empty( $ref['file_id'] ) || 'document' !== (string) ( $ref['kind'] ?? '' ) ) {
                continue;
            }
            $name = (string) ( $ref['file_name'] ?? '' );
            if ( '' === $name ) {
                continue;
            }
            $ext = strtolower( pathinfo( $name, PATHINFO_EXTENSION ) );
            if ( in_array( $ext, array( 'csv', 'xlsx' ), true ) ) {
                return $ref;
            }
        }

        return null;
    }

    private function is_allowed_stock_manager_media_ref( $ref ) {
        if ( ! is_array( $ref ) || 'document' !== ( $ref['kind'] ?? '' ) ) {
            return true;
        }
        if ( Niwa_AI_Admin_Autonomy_Audio_Utils::is_audio_media_ref( $ref ) ) {
            return true;
        }

        $name = (string) ( $ref['file_name'] ?? '' );
        if ( '' === $name ) {
            return false;
        }

        $ext = strtolower( pathinfo( $name, PATHINFO_EXTENSION ) );
        return in_array( $ext, array( 'csv', 'xlsx' ), true );
    }

    private function media_refs( $message ) {
        $message = is_array( $message ) ? $message : array();
        $refs    = isset( $message['media_refs'] ) && is_array( $message['media_refs'] ) ? $message['media_refs'] : array();
        return array_values( array_filter( $refs, 'is_array' ) );
    }
}
