<?php
/**
 * 芋头CMS - 页面装修组件定义
 */

namespace Core\Lib;

class BuilderComponents
{
    public static function getDefinitions()
    {
        return [
            'text' => [
                'label' => '文本',
                'description' => '标题、段落和说明文案。',
                'groups' => [
                    'content' => '内容',
                    'layout' => '布局',
                ],
                'fields' => [
                    'title' => ['type' => 'text', 'label' => '标题', 'default' => '这是一个文本模块', 'group' => 'content'],
                    'content' => ['type' => 'textarea', 'label' => '正文', 'default' => '在这里填写正文内容。', 'group' => 'content'],
                    'align' => [
                        'type' => 'select',
                        'label' => '对齐方式',
                        'default' => 'left',
                        'options' => ['left' => '左对齐', 'center' => '居中', 'right' => '右对齐'],
                        'group' => 'layout',
                    ],
                    'variant' => [
                        'type' => 'select',
                        'label' => '样式方案',
                        'default' => 'default',
                        'options' => [
                            'default' => '标准文本',
                            'highlight' => '强调卡片',
                            'minimal' => '极简段落',
                        ],
                        'group' => 'layout',
                    ],
                ],
                'style_defaults' => [
                    'section_bg_color' => '#ffffff',
                    'section_padding_top' => 28,
                    'section_padding_bottom' => 28,
                    'section_radius' => 18,
                    'card_bg_color' => '#ffffff',
                ],
            ],
            'image' => [
                'label' => '图片',
                'description' => '单张主图、横幅图或视觉展示。',
                'groups' => [
                    'content' => '内容',
                    'action' => '跳转',
                    'layout' => '布局',
                ],
                'fields' => [
                    'image_url' => ['type' => 'text', 'label' => '图片地址', 'default' => '', 'upload' => 'image', 'group' => 'content'],
                    'alt_text' => ['type' => 'text', 'label' => '图片说明', 'default' => '图片模块', 'group' => 'content'],
                    'caption' => ['type' => 'text', 'label' => '说明文字', 'default' => '', 'group' => 'content'],
                    'link' => ['type' => 'text', 'label' => '点击链接', 'default' => '', 'group' => 'action'],
                    'variant' => [
                        'type' => 'select',
                        'label' => '样式方案',
                        'default' => 'default',
                        'options' => [
                            'default' => '标准展示',
                            'cover' => '整宽横幅',
                            'card' => '卡片包裹',
                        ],
                        'group' => 'layout',
                    ],
                ],
                'style_defaults' => [
                    'section_bg_color' => '#ffffff',
                    'section_padding_top' => 24,
                    'section_padding_bottom' => 24,
                    'section_radius' => 20,
                    'card_bg_color' => '#ffffff',
                ],
            ],
            'button' => [
                'label' => '按钮',
                'description' => '行动按钮或咨询入口。',
                'groups' => [
                    'content' => '内容',
                    'layout' => '布局',
                ],
                'fields' => [
                    'text' => ['type' => 'text', 'label' => '按钮文字', 'default' => '立即查看', 'group' => 'content'],
                    'link' => ['type' => 'text', 'label' => '跳转链接', 'default' => '/', 'group' => 'content'],
                    'align' => [
                        'type' => 'select',
                        'label' => '对齐方式',
                        'default' => 'left',
                        'options' => ['left' => '左对齐', 'center' => '居中', 'right' => '右对齐'],
                        'group' => 'layout',
                    ],
                    'variant' => [
                        'type' => 'select',
                        'label' => '样式方案',
                        'default' => 'solid',
                        'options' => [
                            'solid' => '实心按钮',
                            'outline' => '描边按钮',
                            'ghost' => '轻量按钮',
                        ],
                        'group' => 'layout',
                    ],
                ],
                'style_defaults' => [
                    'section_padding_top' => 18,
                    'section_padding_bottom' => 18,
                    'section_radius' => 999,
                    'button_bg_color' => '#111827',
                    'button_text_color' => '#ffffff',
                ],
            ],
            'navbar' => [
                'label' => '导航栏',
                'description' => '站点导航、品牌名和主按钮。',
                'groups' => [
                    'content' => '内容',
                    'action' => '按钮',
                    'layout' => '布局',
                ],
                'fields' => [
                    'brand_name' => ['type' => 'text', 'label' => '品牌名称', 'default' => '芋头CMS', 'group' => 'content'],
                    'nav_items' => ['type' => 'link_list', 'label' => '导航链接', 'default' => "首页|/\n产品中心|/public/index.php?m=product\n新闻资讯|/public/index.php?m=article\n联系我们|/public/index.php?m=guestbook", 'group' => 'content'],
                    'button_text' => ['type' => 'text', 'label' => '右侧按钮文字', 'default' => '立即咨询', 'group' => 'action'],
                    'button_link' => ['type' => 'text', 'label' => '右侧按钮链接', 'default' => '/public/index.php?m=guestbook', 'group' => 'action'],
                    'variant' => [
                        'type' => 'select',
                        'label' => '样式方案',
                        'default' => 'default',
                        'options' => [
                            'default' => '经典导航',
                            'centered' => '居中导航',
                            'split' => '分栏品牌',
                            'pill' => '胶囊菜单',
                            'minimal' => '极简细线',
                        ],
                        'group' => 'layout',
                    ],
                ],
                'style_defaults' => [
                    'section_bg_color' => '#f8fafc',
                    'section_padding_top' => 18,
                    'section_padding_bottom' => 18,
                    'section_radius' => 24,
                    'card_bg_color' => '#ffffff',
                    'button_bg_color' => '#111827',
                    'button_text_color' => '#ffffff',
                ],
            ],
            'hero' => [
                'label' => '首屏横幅',
                'description' => '页面主标题、副标题和主按钮。',
                'groups' => [
                    'content' => '文案',
                    'action' => '按钮',
                    'media' => '背景素材',
                    'layout' => '布局',
                ],
                'fields' => [
                    'title' => ['type' => 'text', 'label' => '主标题', 'default' => '为企业站点提供更轻量的页面装修能力', 'group' => 'content'],
                    'subtitle' => ['type' => 'textarea', 'label' => '副标题', 'default' => '简化后台操作，让运营快速完成页面搭建。', 'group' => 'content'],
                    'button_text' => ['type' => 'text', 'label' => '按钮文字', 'default' => '查看方案', 'group' => 'action'],
                    'button_link' => ['type' => 'text', 'label' => '按钮链接', 'default' => '/public/index.php?m=guestbook', 'group' => 'action'],
                    'background_image' => ['type' => 'text', 'label' => '背景图片', 'default' => '', 'upload' => 'image', 'group' => 'media'],
                    'variant' => [
                        'type' => 'select',
                        'label' => '样式方案',
                        'default' => 'default',
                        'options' => [
                            'default' => '经典横幅',
                            'spotlight' => '聚焦居中',
                            'split' => '左右分栏',
                            'frame' => '边框舞台',
                            'strip' => '横向条带',
                        ],
                        'group' => 'layout',
                    ],
                ],
                'style_defaults' => [
                    'section_bg_color' => '#f8fafc',
                    'section_padding_top' => 36,
                    'section_padding_bottom' => 36,
                    'section_radius' => 28,
                    'button_bg_color' => '#ffffff',
                    'button_text_color' => '#111827',
                ],
            ],
            'image_text' => [
                'label' => '图文介绍',
                'description' => '图文组合介绍企业、服务和卖点。',
                'groups' => [
                    'content' => '文案',
                    'media' => '图片',
                    'layout' => '布局',
                ],
                'fields' => [
                    'title' => ['type' => 'text', 'label' => '标题', 'default' => '为什么选择我们', 'group' => 'content'],
                    'content' => ['type' => 'textarea', 'label' => '内容', 'default' => '用一段清晰文案介绍核心能力和服务优势。', 'group' => 'content'],
                    'image' => ['type' => 'text', 'label' => '图片地址', 'default' => '', 'upload' => 'image', 'group' => 'media'],
                    'variant' => [
                        'type' => 'select',
                        'label' => '样式方案',
                        'default' => 'default',
                        'options' => [
                            'default' => '并排图文',
                            'feature' => '重点展示',
                            'story' => '故事卡片',
                            'card' => '卡片排版',
                            'editorial' => '杂志编排',
                        ],
                        'group' => 'layout',
                    ],
                    'image_position' => [
                        'type' => 'select',
                        'label' => '图片位置',
                        'default' => 'left',
                        'options' => ['left' => '左侧', 'right' => '右侧'],
                        'group' => 'layout',
                    ],
                ],
            ],
            'rich_text' => [
                'label' => '富文本',
                'description' => '插入正文内容或说明文字。',
                'groups' => [
                    'content' => '内容',
                    'layout' => '布局',
                ],
                'fields' => [
                    'content' => ['type' => 'textarea', 'label' => '内容', 'default' => '<p>请输入页面正文内容。</p>', 'group' => 'content'],
                    'variant' => [
                        'type' => 'select',
                        'label' => '样式方案',
                        'default' => 'default',
                        'options' => [
                            'default' => '标准正文',
                            'prose' => '阅读排版',
                            'spotlight' => '聚焦说明',
                            'split' => '双栏排版',
                            'note' => '提示卡片',
                        ],
                        'group' => 'layout',
                    ],
                ],
            ],
            'product_list' => [
                'label' => '产品列表',
                'description' => '读取产品内容并展示。',
                'groups' => [
                    'content' => '展示',
                    'data' => '数据源',
                    'layout' => '布局',
                ],
                'fields' => [
                    'title' => ['type' => 'text', 'label' => '标题', 'default' => '推荐产品', 'group' => 'content'],
                    'channel_id' => ['type' => 'number', 'label' => '栏目 ID', 'default' => 0, 'group' => 'data'],
                    'limit' => ['type' => 'number', 'label' => '显示数量', 'default' => 6, 'group' => 'content'],
                    'variant' => [
                        'type' => 'select',
                        'label' => '样式方案',
                        'default' => 'default',
                        'options' => [
                            'default' => '标准网格',
                            'showcase' => '首卡突出',
                            'compact' => '紧凑列表',
                            'cards' => '卡片瀑布',
                            'minimal' => '极简商品',
                        ],
                        'group' => 'layout',
                    ],
                    'show_price' => [
                        'type' => 'select',
                        'label' => '显示价格',
                        'default' => 1,
                        'options' => [1 => '显示', 0 => '隐藏'],
                        'group' => 'content',
                    ],
                ],
            ],
            'product_category' => [
                'label' => '产品分类',
                'description' => '展示产品栏目分类导航。',
                'groups' => [
                    'content' => '展示',
                    'data' => '数据源',
                    'layout' => '布局',
                ],
                'fields' => [
                    'title' => ['type' => 'text', 'label' => '标题', 'default' => '产品分类', 'group' => 'content'],
                    'channel_id' => ['type' => 'number', 'label' => '根栏目 ID', 'default' => 0, 'group' => 'data'],
                    'show_description' => [
                        'type' => 'select',
                        'label' => '显示简介',
                        'default' => 0,
                        'options' => [1 => '显示', 0 => '隐藏'],
                        'group' => 'content',
                    ],
                    'variant' => [
                        'type' => 'select',
                        'label' => '样式方案',
                        'default' => 'tabs',
                        'options' => [
                            'tabs' => '横向导航',
                            'cards' => '分类卡片',
                            'minimal' => '极简标签',
                        ],
                        'group' => 'layout',
                    ],
                ],
            ],
            'article_list' => [
                'label' => '文章列表',
                'description' => '读取文章内容并展示。',
                'groups' => [
                    'content' => '展示',
                    'data' => '数据源',
                    'layout' => '布局',
                ],
                'fields' => [
                    'title' => ['type' => 'text', 'label' => '标题', 'default' => '最新资讯', 'group' => 'content'],
                    'channel_id' => ['type' => 'number', 'label' => '栏目 ID', 'default' => 0, 'group' => 'data'],
                    'limit' => ['type' => 'number', 'label' => '显示数量', 'default' => 6, 'group' => 'content'],
                    'variant' => [
                        'type' => 'select',
                        'label' => '样式方案',
                        'default' => 'default',
                        'options' => [
                            'default' => '标准列表',
                            'timeline' => '时间轴',
                            'magazine' => '杂志卡片',
                            'compact' => '紧凑清单',
                            'highlight' => '首条突出',
                        ],
                        'group' => 'layout',
                    ],
                ],
            ],
            'gallery' => [
                'label' => '图片画廊',
                'description' => '上传多张图片进行展示。',
                'groups' => [
                    'content' => '展示',
                    'media' => '图片',
                    'layout' => '布局',
                ],
                'fields' => [
                    'title' => ['type' => 'text', 'label' => '标题', 'default' => '案例展示', 'group' => 'content'],
                    'images' => ['type' => 'textarea', 'label' => '图片地址列表', 'default' => '', 'upload' => 'images', 'group' => 'media'],
                    'variant' => [
                        'type' => 'select',
                        'label' => '样式方案',
                        'default' => 'default',
                        'options' => [
                            'default' => '标准宫格',
                            'mosaic' => '拼贴画廊',
                            'strip' => '横向长卷',
                            'framed' => '留白相框',
                            'spotlight' => '首图突出',
                        ],
                        'group' => 'layout',
                    ],
                ],
            ],
            'carousel' => [
                'label' => '轮播多图',
                'description' => '用于焦点图和横幅轮播。',
                'groups' => [
                    'content' => '展示',
                    'media' => '图片',
                    'action' => '跳转',
                    'layout' => '布局',
                ],
                'fields' => [
                    'title' => ['type' => 'text', 'label' => '标题', 'default' => '焦点展示', 'group' => 'content'],
                    'images' => ['type' => 'textarea', 'label' => '图片地址列表', 'default' => '', 'upload' => 'images', 'group' => 'media'],
                    'links' => ['type' => 'textarea', 'label' => '跳转链接列表', 'default' => '', 'group' => 'action'],
                    'autoplay_seconds' => ['type' => 'number', 'label' => '轮播间隔', 'default' => 4, 'group' => 'content'],
                    'height' => ['type' => 'number', 'label' => '图片高度', 'default' => 420, 'group' => 'layout'],
                    'variant' => [
                        'type' => 'select',
                        'label' => '样式方案',
                        'default' => 'banner',
                        'options' => [
                            'banner' => '横幅轮播',
                            'cards' => '卡片轮播',
                            'fade' => '渐变焦点',
                        ],
                        'group' => 'layout',
                    ],
                ],
            ],
            'contact_card' => [
                'label' => '联系方式',
                'description' => '展示公司联系信息与二维码。',
                'groups' => [
                    'content' => '内容',
                    'media' => '补充素材',
                    'layout' => '布局',
                ],
                'fields' => [
                    'title' => ['type' => 'text', 'label' => '标题', 'default' => '联系我们', 'group' => 'content'],
                    'company' => ['type' => 'text', 'label' => '公司名称', 'default' => '芋头CMS', 'group' => 'content'],
                    'phone' => ['type' => 'text', 'label' => '联系电话', 'default' => '400-000-0000', 'group' => 'content'],
                    'email' => ['type' => 'text', 'label' => '联系邮箱', 'default' => 'service@example.com', 'group' => 'content'],
                    'address' => ['type' => 'text', 'label' => '联系地址', 'default' => '请填写公司地址', 'group' => 'content'],
                    'qrcode' => ['type' => 'text', 'label' => '二维码图片', 'default' => '', 'upload' => 'image', 'group' => 'media'],
                    'variant' => [
                        'type' => 'select',
                        'label' => '样式方案',
                        'default' => 'default',
                        'options' => [
                            'default' => '标准名片',
                            'split' => '信息分栏',
                            'compact' => '紧凑列表',
                            'panel' => '信息面板',
                            'dark' => '深色商务',
                        ],
                        'group' => 'layout',
                    ],
                ],
            ],
            'guestbook_form' => [
                'label' => '留言引导',
                'description' => '引导访客前往留言页或咨询页。',
                'groups' => [
                    'content' => '文案',
                    'action' => '按钮',
                    'layout' => '布局',
                ],
                'fields' => [
                    'title' => ['type' => 'text', 'label' => '标题', 'default' => '留下需求，我们尽快联系您', 'group' => 'content'],
                    'subtitle' => ['type' => 'textarea', 'label' => '说明', 'default' => '适合放在联系页、首页底部或专题页。', 'group' => 'content'],
                    'button_text' => ['type' => 'text', 'label' => '按钮文字', 'default' => '前往留言', 'group' => 'action'],
                    'button_link' => ['type' => 'text', 'label' => '按钮链接', 'default' => '/public/index.php?m=guestbook', 'group' => 'action'],
                    'variant' => [
                        'type' => 'select',
                        'label' => '样式方案',
                        'default' => 'default',
                        'options' => [
                            'default' => '标准引导',
                            'centered' => '居中号召',
                            'split' => '左右分栏',
                            'banner' => '横幅号召',
                            'minimal' => '轻量按钮',
                        ],
                        'group' => 'layout',
                    ],
                ],
            ],
            'footer' => [
                'label' => '页脚',
                'description' => '显示版权说明和底部链接。',
                'groups' => [
                    'content' => '内容',
                    'links' => '链接',
                    'layout' => '布局',
                ],
                'fields' => [
                    'brand_name' => ['type' => 'text', 'label' => '品牌名称', 'default' => '芋头CMS', 'group' => 'content'],
                    'description' => ['type' => 'textarea', 'label' => '简介', 'default' => '用更简单的方式管理企业网站页面内容。', 'group' => 'content'],
                    'quick_links' => ['type' => 'link_list', 'label' => '底部链接', 'default' => "关于我们|/public/index.php?m=page&id=1\n产品中心|/public/index.php?m=product\n新闻资讯|/public/index.php?m=article", 'group' => 'links'],
                    'copyright_text' => ['type' => 'text', 'label' => '版权说明', 'default' => '© 2026 芋头CMS. All rights reserved.', 'group' => 'content'],
                    'icp_text' => ['type' => 'text', 'label' => '备案信息', 'default' => '', 'group' => 'content'],
                    'variant' => [
                        'type' => 'select',
                        'label' => '样式方案',
                        'default' => 'default',
                        'options' => [
                            'default' => '标准页脚',
                            'columns' => '多栏信息',
                            'compact' => '紧凑页脚',
                            'centered' => '居中品牌',
                            'darkband' => '深色横带',
                        ],
                        'group' => 'layout',
                    ],
                ],
                'style_defaults' => [
                    'section_bg_color' => '#0f172a',
                    'section_text_color' => '#e2e8f0',
                    'section_padding_top' => 30,
                    'section_padding_bottom' => 30,
                    'section_radius' => 24,
                    'card_bg_color' => '#111827',
                    'button_bg_color' => '#1e293b',
                    'button_text_color' => '#e2e8f0',
                ],
            ],
            'page_list' => [
                'label' => '单页列表',
                'description' => '读取单页内容摘要。',
                'groups' => [
                    'content' => '展示',
                    'data' => '数据源',
                    'layout' => '布局',
                ],
                'fields' => [
                    'title' => ['type' => 'text', 'label' => '标题', 'default' => '精选单页', 'group' => 'content'],
                    'channel_id' => ['type' => 'number', 'label' => '栏目 ID', 'default' => 0, 'group' => 'data'],
                    'limit' => ['type' => 'number', 'label' => '显示数量', 'default' => 6, 'group' => 'content'],
                    'variant' => [
                        'type' => 'select',
                        'label' => '样式方案',
                        'default' => 'default',
                        'options' => [
                            'default' => '标准卡片',
                            'feature' => '首卡突出',
                            'compact' => '紧凑列表',
                            'editorial' => '杂志排版',
                            'minimal' => '极简目录',
                        ],
                        'group' => 'layout',
                    ],
                ],
            ],
            'image_list' => [
                'label' => '图集列表',
                'description' => '读取图集内容并展示。',
                'groups' => [
                    'content' => '展示',
                    'data' => '数据源',
                    'layout' => '布局',
                ],
                'fields' => [
                    'title' => ['type' => 'text', 'label' => '标题', 'default' => '精选图集', 'group' => 'content'],
                    'channel_id' => ['type' => 'number', 'label' => '栏目 ID', 'default' => 0, 'group' => 'data'],
                    'limit' => ['type' => 'number', 'label' => '显示数量', 'default' => 6, 'group' => 'content'],
                    'variant' => [
                        'type' => 'select',
                        'label' => '样式方案',
                        'default' => 'default',
                        'options' => [
                            'default' => '标准图卡',
                            'mosaic' => '拼贴缩略',
                            'spotlight' => '首图突出',
                            'compact' => '紧凑缩略',
                            'minimal' => '极简图片墙',
                        ],
                        'group' => 'layout',
                    ],
                ],
            ],
            'download_list' => [
                'label' => '下载列表',
                'description' => '读取下载资源并展示。',
                'groups' => [
                    'content' => '展示',
                    'data' => '数据源',
                    'layout' => '布局',
                ],
                'fields' => [
                    'title' => ['type' => 'text', 'label' => '标题', 'default' => '资料下载', 'group' => 'content'],
                    'channel_id' => ['type' => 'number', 'label' => '栏目 ID', 'default' => 0, 'group' => 'data'],
                    'limit' => ['type' => 'number', 'label' => '显示数量', 'default' => 6, 'group' => 'content'],
                    'variant' => [
                        'type' => 'select',
                        'label' => '样式方案',
                        'default' => 'default',
                        'options' => [
                            'default' => '标准下载卡',
                            'feature' => '重点资料',
                            'compact' => '紧凑列表',
                            'table' => '表格式目录',
                            'minimal' => '极简文档流',
                        ],
                        'group' => 'layout',
                    ],
                ],
            ],
            'video_list' => [
                'label' => '视频列表',
                'description' => '读取视频内容并展示。',
                'groups' => [
                    'content' => '展示',
                    'data' => '数据源',
                    'layout' => '布局',
                ],
                'fields' => [
                    'title' => ['type' => 'text', 'label' => '标题', 'default' => '视频中心', 'group' => 'content'],
                    'channel_id' => ['type' => 'number', 'label' => '栏目 ID', 'default' => 0, 'group' => 'data'],
                    'limit' => ['type' => 'number', 'label' => '显示数量', 'default' => 6, 'group' => 'content'],
                    'variant' => [
                        'type' => 'select',
                        'label' => '样式方案',
                        'default' => 'default',
                        'options' => [
                            'default' => '标准视频卡',
                            'cinema' => '影院横幅',
                            'compact' => '紧凑列表',
                            'spotlight' => '首卡突出',
                            'minimal' => '极简播放墙',
                        ],
                        'group' => 'layout',
                    ],
                ],
            ],
            'recruit_list' => [
                'label' => '招聘列表',
                'description' => '读取职位内容并展示。',
                'groups' => [
                    'content' => '展示',
                    'data' => '数据源',
                    'layout' => '布局',
                ],
                'fields' => [
                    'title' => ['type' => 'text', 'label' => '标题', 'default' => '正在招聘', 'group' => 'content'],
                    'channel_id' => ['type' => 'number', 'label' => '栏目 ID', 'default' => 0, 'group' => 'data'],
                    'limit' => ['type' => 'number', 'label' => '显示数量', 'default' => 6, 'group' => 'content'],
                    'variant' => [
                        'type' => 'select',
                        'label' => '样式方案',
                        'default' => 'default',
                        'options' => [
                            'default' => '标准职位卡',
                            'board' => '招聘看板',
                            'compact' => '紧凑列表',
                            'spotlight' => '重点岗位',
                            'minimal' => '极简职位流',
                        ],
                        'group' => 'layout',
                    ],
                ],
            ],
        ];
    }

    public static function getStyleFields()
    {
        return [
            'section_bg_color' => ['type' => 'color', 'label' => '区块背景色', 'default' => ''],
            'section_text_color' => ['type' => 'color', 'label' => '文字颜色', 'default' => ''],
            'section_padding_top' => ['type' => 'number', 'label' => '上内边距', 'default' => 56],
            'section_padding_bottom' => ['type' => 'number', 'label' => '下内边距', 'default' => 56],
            'section_radius' => ['type' => 'number', 'label' => '圆角', 'default' => 18],
            'card_bg_color' => ['type' => 'color', 'label' => '卡片背景色', 'default' => ''],
            'button_bg_color' => ['type' => 'color', 'label' => '按钮背景色', 'default' => ''],
            'button_text_color' => ['type' => 'color', 'label' => '按钮文字色', 'default' => ''],
        ];
    }

    public static function getPresetLayouts()
    {
        return [
            'blank' => [
                'label' => '空白页面',
                'description' => '从零开始搭建。',
                'layout' => self::buildPresetLayout('blank'),
            ],
            'corporate_home' => [
                'label' => '企业首页',
                'description' => '导航、首屏、图文、产品、资讯、联系和页脚。',
                'layout' => self::buildPresetLayout('corporate_home'),
            ],
            'about_page' => [
                'label' => '公司介绍页',
                'description' => '适合单页介绍和图文内容展示。',
                'layout' => self::buildPresetLayout('about_page'),
            ],
            'contact_page' => [
                'label' => '联系咨询页',
                'description' => '突出咨询转化。',
                'layout' => self::buildPresetLayout('contact_page'),
            ],
        ];
    }

    public static function getDefinition($type)
    {
        $definitions = self::getDefinitions();
        return $definitions[$type] ?? null;
    }

    public static function getStyleDefaults($type = '')
    {
        $styleFields = self::getStyleFields();
        $defaults = [];
        foreach ($styleFields as $styleKey => $styleField) {
            $defaults[$styleKey] = $styleField['default'] ?? '';
        }

        $definition = self::getDefinition($type);
        if ($definition && !empty($definition['style_defaults']) && is_array($definition['style_defaults'])) {
            $defaults = array_merge($defaults, $definition['style_defaults']);
        }

        return $defaults;
    }

    public static function buildDefaultSection($type, $collapsed = true)
    {
        $definition = self::getDefinition($type);
        if (!$definition) {
            return null;
        }

        $props = [];
        foreach ($definition['fields'] as $fieldKey => $field) {
            $props[$fieldKey] = $field['default'] ?? '';
        }

        return [
            'id' => self::generateSectionId($type),
            'type' => $type,
            'props' => $props,
            'styles' => self::getStyleDefaults($type),
            'layout' => self::getDefaultSectionLayout($type),
            'collapsed' => !empty($collapsed),
            'hidden' => false,
            'locked' => false,
        ];
    }

    public static function normalizeSection(array $section)
    {
        $type = trim((string) ($section['type'] ?? ''));
        $definition = self::getDefinition($type);
        if (!$definition) {
            return null;
        }

        $defaultSection = self::buildDefaultSection($type, !empty($section['collapsed']));
        $propsSource = isset($section['props']) && is_array($section['props']) ? $section['props'] : [];
        $stylesSource = isset($section['styles']) && is_array($section['styles']) ? $section['styles'] : [];

        foreach ($definition['fields'] as $fieldKey => $field) {
            if (array_key_exists($fieldKey, $propsSource) && !is_array($propsSource[$fieldKey])) {
                $defaultSection['props'][$fieldKey] = $propsSource[$fieldKey];
            }
        }

        foreach (self::getStyleDefaults($type) as $styleKey => $defaultValue) {
            if (array_key_exists($styleKey, $stylesSource) && !is_array($stylesSource[$styleKey])) {
                $defaultSection['styles'][$styleKey] = $stylesSource[$styleKey];
            }
        }

        $defaultSection['layout'] = self::normalizeSectionLayout($section['layout'] ?? null, $type);

        $defaultSection['id'] = trim((string) ($section['id'] ?? '')) !== ''
            ? trim((string) $section['id'])
            : self::generateSectionId($type);
        $defaultSection['collapsed'] = !empty($section['collapsed']);
        $defaultSection['hidden'] = !empty($section['hidden']);
        $defaultSection['locked'] = !empty($section['locked']);

        return $defaultSection;
    }

    public static function generateSectionId($type = '')
    {
        $type = preg_replace('/[^a-z0-9_]+/i', '-', trim((string) $type));
        if ($type === '') {
            $type = 'section';
        }

        return $type . '-' . substr(md5(uniqid((string) mt_rand(), true)), 0, 10);
    }

    public static function getDefaultSectionLayout($type = '', $index = 0)
    {
        $index = max(0, intval($index));
        $column = $index % 2;
        $row = intval(floor($index / 2));
        $width = 420;
        $height = 220;

        if (in_array($type, ['navbar', 'footer'], true)) {
            $width = 560;
            $height = 132;
        } elseif (in_array($type, ['hero', 'banner'], true)) {
            $width = 560;
            $height = 280;
        } elseif (in_array($type, ['gallery', 'rich_text'], true)) {
            $width = 480;
            $height = 260;
        }

        return [
            'position_mode' => 'flow',
            'x' => 32 + ($column * 452),
            'y' => 32 + ($row * 272),
            'w' => $width,
            'h' => $height,
            'z' => $index + 1,
        ];
    }

    public static function normalizeSectionLayout($value, $type = '', $index = 0)
    {
        $layout = self::getDefaultSectionLayout($type, $index);
        if (!is_array($value)) {
            return $layout;
        }

        $mode = trim((string) ($value['position_mode'] ?? ''));
        $layout['position_mode'] = $mode === 'free' ? 'free' : 'flow';
        $layout['x'] = max(0, intval($value['x'] ?? $layout['x']));
        $layout['y'] = max(0, intval($value['y'] ?? $layout['y']));
        $layout['w'] = max(220, intval($value['w'] ?? $layout['w']));
        $layout['h'] = max(120, intval($value['h'] ?? $layout['h']));
        $layout['z'] = max(1, intval($value['z'] ?? $layout['z']));

        return $layout;
    }

    public static function buildPresetLayout($presetKey, $title = '')
    {
        $title = trim((string) $title);
        $layout = [
            'page' => [
                'title' => $title,
            ],
            'sections' => [],
        ];

        switch ($presetKey) {
            case 'corporate_home':
                $layout['page']['title'] = $title !== '' ? $title : '企业首页';
                $layout['sections'] = [
                    self::buildSectionWithOverrides('navbar', [], ['section_bg_color' => '#ffffff']),
                    self::buildSectionWithOverrides('hero'),
                    self::buildSectionWithOverrides('image_text', [
                        'title' => '我们提供什么',
                        'content' => '用一个区块快速概述企业能力、交付方式和服务优势。',
                    ]),
                    self::buildSectionWithOverrides('product_list'),
                    self::buildSectionWithOverrides('article_list'),
                    self::buildSectionWithOverrides('contact_card'),
                    self::buildSectionWithOverrides('footer'),
                ];
                break;

            case 'about_page':
                $layout['page']['title'] = $title !== '' ? $title : '公司介绍';
                $layout['sections'] = [
                    self::buildSectionWithOverrides('navbar'),
                    self::buildSectionWithOverrides('hero', [
                        'title' => '关于我们',
                        'subtitle' => '用清晰的首屏介绍企业定位、核心能力和品牌理念。',
                        'button_text' => '联系我们',
                    ]),
                    self::buildSectionWithOverrides('rich_text', [
                        'content' => '<p>在这里填写公司介绍正文内容。</p>',
                    ]),
                    self::buildSectionWithOverrides('image_text', [
                        'title' => '服务优势',
                        'content' => '通过图文组合呈现服务流程、交付优势或典型案例。',
                        'image_position' => 'right',
                    ]),
                    self::buildSectionWithOverrides('contact_card'),
                    self::buildSectionWithOverrides('footer'),
                ];
                break;

            case 'contact_page':
                $layout['page']['title'] = $title !== '' ? $title : '联系咨询';
                $layout['sections'] = [
                    self::buildSectionWithOverrides('navbar'),
                    self::buildSectionWithOverrides('hero', [
                        'title' => '欢迎提交咨询需求',
                        'subtitle' => '把表单入口、电话和地址集中到一个页面。',
                        'button_text' => '立即留言',
                    ]),
                    self::buildSectionWithOverrides('contact_card'),
                    self::buildSectionWithOverrides('guestbook_form'),
                    self::buildSectionWithOverrides('footer'),
                ];
                break;

            case 'blank':
            default:
                $layout['page']['title'] = $title !== '' ? $title : '空白页面';
                $layout['sections'] = [];
                break;
        }

        return $layout;
    }

    private static function buildSectionWithOverrides($type, array $props = [], array $styles = [], $collapsed = true)
    {
        $section = self::buildDefaultSection($type, $collapsed);
        if (!$section) {
            return null;
        }

        foreach ($props as $key => $value) {
            $section['props'][$key] = $value;
        }
        foreach ($styles as $key => $value) {
            $section['styles'][$key] = $value;
        }

        return $section;
    }
}
