<?php

declare(strict_types=1);

/**
 * Per-language branding.
 *
 * Site name, tagline and the homepage title/description used to be single global values, so a
 * thirty-language site served the same wording in every locale: an English page could carry an
 * Arabic site name in its <title>, its og:title and its structured data. These four strings sit
 * in front of every indexed page, so that was a real multilingual SEO problem.
 *
 * They are now ordinary translatable strings. The value typed in Settings is the DEFAULT; each
 * language may override it in Languages > Edit Translations > Branding, and anything not
 * overridden falls back to that default. Nothing changes for a site that never touches them.
 */
class Branding
{
    public const KEYS = [
        'branding_site_name'        => ['setting' => 'site_name',                 'default' => 'TubePress'],
        'branding_site_description' => ['setting' => 'site_description',          'default' => ''],
        'branding_home_title'       => ['setting' => 'seo_home_title',            'default' => ''],
        'branding_home_description' => ['setting' => 'seo_home_meta_description', 'default' => ''],
    ];

    /** Register the four strings so they appear in the translation editor with the global value as default. */
    public static function register(): void
    {
        $keys = [];
        foreach (self::KEYS as $key => $cfg) {
            $keys[$key] = Setting::get($cfg['setting'], $cfg['default']);
        }
        Translator::registerKeys('Branding', $keys);
    }

    /** The value for the current locale: language override first, then the global setting. */
    public static function get(string $key): string
    {
        $cfg = self::KEYS[$key] ?? null;
        if ($cfg === null) {
            return '';
        }
        $global = (string) Setting::get($cfg['setting'], $cfg['default']);
        $value = Translator::get($key);

        return ($value === '' || $value === $key) ? $global : $value;
    }

    public static function siteName(): string
    {
        $name = self::get('branding_site_name');
        return $name !== '' ? $name : 'TubePress';
    }

    public static function siteDescription(): string
    {
        return self::get('branding_site_description');
    }

    public static function homeTitle(): string
    {
        return self::get('branding_home_title');
    }

    public static function homeDescription(): string
    {
        return self::get('branding_home_description');
    }
}
