<?php
// components/Ui/PageHeader.php
namespace Components\Ui;

use Components\Base;

class PageHeader
{
    /**
     * Render a simple page header/title block.
     *
     * Options (all optional):
     * - tag:        Heading tag to use (default: 'h3')
     * - classes:    CSS classes for the heading element (default: 'text-lg font-semibold mb-2')
     * - subtitle:   Secondary text below the title (string)
     * - subtitleClasses: CSS classes for subtitle (default: 'text-secondary mt-1')
     * - wrapperClasses:  CSS classes for outer wrapper <div> (default: '')
     * - attrs:      Extra HTML attributes for the outer wrapper (array key=>value)
     * - right:      Optional HTML string to render on the right side of the title row (e.g., action buttons)
     * - rightWrapperClasses: CSS classes for the right-side wrapper (default: '')
     * - headerRowClasses: CSS classes for the header row wrapper when a right slot is present
     *                     (default: 'flex items-center justify-between gap-4')
     */
    public static function render(string $title, array $opts = []): string
    {
        $tag            = $opts['tag']             ?? 'h1';
        $classes        = $opts['classes']         ?? 'text-2xl opacity-80 font-semibold mb-2';
        $subtitle       = isset($opts['subtitle']) ? (string)$opts['subtitle'] : '';
        $subtitleClass  = $opts['subtitleClasses'] ?? 'text-secondary opacity-80 mt-1';
        $wrapperClasses = $opts['wrapperClasses']  ?? '';
        $attrs          = isset($opts['attrs']) && is_array($opts['attrs']) ? $opts['attrs'] : [];
        $rightHtml      = isset($opts['right']) ? (string)$opts['right'] : '';
        // Default to no vertical padding so header height stays consistent
        $rightWrapper   = $opts['rightWrapperClasses'] ?? '';
        $rowClasses     = $opts['headerRowClasses'] ?? 'flex items-center justify-between gap-4';

        // Compose wrapper attributes
        $wrapperAttr = '';
        $wrapperCls  = trim((string)$wrapperClasses);
        if ($wrapperCls !== '') {
            $attrs['class'] = isset($attrs['class']) && $attrs['class'] !== ''
                ? trim((string)$attrs['class'] . ' ' . $wrapperCls)
                : $wrapperCls;
        }
        if (!empty($attrs)) {
            $wrapperAttr = Base::attrs($attrs);
        }

        $titleEsc    = Base::h($title);
        $classesAttr = Base::h((string)$classes, false);
        $tagName     = preg_replace('~[^a-z0-9]~i', '', (string)$tag) ?: 'h3';

        $subtitleHtml = '';
        if ($subtitle !== '') {
            $subtitleEsc   = Base::h($subtitle);
            $subtitleClass = Base::h((string)$subtitleClass, false);
            $subtitleHtml  = "\n  <p class=\"{$subtitleClass}\">{$subtitleEsc}</p>";
        }

        // If a right-side slot is provided, render a header row with actions aligned right.
        if ($rightHtml !== '') {
            $rowClsEsc   = Base::h((string)$rowClasses, false);
            $rightClsEsc = Base::h((string)$rightWrapper, false);
            return <<<HTML
<div{$wrapperAttr}>
  <div class="{$rowClsEsc}">
    <{$tagName} class="{$classesAttr}">{$titleEsc}</{$tagName}>
    <div class="{$rightClsEsc}">{$rightHtml}</div>
  </div>
  {$subtitleHtml}
</div>
HTML;
        }

        // Default simple header without right slot
        return <<<HTML
<div{$wrapperAttr}>
  <{$tagName} class="{$classesAttr}">{$titleEsc}</{$tagName}>
  {$subtitleHtml}
</div>
HTML;
    }
}
