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

/**
 * Theme-wide wizard service that applies AI rewrite to:
 *  - Pages, posts and CPTs (via layout-safe content service)
 *  - Customizer / theme_mod texts
 *  - Widgets
 *  - Menus
 *  - Slider titles (Revolution Slider, Smart Slider 3)
 *  - ACF text fields
 *
 * @since 2.2.0
 */
class AI_Magic_Theme_Wizard_Service {

    /**
     * @var AI_Magic_Layout_AI
     */
    protected $ai;

    /**
     * @var AI_Magic_Content_Service
     */
    protected $content_service;

    /**
     * @param mixed $provider Provider instance
     */
    public function __construct( $provider ) {
        $this->ai              = new AI_Magic_Layout_AI( $provider );
        $this->content_service = new AI_Magic_Content_Service( $provider );
    }

    /**
     * Run theme wizard for selected scopes.
     *
     * @param string $prompt
     * @param array  $scopes e.g. array( 'content', 'customizer', 'widgets', 'menus', 'sliders', 'acf' )
     *
     * @return void
     */
    public function run( $prompt, array $scopes ) {

        if ( ! $this->ai->has_provider() ) {
            return;
        }

        $prompt = (string) $prompt;
        if ( trim( $prompt ) === '' ) {
            return;
        }

        // Normalize scope keys.
        $scopes = array_map( 'sanitize_key', $scopes );

        if ( in_array( 'content', $scopes, true ) ) {
            $this->update_all_content( $prompt );
        }

        if ( in_array( 'customizer', $scopes, true ) ) {
            $this->update_customizer( $prompt );
        }

        if ( in_array( 'widgets', $scopes, true ) ) {
            $this->update_widgets( $prompt );
        }

        if ( in_array( 'menus', $scopes, true ) ) {
            $this->update_menus( $prompt );
        }

        if ( in_array( 'sliders', $scopes, true ) ) {
            $this->update_sliders( $prompt );
        }

        if ( in_array( 'acf', $scopes, true ) ) {
            $this->update_acf( $prompt );
        }
    }

    /**
     * Update all posts/pages/CPTs via layout-safe content service.
     *
     * @param string $prompt
     * @return void
     */
    protected function update_all_content( $prompt ) {

        // Built-in post types.
        $post_types = array( 'post', 'page' );

        // Add public custom post types.
        $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 ) {

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

            $posts = get_posts( $query_args );

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

            foreach ( $posts as $post_id ) {
                $this->content_service->process_post( $post_id, $prompt );
            }
        }
    }

    /**
     * Update Customizer / theme_mod texts.
     *
     * @param string $prompt
     * @return void
     */
    protected function update_customizer( $prompt ) {
        if ( ! class_exists( 'AI_Magic_Customizer_Scanner' ) ) {
            return;
        }

        $scanner = new AI_Magic_Customizer_Scanner();
        $items   = $scanner->get_items();

        if ( empty( $items ) ) {
            return;
        }

        $new_values = $this->ai->rewrite_texts( $items, $prompt );
        if ( empty( $new_values ) ) {
            return;
        }

        $scanner->apply_new_values( $items, $new_values );
    }

    /**
     * Update widget texts.
     *
     * @param string $prompt
     * @return void
     */
    protected function update_widgets( $prompt ) {
        if ( ! class_exists( 'AI_Magic_Widgets_Scanner' ) ) {
            return;
        }

        $scanner = new AI_Magic_Widgets_Scanner();
        $items   = $scanner->get_items();

        if ( empty( $items ) ) {
            return;
        }

        $new_values = $this->ai->rewrite_texts( $items, $prompt );
        if ( empty( $new_values ) ) {
            return;
        }

        $scanner->apply_new_values( $items, $new_values );
    }

    /**
     * Update navigation menu labels.
     *
     * @param string $prompt
     * @return void
     */
    protected function update_menus( $prompt ) {
        if ( ! class_exists( 'AI_Magic_Menus_Scanner' ) ) {
            return;
        }

        $scanner = new AI_Magic_Menus_Scanner();
        $items   = $scanner->get_items();

        if ( empty( $items ) ) {
            return;
        }

        $new_values = $this->ai->rewrite_texts( $items, $prompt );
        if ( empty( $new_values ) ) {
            return;
        }

        $scanner->apply_new_values( $items, $new_values );
    }

    /**
     * Update slider titles (Revolution Slider, Smart Slider 3).
     *
     * @param string $prompt
     * @return void
     */
    protected function update_sliders( $prompt ) {
        if ( ! class_exists( 'AI_Magic_Sliders_Scanner' ) ) {
            return;
        }

        $scanner = new AI_Magic_Sliders_Scanner();
        $items   = $scanner->get_items();

        if ( empty( $items ) ) {
            return;
        }

        $new_values = $this->ai->rewrite_texts( $items, $prompt );
        if ( empty( $new_values ) ) {
            return;
        }

        $scanner->apply_new_values( $items, $new_values );
    }

    /**
     * Update ACF text fields across public post types.
     *
     * @param string $prompt
     * @return void
     */
    protected function update_acf( $prompt ) {
        if ( ! class_exists( 'AI_Magic_ACF_Scanner' ) ) {
            return;
        }

        $scanner = new AI_Magic_ACF_Scanner();
        $items   = $scanner->get_items();

        if ( empty( $items ) ) {
            return;
        }

        $new_values = $this->ai->rewrite_texts( $items, $prompt );
        if ( empty( $new_values ) ) {
            return;
        }

        $scanner->apply_new_values( $items, $new_values );
    }

}
