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

/**
 * Запрос к API OpenAI для генерации outline (плана статьи).
 */
if (!function_exists('request_article_outline')) {
    function request_article_outline($api_key, $text_model, $outline_prompt) {
        $response = wp_remote_post('https://api.openai.com/v1/chat/completions', [
            'headers' => [
                'Authorization' => 'Bearer ' . $api_key,
                'Content-Type'  => 'application/json',
            ],
            'body'    => json_encode([
                'model'    => $text_model,
                'messages' => [
                    ['role' => 'user', 'content' => $outline_prompt]
                ],
            ]),
            'timeout' => 1000
        ]);

        if (is_wp_error($response)) {
            error_log('Ошибка при запросе outline: ' . $response->get_error_message());
            return 'No outline generated';
        }

        $result = json_decode(wp_remote_retrieve_body($response), true);
        return $result['choices'][0]['message']['content'] ?? 'No outline generated';
    }
}

/**
 * Запрос к API OpenAI для генерации финального контента статьи.
 */
if (!function_exists('request_content_generation')) {
    function request_content_generation($api_key, $text_model, $prompt_article) {
        $response = wp_remote_post('https://api.openai.com/v1/chat/completions', [
            'headers' => [
                'Authorization' => 'Bearer ' . $api_key,
                'Content-Type'  => 'application/json',
            ],
            'body' => json_encode([
                'model' => $text_model,
                'messages' => [['role' => 'user', 'content' => $prompt_article]],
            ]),
            'timeout' => 1000
        ]);

        if (is_wp_error($response)) {
            error_log('Ошибка при запросе генерации контента: ' . $response->get_error_message());
            return 'No content generated';
        }

        $result = json_decode(wp_remote_retrieve_body($response), true);
        return $result['choices'][0]['message']['content'] ?? 'No content generated';
    }
}

/**
 * Основная функция для генерации статьи с учётом outline, внешних ссылок и контроля плотности ключевого слова.
 */
function generate_article_content(
    $post_topic,
    $category_name,
    $selected_language,
    $keyword,
    $format,
    $text_length,
    $site_type,
    $writing_style,
    $target_audience,
    $custom_theme,
    $additional_requests,
    $single_post_additional_request,
    $related_articles_context,
    $api_key,
    $text_model,
    $target_region
) {
    // Приводим $text_length к диапазону [500..10000] слов
    if ($text_length < 500) {
        $text_length = 500;
    } elseif ($text_length > 10000) {
        $text_length = 10000;
    }

    // Рассчитываем, сколько будет основных и дополнительных секций исходя из объёма
    $factor = ($text_length - 500) / (10000 - 500);  // 0..1
    if ($factor < 0) $factor = 0;
    if ($factor > 1) $factor = 1;

    // При небольшом объёме статей делаем меньше секций, при большом — больше
    $main_sections_min = round(2 + (4 - 2) * $factor);  // от 2 до 4
    $main_sections_max = round(4 + (6 - 4) * $factor);  // от 4 до 6

    $subsections_min = round(1 + (2 - 1) * $factor);    // от 1 до 2
    $subsections_max = round(2 + (3 - 2) * $factor);    // от 2 до 3

    // Получаем «полное» название языка
    $language_full_name = get_full_language_name($selected_language);

    /*
     * Шаг 1: Генерируем план (outline) статьи
     */
    $outline_prompt = "
You are an expert researcher and professional writer.
Create a concise outline (table of contents) for an in-depth article in '{$language_full_name}' 
on the topic: '{$post_topic}', appropriate for the '{$category_name}' category. 
The article should be aimed at '{$target_audience}' in '{$target_region}', 
with a target length of approximately {$text_length} words. 
Organize the outline into {$main_sections_min} to {$main_sections_max} main sections, 
each with {$subsections_min} to {$subsections_max} subsections. 
Incorporate the keyword '{$keyword}' where relevant, 
and provide clear, structured points for each section. 
Do not generate the full article here — ONLY the outline (like a table of contents).";

    // Генерируем outline
    $outline = request_article_outline($api_key, $text_model, $outline_prompt);

    // Логируем outline, чтобы видеть какой он сформировался и при каком объёме
    //error_log('[openai_generate_content] Generated outline for topic "' . $post_topic . '" at length ' . $text_length . ' words:');
    //error_log($outline);

    /*
     * Шаг 2: Формируем окончательный промпт для генерации всей статьи
     */
    $final_prompt = "
Using the following outline:
------------------------------
{$outline}
------------------------------
Write a complete, detailed, SEO-optimized '{$format}' in '{$language_full_name}' about '{$post_topic}', 
suitable for the '{$category_name}' category. 
Ensure the content is approximately {$text_length} words long, thoroughly covering each section 
from the outline without adding or removing major points.

Requirements:
1. Use <h2> for main headings (sections) and <h3> for subsections (avoid numbering).
2. Do NOT use <h1> inside the article.
3. Each paragraph should be wrapped in <p> tags and contain at least three sentences.
4. Use only unordered lists (<ul><li>) for bullet points, no numbered lists.
5. The keyword '{$keyword}' must appear in the first paragraph and in at least one <h3> subheading.
6. The keyword must have a density between 1.0% and 1.5% based on the total word count of the article.
7. Include at least two internal links related to '{$related_articles_context}' with varied anchor texts in '{$language_full_name}'.
8. Include at least one external link to an authoritative website or source (for example, a well-known organization or recognized publication).
9. Maintain an authoritative and informative tone, reflecting a '{$writing_style}' style.
10. Make the article appealing and understandable for '{$target_audience}' in '{$target_region}'.
11. The article should begin with the keyword and end organically after the last section, without a separate summary or mention of content generation process.

Self-check Instructions (mandatory):
- Verify that the keyword appears in the first paragraph and at least one <h3> subheading.
- Calculate the keyword density; it must be no less than 1.0% and no more than 1.5% of the total word count.
- Confirm that the article contains at least one external link to an authoritative source.
If any of these conditions are not met, revise the article until all are satisfied.
Output only the final revised article text.
";

    // Если указана своя тема
    if (!empty($custom_theme)) {
        $final_prompt .= "Ensure the content aligns with the custom theme '{$custom_theme}'. ";
    }

    // Дополнительные общие запросы
    if (!empty($additional_requests)) {
        $final_prompt .= "Also incorporate these requests: '{$additional_requests}'. ";
    }

    // Если тип сайта — магазин (store), добавляем товары WooCommerce
    if ($site_type === 'store') {
        $random_products = get_random_woocommerce_products();
        $product_info_sentences = [];

        foreach ($random_products as $product) {
            $product_info_sentences[] = "Optionally reference the product: {$product['url']}.";
        }

        $product_info_text = implode(' ', $product_info_sentences);
        $final_prompt .= " " . $product_info_text;
    }

    // Индивидуальный запрос для конкретного поста
    if (!empty($single_post_additional_request)) {
        $final_prompt .= "Additionally, be sure to address this specific request: '{$single_post_additional_request}'. ";
    }

    // Общие уточнения
    $final_prompt .= "Use well-structured subheadings and neatly formatted paragraphs suitable for a '{$site_type}' site.";

    // Генерация статьи
    $article_content = request_content_generation($api_key, $text_model, $final_prompt);

    // Проверка и повторная генерация при необходимости
    $attempts = 0;
    $max_attempts = 3;
    while (in_array($article_content, ['No content generated', 'Содержание по умолчанию']) && $attempts < $max_attempts) {
        sleep(5); // задержка между повторными запросами
        $article_content = request_content_generation($api_key, $text_model, $final_prompt);
        $attempts++;
    }

    // Убедимся, что $article_content — это строка
    if (!is_string($article_content)) {
        $article_content = (string) $article_content;
    }

    // Очистка от нежелательных элементов:

    // 1) Удаляем HTML-комментарии
    $article_content = preg_replace('/<!--.*?-->/s', '', $article_content);

    // 2) Удаляем <h1>...</h1>
    $article_content = preg_replace('/<h1>.*?<\/h1>/si', '', $article_content);

    // 3) Удаляем <!DOCTYPE html...>
    $article_content = preg_replace('/<!DOCTYPE html.*?>/si', '', $article_content);

    // 4) Удаляем <pre> и <code> в начале и конце статьи
    $article_content = preg_replace('/^(<pre.*?>.*?<\/pre>|<code>.*?<\/code>)/si', '', $article_content);
    $article_content = preg_replace('/(<pre.*?>.*?<\/pre>|<code>.*?<\/code>)$/si', '', $article_content);

    // 5) Удаление всех возможных вариантов «```html» и пр. в начале
    $article_content = preg_replace('/^(\s*((\{){1,3}|\'{1,3}|"{1,3})html\s*)+/i', '', $article_content);

    // 6) Удаление кавычек/фигурных скобок в конце
    $article_content = preg_replace('/(\s*((\{){1,3}|\'{1,3}|"{1,3})\s*)+$/m', '', $article_content);
    $article_content = preg_replace('/(\s*((\{){1,3}|\'{1,3}|"{1,3})\s*)+(\r?\n|\r)?$/m', '', $article_content);

    // 7) Удаляем «мета-комментарии» модели (I generated..., This content was created..., etc.)
    $article_content = preg_replace('/\b(This (HTML structure|article|content|section|review)|I (generated|created)).*$/mi', '', $article_content);

    return $article_content;
}
