<?php

declare(strict_types=1);

final class MyronSeoPanelService
{
    public function __construct(
        private readonly MyronSeoFieldResolver $resolver,
        private readonly MyronJsonLdService $jsonLd,
    ) {
    }

    /**
     * @param array<string, mixed> $page
     * @return array<string, mixed>
     */
    public function panelData(array $page): array
    {
        $resolved = $this->resolver->resolveAll($page);
        $canonical = (string) ($resolved['canonical']['value'] ?? '');
        $host = parse_url($canonical, PHP_URL_HOST);
        return [
            'resolved' => $resolved,
            'jsonld_preview' => $this->jsonLd->renderPage($page, myronSiteSettings()),
            'preview' => [
                'host' => is_string($host) ? $host : '',
                'url' => $canonical,
                'path' => (string) ($page['path'] ?? '/'),
                'serp_title' => (string) ($resolved['seo_title']['value'] ?? ''),
                'serp_description' => (string) ($resolved['meta_description']['value'] ?? ''),
                'og_title' => (string) ($resolved['og_title']['value'] ?? ''),
                'og_description' => (string) ($resolved['og_description']['value'] ?? ''),
                'og_image' => (string) ($resolved['og_image']['value'] ?? ''),
                'twitter_title' => (string) ($resolved['twitter_title']['value'] ?? ''),
                'twitter_description' => (string) ($resolved['twitter_description']['value'] ?? ''),
                'twitter_image' => (string) ($resolved['twitter_image']['value'] ?? ''),
            ],
        ];
    }
}
