<?php
declare(strict_types=1);
namespace PpPhp\Core\Designer\Generation;
use PpPhp\Core\Designer\Auth\DesignerAuthRenderer;use PpPhp\Core\Designer\Dashboard\DesignerDashboardRenderer;use PpPhp\Core\Designer\DataGrid\DesignerDataGridRenderer;use PpPhp\Core\Designer\Navigation\DesignerNavigationRenderer;
final class DesignerPageRenderer{
 public function renderPage(array $page):string{ $body=[]; foreach((array)($page['components']??[]) as $c){$body[]=$this->renderComponent((array)$c,$page);} return '<!doctype html><html><head><meta charset="utf-8"><title>'.$this->e((string)($page['title']??'Page')).'</title><meta name="viewport" content="width=device-width,initial-scale=1"></head><body class="ppg-page ppg-page-'.$this->e((string)($page['pageType']??'page')).'">'.implode("\n",$body).'</body></html>';}
 public function renderComponent(array $c,array $page=[]):string{ $t=(string)($c['type']??''); if($t==='dashboard')return (new DesignerDashboardRenderer())->render($c); if($t==='data_grid')return (new DesignerDataGridRenderer())->render($c); if($t==='auth_form')return (new DesignerAuthRenderer())->render($page?:['components'=>[$c]]); if($t==='navigation_menu')return (new DesignerNavigationRenderer())->render($c); if($t==='form')return $this->form($c); if($t==='heading')return '<h2>'.$this->e((string)($c['props']['text']??$c['label']??'Heading')).'</h2>'; return '<div class="ppg-component" data-component-id="'.$this->e((string)($c['id']??'component')).'">'.$this->e((string)($c['label']??$t?:'Component')).'</div>';}
 private function form(array $c):string{ $p=(array)($c['props']??[]); $h=['<form class="ppg-form" data-form-id="'.$this->e((string)($p['formId']??'form')).'"><h2>'.$this->e((string)($p['title']??'Form')).'</h2>']; foreach((array)($p['sections']??[]) as $s){$h[]='<section class="ppg-form-section"><h3>'.$this->e((string)($s['title']??'Section')).'</h3>'; foreach((array)($s['fields']??[]) as $f){$h[]='<label><span>'.$this->e((string)($f['label']??$f['name']??'Field')).'</span><input type="'.$this->e((string)($f['type']??'text')).'" name="'.$this->e((string)($f['name']??'field')).'"></label>';}$h[]='</section>';} return implode('',$h).'</form>';}
 private function e(string $v):string{return htmlspecialchars($v,ENT_QUOTES,'UTF-8');}
}
