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

/**
 * Scan & update ACF (Advanced Custom Fields) text-like fields
 * across all public post types.
 *
 * This relies on ACF's get_fields() if available.
 *
 * @since 2.3.0
 */
class AI_Magic_ACF_Scanner {

    /**
     * Collect ACF text field values across all posts of all public post types.
     *
     * @return array
     */
    public function get_items() {

        $items = array();

        if ( ! function_exists( 'get_fields' ) ) {
            return $items;
        }

        // Built-in + public CPTs
        $post_types = array( 'post', 'page' );
        $cpts       = get_post_types(
            array(
                'public'   => true,
                '_builtin' => false,
            ),
            'names'
        );

        if ( is_array( $cpts ) ) {
            $post_types = array_merge( $post_types, $cpts );
        }
        $post_types = array_unique( $post_types );

        foreach ( $post_types as $pt ) {

            $posts = get_posts(
                array(
                    'post_type'      => $pt,
                    'post_status'    => 'publish',
                    'posts_per_page' => -1,
                    'fields'         => 'ids',
                )
            );

            if ( empty( $posts ) ) {
                continue;
            }

            foreach ( $posts as $post_id ) {

                $fields = get_fields( $post_id );
                if ( empty( $fields ) || ! is_array( $fields ) ) {
                    continue;
                }

                foreach ( $fields as $field_key => $value ) {

                    // Only simple string fields are processed (text/textarea/wysiwyg).
                    if ( ! is_string( $value ) ) {
                        continue;
                    }

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

                    $items[] = array(
                        'post_id'   => (int) $post_id,
                        'meta_key'  => $field_key,
                        'value'     => $value,
                    );
                }
            }
        }

        return $items;
    }

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

        foreach ( $items as $index => $item ) {
            if ( ! isset( $new_values[ $index ] ) ) {
                continue;
            }

            $post_id  = (int) $item['post_id'];
            $meta_key = $item['meta_key'];
            $new_val  = $new_values[ $index ];

            if ( function_exists( 'update_field' ) ) {
                // Try ACF API first (field name or key).
                @update_field( $meta_key, $new_val, $post_id );
            } else {
                update_post_meta( $post_id, $meta_key, $new_val );
            }
        }
    }

}
