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

/**
 * Scan & update slider titles for popular slider plugins
 * (Revolution Slider, Smart Slider 3).
 *
 * NOTE: This focuses on high-level textual fields like slider and slide titles.
 * Deep layer content is highly plugin-version-specific, so it is intentionally
 * left out to avoid corrupting slider structures.
 *
 * @since 2.3.0
 */
class AI_Magic_Sliders_Scanner {

    /**
     * Collect slider-related texts.
     *
     * @return array
     */
    public function get_items() {
        global $wpdb;

        $items = array();

        // Revolution Slider
        $table_rev_sliders = $wpdb->prefix . 'revslider_sliders';
        $table_rev_slides  = $wpdb->prefix . 'revslider_slides';

        $rev_sliders_table_exists = ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_rev_sliders ) ) === $table_rev_sliders );
        $rev_slides_table_exists  = ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_rev_slides ) ) === $table_rev_slides );

        if ( $rev_sliders_table_exists ) {
            $sliders = $wpdb->get_results( "SELECT id, title FROM {$table_rev_sliders}" );
            if ( ! empty( $sliders ) ) {
                foreach ( $sliders as $slider ) {
                    if ( ! is_string( $slider->title ) || $slider->title === '' ) {
                        continue;
                    }
                    $items[] = array(
                        'engine' => 'revslider',
                        'scope'  => 'slider_title',
                        'id'     => (int) $slider->id,
                        'value'  => $slider->title,
                    );
                }
            }
        }

        if ( $rev_slides_table_exists ) {
            $slides = $wpdb->get_results( "SELECT id, title FROM {$table_rev_slides}" );
            if ( ! empty( $slides ) ) {
                foreach ( $slides as $slide ) {
                    if ( ! is_string( $slide->title ) || $slide->title === '' ) {
                        continue;
                    }
                    $items[] = array(
                        'engine' => 'revslider',
                        'scope'  => 'slide_title',
                        'id'     => (int) $slide->id,
                        'value'  => $slide->title,
                    );
                }
            }
        }

        // Smart Slider 3
        $table_ss_sliders = $wpdb->prefix . 'nextend2_smartslider3_sliders';
        $table_ss_slides  = $wpdb->prefix . 'nextend2_smartslider3_slides';

        $ss_sliders_exists = ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_ss_sliders ) ) === $table_ss_sliders );
        $ss_slides_exists  = ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_ss_slides ) ) === $table_ss_slides );

        if ( $ss_sliders_exists ) {
            $sliders = $wpdb->get_results( "SELECT id, title FROM {$table_ss_sliders}" );
            if ( ! empty( $sliders ) ) {
                foreach ( $sliders as $slider ) {
                    if ( ! is_string( $slider->title ) || $slider->title === '' ) {
                        continue;
                    }
                    $items[] = array(
                        'engine' => 'smartslider',
                        'scope'  => 'slider_title',
                        'id'     => (int) $slider->id,
                        'value'  => $slider->title,
                    );
                }
            }
        }

        if ( $ss_slides_exists ) {
            $slides = $wpdb->get_results( "SELECT id, title FROM {$table_ss_slides}" );
            if ( ! empty( $slides ) ) {
                foreach ( $slides as $slide ) {
                    if ( ! is_string( $slide->title ) || $slide->title === '' ) {
                        continue;
                    }
                    $items[] = array(
                        'engine' => 'smartslider',
                        'scope'  => 'slide_title',
                        'id'     => (int) $slide->id,
                        'value'  => $slide->title,
                    );
                }
            }
        }

        return $items;
    }

    /**
     * Apply AI-generated values back into slider tables.
     *
     * @param array $items
     * @param array $new_values
     * @return void
     */
    public function apply_new_values( array $items, array $new_values ) {
        global $wpdb;

        $table_rev_sliders = $wpdb->prefix . 'revslider_sliders';
        $table_rev_slides  = $wpdb->prefix . 'revslider_slides';
        $table_ss_sliders  = $wpdb->prefix . 'nextend2_smartslider3_sliders';
        $table_ss_slides   = $wpdb->prefix . 'nextend2_smartslider3_slides';

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

            $engine = $item['engine'];
            $scope  = $item['scope'];
            $id     = (int) $item['id'];
            $value  = $new_values[ $index ];

            if ( $engine === 'revslider' ) {
                if ( $scope === 'slider_title' ) {
                    $wpdb->update(
                        $table_rev_sliders,
                        array( 'title' => $value ),
                        array( 'id'    => $id ),
                        array( '%s' ),
                        array( '%d' )
                    );
                } elseif ( $scope === 'slide_title' ) {
                    $wpdb->update(
                        $table_rev_slides,
                        array( 'title' => $value ),
                        array( 'id'    => $id ),
                        array( '%s' ),
                        array( '%d' )
                    );
                }
            } elseif ( $engine === 'smartslider' ) {
                if ( $scope === 'slider_title' ) {
                    $wpdb->update(
                        $table_ss_sliders,
                        array( 'title' => $value ),
                        array( 'id'    => $id ),
                        array( '%s' ),
                        array( '%d' )
                    );
                } elseif ( $scope === 'slide_title' ) {
                    $wpdb->update(
                        $table_ss_slides,
                        array( 'title' => $value ),
                        array( 'id'    => $id ),
                        array( '%s' ),
                        array( '%d' )
                    );
                }
            }
        }
    }

}
