<?php
declare(strict_types=1);
namespace PpPhp\Core\Designer\Generation;
use PpPhp\Core\Designer\Generation\Contracts\GeneratedPage;
final class DesignerPageGenerator{ public function __construct(private readonly DesignerPageRenderer $renderer=new DesignerPageRenderer()){}
 public function generate(array $page):GeneratedPage{ $id=(string)($page['pageId']??$page['id']??'page'); $type=(string)($page['pageType']??$this->infer($page)); $title=(string)($page['title']??$id); $path=$this->pathFor($id,$type,(string)($page['table']??'')); return GeneratedPage::create($id,$type,$path,$title,$this->renderer->renderPage($page),(array)($page['assets']??[]),['id'=>$id.'_route','path'=>'/'.str_replace('_','-',$id),'pageId'=>$id,'title'=>$title],['source'=>'designer-json','generatedAt'=>date('c')]);}
 private function infer(array $page):string{foreach((array)($page['components']??[]) as $c){$t=$c['type']??''; if($t==='dashboard')return 'dashboard'; if($t==='data_grid')return 'list'; if($t==='auth_form')return (string)($c['props']['pageType']??'login'); if($t==='form')return 'form';}return 'page';}
 private function pathFor(string $id,string $type,string $table):string{ if(in_array($type,['login','register','forgot_password','reset_password','change_password','profile','user_info','access_denied'],true))return 'generated/auth/'.str_replace('_','-',$type).'.php'; if($type==='dashboard')return 'generated/dashboard/index.php'; if($table!=='')return 'generated/'.$table.'/'.match($type){'list'=>'list.php','add'=>'add.php','edit'=>'edit.php','view'=>'view.php',default=>$type.'.php'}; return 'generated/pages/'.str_replace('_','-',$id).'.php';}
}
