<?php

declare(strict_types=1);

namespace PpPhp\Core;

/**
 * The single source of truth for how the PLATFORM brand is shown.
 *
 * The Owner configures the brand name + optional logo once (Settings → Branding,
 * stored in PlatformSettings::BRAND); every platform surface renders through this
 * helper, so a change in one place carries everywhere. When nothing is customised
 * it renders the built-in crystal "PPPHP" logo (core/views/brand-lockup.php /
 * brand-logo.php), so the default look is unchanged.
 *
 * Not used by generated customer apps — those carry their own brand.
 */
class Brand
{
    /** Configured product name (defaults to "PPPHP"). */
    public static function name(): string
    {
        return PlatformSettings::brandName();
    }

    /** Optional short tagline (used in footers). */
    public static function tagline(): string
    {
        return (string) (PlatformSettings::brand()['tagline'] ?? '');
    }

    /** The custom logo data-URI, or '' when the built-in logo should be used. */
    public static function logo(): string
    {
        return (string) (PlatformSettings::brand()['logo'] ?? '');
    }

    public static function hasCustomLogo(): bool
    {
        return self::logo() !== '';
    }

    /** True when nothing is customised — render the built-in crystal logo verbatim. */
    public static function isDefault(): bool
    {
        return !self::hasCustomLogo() && self::name() === PlatformSettings::DEFAULT_BRAND_NAME;
    }

    /**
     * The square brand MARK (for navbars, avatars, favicons-in-HTML).
     * Custom logo → the uploaded image; otherwise the built-in crystal monogram.
     */
    public static function mark(int $size = 34, string $class = ''): string
    {
        $logo = self::logo();
        if ($logo !== '') {
            return '<img src="' . htmlspecialchars($logo, ENT_QUOTES) . '" alt="' . htmlspecialchars(self::name(), ENT_QUOTES) . '" '
                . 'class="pp-brand-img ' . htmlspecialchars($class) . '" '
                . 'style="height:' . $size . 'px;width:' . $size . 'px;object-fit:contain;display:block">';
        }
        return self::renderPartial('brand-logo.php', [
            'brandLogoSize'  => $size,
            'brandLogoClass' => $class !== '' ? $class : 'logo-svg',
        ]);
    }

    /**
     * The full brand LOCKUP (mark + wordmark) for headers, auth pages, footers.
     *  - custom logo  → the uploaded image at the given height;
     *  - custom name  → the crystal monogram + the name in text;
     *  - all default  → the built-in animated crystal "PP·</>·PHP" lockup.
     */
    public static function lockup(int $height = 40, string $class = ''): string
    {
        $logo = self::logo();
        if ($logo !== '') {
            return '<img src="' . htmlspecialchars($logo, ENT_QUOTES) . '" alt="' . htmlspecialchars(self::name(), ENT_QUOTES) . '" '
                . 'class="pp-brand-img ' . htmlspecialchars($class) . '" '
                . 'style="height:' . $height . 'px;width:auto;max-width:220px;object-fit:contain;display:block">';
        }
        if (self::name() !== PlatformSettings::DEFAULT_BRAND_NAME) {
            $mark = self::mark($height, '');
            $fs   = max(15, (int) round($height * 0.5));
            return '<span class="pp-brand-lockup ' . htmlspecialchars($class) . '" style="display:inline-flex;align-items:center;gap:.55rem">'
                . $mark
                . '<span class="pp-brand-name" style="font-weight:800;letter-spacing:-.02em;font-size:' . $fs . 'px;line-height:1">'
                . htmlspecialchars(self::name()) . '</span></span>';
        }
        return self::renderPartial('brand-lockup.php', [
            'brandLockupHeight' => $height,
            'brandLockupClass'  => $class,
        ]);
    }

    /** Render a core/views brand partial to a string with the given locals. */
    private static function renderPartial(string $file, array $vars): string
    {
        foreach ($vars as $__k => $__v) {
            ${$__k} = $__v;
        }
        ob_start();
        include constant('BASE_PATH') . '/core/views/' . $file;
        return (string) ob_get_clean();
    }
}
