<?php

declare(strict_types=1);

final class MyronPublicHeadMetadataResolver
{
    /**
     * @param array<string, mixed> $page
     * @return array<string, mixed>
     */
    public function resolvePublishedPage(array $page): array
    {
        $requestPath = trim((string) ($page['_requestPath'] ?? ''));
        if ($requestPath !== '') {
            $page['path'] = myronNormalizePath($requestPath);
        }
        $settings = function_exists('myronSiteSettings') ? myronSiteSettings() : [];
        $resolver = new MyronSeoFieldResolver(is_array($settings) ? $settings : []);
        $resolved = $resolver->resolveAll($page);
        $title = (string) ($resolved['seo_title']['value'] ?? myronSiteTitle());
        $description = (string) ($resolved['meta_description']['value'] ?? '');
        $canonical = (string) ($resolved['canonical']['value'] ?? '');
        $forceNoindex = (bool) ($page['_forceNoindex'] ?? false);
        $robots = $forceNoindex ? 'noindex, follow' : (string) ($resolved['robots']['value'] ?? '');

        return [
            'documentTitle' => myronComposeDocumentTitle($title),
            'metaDescription' => $description,
            'canonicalUrl' => !empty($settings['seo_enable_canonical_output']) ? $canonical : '',
            'robots' => $forceNoindex || !empty($settings['seo_enable_robots_output']) ? $robots : '',
            'og' => [
                'type' => 'article',
                'title' => (string) ($resolved['og_title']['value'] ?? ''),
                'description' => (string) ($resolved['og_description']['value'] ?? ''),
                'url' => $canonical,
                'image' => (string) ($resolved['og_image']['value'] ?? ''),
            ],
            'twitter' => [
                'card' => (string) (($settings['seo_twitter_card_type'] ?? '') ?: 'summary_large_image'),
                'title' => (string) ($resolved['twitter_title']['value'] ?? ''),
                'description' => (string) ($resolved['twitter_description']['value'] ?? ''),
                'image' => (string) ($resolved['twitter_image']['value'] ?? ''),
            ],
            'socialEnabled' => !empty($settings['seo_enable_social_output']),
        ];
    }

    /**
     * @param array<string, mixed> $vcard
     * @return array{documentTitle:string, metaDescription:string, canonicalUrl:string, robots:string}
     */
    public function resolveVCard(array $vcard): array
    {
        $title = $this->firstNonEmpty([
            $vcard['display_name'] ?? '',
            'vCard',
        ]);
        $description = trim((string) ($vcard['job_title'] ?? '') . ' ' . (string) ($vcard['organization'] ?? ''));

        return [
            'documentTitle' => myronComposeDocumentTitle($title),
            'metaDescription' => $description,
            'canonicalUrl' => '',
            'robots' => '',
        ];
    }

    /**
     * @return array{documentTitle:string, metaDescription:string, canonicalUrl:string, robots:string}
     */
    public function resolveNotFound(): array
    {
        return [
            'documentTitle' => myronComposeDocumentTitle('Page not found'),
            'metaDescription' => 'The requested MyronCMS page is not published yet.',
            'canonicalUrl' => '',
            'robots' => '',
        ];
    }

    /**
     * @param list<mixed> $candidates
     */
    private function firstNonEmpty(array $candidates): string
    {
        foreach ($candidates as $candidate) {
            $value = trim((string) $candidate);
            if ($value !== '') {
                return $value;
            }
        }

        return myronSiteTitle();
    }
}
