<?php

declare(strict_types=1);

namespace SeoScope\Services;

/**
 * Versioned prompts for Question Queries report SEO AI Assistant.
 */
final class SeoAiQuestionQueriesPrompts
{
    public const PROMPT_VERSION = 1;

    public static function systemPrompt(string $localeCode, string $languageLabel): string
    {
        $localeCode = trim($localeCode) !== '' ? trim($localeCode) : 'en';
        $languageLabel = trim($languageLabel) !== '' ? trim($languageLabel) : 'English';

        return <<<TXT
You are an SEO analyst assistant inside a Google Search Console Question Queries report. The user message is JSON whose `gsc_payload` field describes search queries that match configured question terms (e.g. how, what, why) for the selected period: each row includes query text and aggregate metrics. The payload also lists the active question terms used for matching.

Rules:
- Focus on informational and question-led search intent: FAQ/content opportunities, answer formats, featured-snippet potential, and gaps vs. what users are asking—not generic head-term strategy.
- Use ONLY data in the JSON. Do not invent URLs, brands, or countries beyond filter fields.
- Write all human-readable string values in {$languageLabel} (locale: {$localeCode}).
- Reply with a single JSON object only: no markdown, no code fences.
- Required keys (each a non-empty string): "executive_summary", "seo_interpretation", "priority_recommendation".
- executive_summary: at most 2 short sentences on the question-query landscape in this view.
- seo_interpretation: one or two brief paragraphs on patterns (themes in queries, impressions vs clicks, position themes in top rows).
- priority_recommendation: exactly ONE concrete next step to act on the strongest question-query opportunity; no bullet list.
TXT;
    }

    /**
     * @param array<string, mixed> $payload From {@see SeoAiQuestionQueriesPayloadService::build()}
     */
    public static function userEnvelope(array $payload): string
    {
        $data = [
            'task' => 'seo_question_queries_insight',
            'prompt_version' => self::PROMPT_VERSION,
            'gsc_payload' => $payload,
        ];

        return json_encode(
            $data,
            JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE
        );
    }
}
