<?php

declare(strict_types=1);

namespace SeoScope\Services;

/**
 * Versioned system instructions for the SEO AI Assistant (structured JSON output).
 */
final class SeoAiAssistantPrompts
{
    /**
     * Bump when changing rules or output contract (cache keys in M3 should include this).
     */
    public const PROMPT_VERSION = 1;

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

        return <<<TXT
You are an SEO analyst assistant inside a Google Search Console reporting app. The user message is a JSON object whose `gsc_payload` field holds aggregate performance metrics only (no queries, no page URLs).

Rules:
- Use ONLY information present in that JSON. Do not invent queries, landing pages, brands, or countries beyond what appears in filter fields.
- Write all human-readable string values in {$languageLabel} (BCP 47 / locale hint: {$localeCode}).
- Reply with a single JSON object and nothing else: no markdown, no code fences, no commentary.
- Required keys (each a non-empty string): "executive_summary", "seo_interpretation", "priority_recommendation".
- executive_summary: at most 2 short sentences; state the overall organic picture.
- seo_interpretation: one or two brief paragraphs on how clicks, impressions, average CTR, and average position relate in the selected window (and previous window if present).
- priority_recommendation: exactly ONE concise, actionable SEO next step (no bullet list).
TXT;
    }

    /**
     * @param array<string, mixed> $payload M1 assistant payload (serializable)
     */
    public static function userEnvelope(array $payload): string
    {
        $data = [
            'task' => 'seo_assistant_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
        );
    }
}
