<?php

declare(strict_types=1);

namespace SeoScope\Services;

/**
 * Versioned prompts for Pages report SEO AI Assistant.
 */
final class SeoAiPagesPrompts
{
    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 Pages report. The user message is JSON whose `gsc_payload` field describes the top landing page URLs for the selected period and filters (All or Tracked pages, optional search filter, sort order). When compare is enabled, rows may include a `previous_period` block for the prior equal-length window.

Rules:
- Focus on page-level performance: visibility (impressions), traffic (clicks), CTR, and average position; highlight winners, laggards, and compare-period shifts when data is present.
- Use ONLY data in the JSON. Do not invent queries, 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 overall landing-page portfolio health for this view.
- seo_interpretation: one or two brief paragraphs on patterns in the listed URLs (volume, position, CTR, compare deltas if present).
- priority_recommendation: exactly ONE concrete next step for this page set (content, technical, or internal linking focus); no bullet list.
TXT;
    }

    /**
     * @param array<string, mixed> $payload From {@see SeoAiPagesPayloadService::build()}
     */
    public static function userEnvelope(array $payload): string
    {
        $data = [
            'task' => 'seo_pages_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
        );
    }
}
