<?php

declare(strict_types=1);

namespace SeoScope\Services;

/**
 * Versioned prompts for Quick Win → Low CTR Pages SEO AI Assistant.
 */
final class SeoAiLowCtrPagesPrompts
{
    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 Quick Win report. The user message is JSON whose `gsc_payload` field describes Low CTR Pages only: landing page URLs with many impressions but CTR below the expected CTR for their average position (scaled by the configured coefficient). Each row includes the page URL/path and aggregate metrics for the selected period.

Rules:
- Focus ONLY on page-level CTR recovery: snippet, title/meta, SERP presentation, and above-the-fold relevance for the listed URLs—not generic site health, link building, or unrelated tactics.
- Use ONLY data in the JSON (top pages list + portfolio summary). 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 low-CTR page potential (clicks left on the table, concentration of high-impression URLs).
- seo_interpretation: one or two brief paragraphs on patterns in the listed pages (gap size, position, URL themes)—prioritize the highest-score rows.
- priority_recommendation: exactly ONE concrete next step to lift CTR on the best target page(s); no bullet list.
TXT;
    }

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