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

/**
 * High-level service that:
 *  - Detects editor (Elementor / Gutenberg / WPBakery / Classic HTML).
 *  - Extracts texts.
 *  - Sends them to AI.
 *  - Writes back AI-generated texts without breaking layout.
 *
 * @since 2.1.0
 * @since 2.1.3 Added Classic HTML processor support
 */
class AI_Magic_Content_Service
{

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

    /**
     * @param mixed $provider Provider instance (OpenAI/Google/Anthropic, etc.)
     */
    public function __construct($provider = null)
    {
        $this->ai = new AI_Magic_Layout_AI($provider);
    }

    /**
     * Process a single post.
     *
     * Detects the editor type and uses the appropriate processor
     * to safely extract and update text content.
     *
     * @param int    $post_id
     * @param string $prompt
     *
     * @return bool True if processed, false otherwise
     */
    public function process_post($post_id, $prompt)
    {

        $post_id = absint($post_id);
        if (!$post_id || !$this->ai->has_provider()) {
            return false;
        }

        $editor = AI_Magic_Editor_Detector::detect_editor($post_id);

        if ($editor === 'elementor') {
            return $this->process_elementor($post_id, $prompt);
        } elseif ($editor === 'gutenberg') {
            return $this->process_gutenberg($post_id, $prompt);
        } elseif ($editor === 'wpbakery') {
            return $this->process_wpbakery($post_id, $prompt);
        } else {
            // Classic editor / plain HTML
            return $this->process_classic($post_id, $prompt);
        }
    }

    /**
     * Process Elementor content.
     *
     * @param int    $post_id
     * @param string $prompt
     * @return bool
     */
    protected function process_elementor($post_id, $prompt)
    {
        $processor = new AI_Magic_Elementor_Processor();
        $elements = $processor->get_raw_data($post_id);

        if (empty($elements)) {
            return false;
        }

        $texts = array();
        $texts = $processor->extract_texts($elements, $texts);

        if (empty($texts)) {
            return false;
        }

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

        $processor->apply_new_texts($texts, $new_values);
        $processor->save_raw_data($post_id, $elements);

        return true;
    }

    /**
     * Process Gutenberg content.
     *
     * @param int    $post_id
     * @param string $prompt
     * @return bool
     */
    protected function process_gutenberg($post_id, $prompt)
    {
        $processor = new AI_Magic_Gutenberg_Processor();
        $content = $processor->get_content($post_id);

        $texts = $processor->extract_paragraphs($content);
        if (empty($texts)) {
            return false;
        }

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

        $new_content = $processor->apply_new_paragraphs($content, $texts, $new_values);
        $processor->save_content($post_id, $new_content);

        return true;
    }

    /**
     * Process WPBakery content.
     *
     * @param int    $post_id
     * @param string $prompt
     * @return bool
     */
    protected function process_wpbakery($post_id, $prompt)
    {
        $processor = new AI_Magic_WPBakery_Processor();
        $content = $processor->get_content($post_id);

        $texts = $processor->extract_vc_texts($content);
        if (empty($texts)) {
            return false;
        }

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

        $new_content = $processor->apply_new_vc_texts($content, $texts, $new_values);
        $processor->save_content($post_id, $new_content);

        return true;
    }

    /**
     * Process Classic Editor (TinyMCE) HTML content.
     *
     * @since 2.1.3
     * @param int    $post_id
     * @param string $prompt
     * @return bool
     */
    protected function process_classic($post_id, $prompt)
    {
        $processor = new AI_Magic_Classic_Processor();
        $content = $processor->get_content($post_id);

        if (empty($content)) {
            return false;
        }

        $texts = $processor->extract_texts($content);
        if (empty($texts)) {
            return false;
        }

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

        $new_content = $processor->apply_new_texts($content, $texts, $new_values);
        $processor->save_content($post_id, $new_content);

        return true;
    }

}
