<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Scan & update widget texts for all active sidebars.
 *
 * @since 2.2.0
 */
class AI_Magic_Widgets_Scanner {

    /**
     * Collect widget text fields (title, text, content).
     *
     * @return array
     */
    public function get_items() {
        $items = array();

        $sidebars_widgets = wp_get_sidebars_widgets();

        if ( empty( $sidebars_widgets ) || ! is_array( $sidebars_widgets ) ) {
            return $items;
        }

        foreach ( $sidebars_widgets as $sidebar_id => $widget_ids ) {

            if ( ! is_array( $widget_ids ) ) {
                continue;
            }

            foreach ( $widget_ids as $widget_id ) {

                // ID like 'text-3', 'custom_html-2', etc.
                if ( ! is_string( $widget_id ) || strpos( $widget_id, '-' ) === false ) {
                    continue;
                }

                list( $base, $number ) = explode( '-', $widget_id, 2 );
                $number   = intval( $number );
                $opt_name = 'widget_' . $base;
                $widgets  = get_option( $opt_name );

                if ( empty( $widgets ) || ! is_array( $widgets ) || ! isset( $widgets[ $number ] ) ) {
                    continue;
                }

                $instance = $widgets[ $number ];
                if ( ! is_array( $instance ) ) {
                    continue;
                }

                foreach ( $instance as $field => $value ) {
                    if ( ! is_string( $value ) ) {
                        continue;
                    }

                    $raw = trim( wp_strip_all_tags( $value ) );
                    if ( $raw === '' || strlen( $raw ) < 3 ) {
                        continue;
                    }

                    $items[] = array(
                        'option_name' => $opt_name,
                        'number'      => $number,
                        'field'       => $field,
                        'value'       => $value,
                    );
                }
            }
        }

        return $items;
    }

    /**
     * Apply AI-generated values back into widget instances.
     *
     * @param array $items
     * @param array $new_values
     * @return void
     */
    public function apply_new_values( array $items, array $new_values ) {

        $grouped = array();

        // Group by option_name to avoid excessive get_option/update_option calls
        foreach ( $items as $index => $item ) {
            if ( ! isset( $new_values[ $index ] ) ) {
                continue;
            }
            $opt     = $item['option_name'];
            $number  = $item['number'];
            $field   = $item['field'];
            $new_val = $new_values[ $index ];

            if ( ! isset( $grouped[ $opt ] ) ) {
                $grouped[ $opt ] = array();
            }
            if ( ! isset( $grouped[ $opt ][ $number ] ) ) {
                $grouped[ $opt ][ $number ] = array();
            }
            $grouped[ $opt ][ $number ][ $field ] = $new_val;
        }

        foreach ( $grouped as $opt_name => $widget_updates ) {
            $widgets = get_option( $opt_name );
            if ( ! is_array( $widgets ) ) {
                continue;
            }

            foreach ( $widget_updates as $number => $fields ) {
                if ( ! isset( $widgets[ $number ] ) || ! is_array( $widgets[ $number ] ) ) {
                    continue;
                }
                foreach ( $fields as $field => $new_val ) {
                    $widgets[ $number ][ $field ] = $new_val;
                }
            }

            update_option( $opt_name, $widgets );
        }
    }

}
