<?php

namespace WPSN\Core;

defined('ABSPATH') || exit;

/**
 * White‑label / branding helpers.
 *
 * Goal: avoid leaking the vendor name in UI when white‑label mode is enabled.
 */
final class Branding
{
    private const VENDOR_BRAND = 'WP SEO Next';

    public static function whiteLabelEnabled(Settings $settings): bool
    {
        return (bool) $settings->get('white_label.enabled', false);
    }

    public static function hideBranding(Settings $settings): bool
    {
        return self::whiteLabelEnabled($settings) && (bool) $settings->get('white_label.hide_branding', false);
    }

    /**
     * Returns the configured brand name.
     *
     * - When white‑label is OFF: returns vendor brand.
     * - When white‑label is ON: returns the custom brand (if set).
     * - When white‑label is ON + "hide branding" is ON and brand is not changed:
     *   returns an empty string (avoid leaking vendor brand).
     */
    public static function brand(Settings $settings): string
    {
        $wlEnabled = self::whiteLabelEnabled($settings);

        if (!$wlEnabled) {
            return self::VENDOR_BRAND;
        }

        $brand = trim((string) $settings->get('white_label.brand_name', ''));

        // If hide-branding is on and the user didn't change the brand, don't output the vendor name.
        if (self::hideBranding($settings)) {
            if ($brand === '' || strcasecmp($brand, self::VENDOR_BRAND) === 0) {
                return '';
            }
        }

        // In normal white-label mode we still want a fallback.
        if ($brand === '') {
            $brand = self::VENDOR_BRAND;
        }

        return $brand;
    }

    /**
     * Returns a "Label (Brand)" string unless branding should be hidden.
     */
    public static function labelWithBrand(Settings $settings, string $label, string $domain = 'wp-seo-next'): string
    {
        $brand = self::brand($settings);
        if ($brand === '') {
            return $label;
        }

        // translators: 1: label, 2: brand name
        return sprintf(__('%1$s (%2$s)', $domain), $label, $brand);
    }

    /**
     * Link text for "Settings". Uses the brand if safe, otherwise generic.
     */
    public static function settingsLinkText(Settings $settings, string $domain = 'wp-seo-next'): string
    {
        $brand = self::brand($settings);
        if ($brand === '') {
            return __('Settings', $domain);
        }
        // translators: %s is brand name
        return sprintf(__('%s Settings', $domain), $brand);
    }
}
