<?php

function cmsValueIsMissing($value)
{
    if ($value === null) {
        return true;
    }

    if (is_string($value)) {
        return trim($value) === '';
    }

    return false;
}

function cmsConfigValueShouldFill($value)
{
    return cmsValueIsMissing($value);
}

function cmsResolveConfigDefaultValue(array $configDefinition, array $configRows)
{
    if (isset($configDefinition['from_config'])) {
        $fromKey = $configDefinition['from_config'];
        if (isset($configRows[$fromKey]['config_value']) && !cmsValueIsMissing($configRows[$fromKey]['config_value'])) {
            return $configRows[$fromKey]['config_value'];
        }
    }

    if (array_key_exists('value', $configDefinition)) {
        return $configDefinition['value'];
    }

    return isset($configDefinition['fallback']) ? $configDefinition['fallback'] : '';
}

function cmsDbUpgradeDefinition($prefix = 'yutou_')
{
    $prefix = (string) $prefix;

    return array(
        'version' => '20260612_cms_upgrade_01',
        'tables' => array(
            $prefix . 'config' => "CREATE TABLE IF NOT EXISTS `{$prefix}config` (
                `id` int(11) NOT NULL AUTO_INCREMENT,
                `config_key` varchar(50) NOT NULL,
                `config_value` text,
                `config_type` varchar(20) DEFAULT 'text',
                `config_group` varchar(20) DEFAULT 'basic',
                `update_time` int(11) DEFAULT NULL,
                PRIMARY KEY (`id`),
                UNIQUE KEY `config_key` (`config_key`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci",
            $prefix . 'license' => "CREATE TABLE IF NOT EXISTS `{$prefix}license` (
                `id` int(11) NOT NULL AUTO_INCREMENT,
                `license_code` varchar(64) DEFAULT NULL,
                `license_domain` varchar(255) DEFAULT NULL,
                `license_level` tinyint(4) DEFAULT 0,
                `license_expire` date DEFAULT NULL,
                `license_status` tinyint(4) DEFAULT 1,
                `site_id` varchar(64) DEFAULT NULL,
                `bind_count` int(11) DEFAULT 0,
                `max_domains` int(11) DEFAULT 1,
                `create_time` int(11) DEFAULT NULL,
                `update_time` int(11) DEFAULT NULL,
                PRIMARY KEY (`id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci",
            $prefix . 'page' => "CREATE TABLE IF NOT EXISTS `{$prefix}page` (
                `id` int(11) NOT NULL AUTO_INCREMENT,
                `channel_id` int(11) DEFAULT NULL,
                `page_title` varchar(100) NOT NULL,
                `page_content` text,
                `status` tinyint(4) NOT NULL DEFAULT 1,
                `seo_title` varchar(200) DEFAULT NULL,
                `seo_keywords` varchar(200) DEFAULT NULL,
                `seo_description` text,
                `template` varchar(50) DEFAULT 'page.html',
                `is_system` tinyint(4) DEFAULT 0,
                `sort_order` int(11) DEFAULT 100,
                `create_time` int(11) DEFAULT NULL,
                `update_time` int(11) DEFAULT NULL,
                `view` int(11) NOT NULL DEFAULT 0,
                `litpic` varchar(255) DEFAULT '',
                PRIMARY KEY (`id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci",
            $prefix . 'template_showcase' => "CREATE TABLE IF NOT EXISTS `{$prefix}template_showcase` (
                `id` int(11) NOT NULL AUTO_INCREMENT,
                `title` varchar(200) NOT NULL,
                `subtitle` varchar(100) DEFAULT NULL,
                `thumb` varchar(255) DEFAULT NULL,
                `images` text,
                `template_dir` varchar(50) DEFAULT NULL,
                `template_type` tinyint(4) DEFAULT 1,
                `author` varchar(50) DEFAULT NULL,
                `version` varchar(20) DEFAULT NULL,
                `demo_url` varchar(255) DEFAULT NULL,
                `download_url` varchar(255) DEFAULT NULL,
                `description` text,
                `seo_title` varchar(200) DEFAULT NULL,
                `seo_keywords` varchar(200) DEFAULT NULL,
                `seo_description` text,
                `click` int(11) DEFAULT 0,
                `collect_count` int(11) DEFAULT 0,
                `use_count` int(11) DEFAULT 0,
                `sort_order` int(11) DEFAULT 100,
                `is_recommend` tinyint(4) DEFAULT 0,
                `status` tinyint(4) DEFAULT 1,
                `create_time` int(11) DEFAULT NULL,
                `update_time` int(11) DEFAULT NULL,
                PRIMARY KEY (`id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci",
            $prefix . 'builder_page' => "CREATE TABLE IF NOT EXISTS `{$prefix}builder_page` (
                `id` int(11) NOT NULL AUTO_INCREMENT,
                `page_key` varchar(60) NOT NULL,
                `page_type` varchar(20) NOT NULL DEFAULT 'home',
                `ref_id` int(11) NOT NULL DEFAULT 0,
                `title` varchar(150) DEFAULT '',
                `layout_json` mediumtext,
                `published_layout_json` mediumtext,
                `status` tinyint(4) DEFAULT 1,
                `published_at` int(11) NOT NULL DEFAULT 0,
                `is_enabled` tinyint(4) DEFAULT 1,
                `version_no` int(11) DEFAULT 1,
                `create_time` int(11) DEFAULT NULL,
                `update_time` int(11) DEFAULT NULL,
                PRIMARY KEY (`id`),
                UNIQUE KEY `uniq_page_key` (`page_key`),
                KEY `idx_page_type_ref_id` (`page_type`,`ref_id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci",
            $prefix . 'builder_page_history' => "CREATE TABLE IF NOT EXISTS `{$prefix}builder_page_history` (
                `id` int(11) NOT NULL AUTO_INCREMENT,
                `builder_page_id` int(11) NOT NULL,
                `version_no` int(11) NOT NULL,
                `layout_json` mediumtext,
                `create_time` int(11) DEFAULT NULL,
                PRIMARY KEY (`id`),
                KEY `idx_builder_page_id` (`builder_page_id`),
                KEY `idx_builder_page_time` (`builder_page_id`,`create_time`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci",
            $prefix . 'builder_edit_session' => "CREATE TABLE IF NOT EXISTS `{$prefix}builder_edit_session` (
                `id` int(11) NOT NULL AUTO_INCREMENT,
                `page_key` varchar(60) NOT NULL,
                `editor_token` varchar(80) NOT NULL,
                `admin_id` int(11) NOT NULL DEFAULT 0,
                `admin_name` varchar(80) NOT NULL DEFAULT '',
                `version_no` int(11) NOT NULL DEFAULT 0,
                `last_seen` int(11) NOT NULL DEFAULT 0,
                `create_time` int(11) NOT NULL DEFAULT 0,
                PRIMARY KEY (`id`),
                UNIQUE KEY `uniq_editor_token` (`editor_token`),
                KEY `idx_builder_presence` (`page_key`,`last_seen`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci",
        ),
        'columns' => array(
            $prefix . 'license.license_code' => array(
                'table' => $prefix . 'license',
                'column' => 'license_code',
                'sql' => "ALTER TABLE `{$prefix}license` ADD COLUMN `license_code` varchar(64) DEFAULT NULL AFTER `id`",
            ),
            $prefix . 'license.license_domain' => array(
                'table' => $prefix . 'license',
                'column' => 'license_domain',
                'sql' => "ALTER TABLE `{$prefix}license` ADD COLUMN `license_domain` varchar(255) DEFAULT NULL AFTER `license_code`",
            ),
            $prefix . 'license.license_level' => array(
                'table' => $prefix . 'license',
                'column' => 'license_level',
                'sql' => "ALTER TABLE `{$prefix}license` ADD COLUMN `license_level` tinyint(4) DEFAULT 0 AFTER `license_domain`",
            ),
            $prefix . 'license.license_expire' => array(
                'table' => $prefix . 'license',
                'column' => 'license_expire',
                'sql' => "ALTER TABLE `{$prefix}license` ADD COLUMN `license_expire` date DEFAULT NULL AFTER `license_level`",
            ),
            $prefix . 'license.license_status' => array(
                'table' => $prefix . 'license',
                'column' => 'license_status',
                'sql' => "ALTER TABLE `{$prefix}license` ADD COLUMN `license_status` tinyint(4) DEFAULT 1 AFTER `license_expire`",
            ),
            $prefix . 'license.site_id' => array(
                'table' => $prefix . 'license',
                'column' => 'site_id',
                'sql' => "ALTER TABLE `{$prefix}license` ADD COLUMN `site_id` varchar(64) DEFAULT NULL AFTER `license_status`",
            ),
            $prefix . 'license.bind_count' => array(
                'table' => $prefix . 'license',
                'column' => 'bind_count',
                'sql' => "ALTER TABLE `{$prefix}license` ADD COLUMN `bind_count` int(11) DEFAULT 0 AFTER `site_id`",
            ),
            $prefix . 'license.max_domains' => array(
                'table' => $prefix . 'license',
                'column' => 'max_domains',
                'sql' => "ALTER TABLE `{$prefix}license` ADD COLUMN `max_domains` int(11) DEFAULT 1 AFTER `bind_count`",
            ),
            $prefix . 'license.create_time' => array(
                'table' => $prefix . 'license',
                'column' => 'create_time',
                'sql' => "ALTER TABLE `{$prefix}license` ADD COLUMN `create_time` int(11) DEFAULT NULL AFTER `max_domains`",
            ),
            $prefix . 'license.update_time' => array(
                'table' => $prefix . 'license',
                'column' => 'update_time',
                'sql' => "ALTER TABLE `{$prefix}license` ADD COLUMN `update_time` int(11) DEFAULT NULL AFTER `create_time`",
            ),
            $prefix . 'page.channel_id' => array(
                'table' => $prefix . 'page',
                'column' => 'channel_id',
                'sql' => "ALTER TABLE `{$prefix}page` ADD COLUMN `channel_id` int(11) NOT NULL DEFAULT 0 AFTER `id`",
            ),
            $prefix . 'page.status' => array(
                'table' => $prefix . 'page',
                'column' => 'status',
                'sql' => "ALTER TABLE `{$prefix}page` ADD COLUMN `status` tinyint(4) NOT NULL DEFAULT 1 AFTER `page_content`",
            ),
            $prefix . 'page.view' => array(
                'table' => $prefix . 'page',
                'column' => 'view',
                'sql' => "ALTER TABLE `{$prefix}page` ADD COLUMN `view` int(11) NOT NULL DEFAULT 0 AFTER `update_time`",
            ),
            $prefix . 'page.litpic' => array(
                'table' => $prefix . 'page',
                'column' => 'litpic',
                'sql' => "ALTER TABLE `{$prefix}page` ADD COLUMN `litpic` varchar(255) DEFAULT '' AFTER `view`",
            ),
            $prefix . 'template_showcase.subtitle' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'subtitle',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `subtitle` varchar(100) DEFAULT NULL AFTER `title`",
            ),
            $prefix . 'template_showcase.thumb' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'thumb',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `thumb` varchar(255) DEFAULT NULL AFTER `subtitle`",
            ),
            $prefix . 'template_showcase.images' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'images',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `images` text AFTER `thumb`",
            ),
            $prefix . 'template_showcase.template_dir' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'template_dir',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `template_dir` varchar(50) DEFAULT NULL AFTER `images`",
            ),
            $prefix . 'template_showcase.template_type' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'template_type',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `template_type` tinyint(4) DEFAULT 1 AFTER `template_dir`",
            ),
            $prefix . 'template_showcase.author' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'author',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `author` varchar(50) DEFAULT NULL AFTER `template_type`",
            ),
            $prefix . 'template_showcase.version' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'version',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `version` varchar(20) DEFAULT NULL AFTER `author`",
            ),
            $prefix . 'template_showcase.demo_url' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'demo_url',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `demo_url` varchar(255) DEFAULT NULL AFTER `version`",
            ),
            $prefix . 'template_showcase.download_url' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'download_url',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `download_url` varchar(255) DEFAULT NULL AFTER `demo_url`",
            ),
            $prefix . 'template_showcase.description' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'description',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `description` text AFTER `download_url`",
            ),
            $prefix . 'template_showcase.seo_title' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'seo_title',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `seo_title` varchar(200) DEFAULT NULL AFTER `description`",
            ),
            $prefix . 'template_showcase.seo_keywords' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'seo_keywords',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `seo_keywords` varchar(200) DEFAULT NULL AFTER `seo_title`",
            ),
            $prefix . 'template_showcase.seo_description' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'seo_description',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `seo_description` text AFTER `seo_keywords`",
            ),
            $prefix . 'template_showcase.click' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'click',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `click` int(11) DEFAULT 0 AFTER `seo_description`",
            ),
            $prefix . 'template_showcase.collect_count' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'collect_count',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `collect_count` int(11) DEFAULT 0 AFTER `click`",
            ),
            $prefix . 'template_showcase.use_count' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'use_count',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `use_count` int(11) DEFAULT 0 AFTER `collect_count`",
            ),
            $prefix . 'template_showcase.sort_order' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'sort_order',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `sort_order` int(11) DEFAULT 100 AFTER `use_count`",
            ),
            $prefix . 'template_showcase.is_recommend' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'is_recommend',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `is_recommend` tinyint(4) DEFAULT 0 AFTER `sort_order`",
            ),
            $prefix . 'template_showcase.status' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'status',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `status` tinyint(4) DEFAULT 1 AFTER `is_recommend`",
            ),
            $prefix . 'template_showcase.create_time' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'create_time',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `create_time` int(11) DEFAULT NULL AFTER `status`",
            ),
            $prefix . 'template_showcase.update_time' => array(
                'table' => $prefix . 'template_showcase',
                'column' => 'update_time',
                'sql' => "ALTER TABLE `{$prefix}template_showcase` ADD COLUMN `update_time` int(11) DEFAULT NULL AFTER `create_time`",
            ),
            $prefix . 'builder_page.page_key' => array(
                'table' => $prefix . 'builder_page',
                'column' => 'page_key',
                'sql' => "ALTER TABLE `{$prefix}builder_page` ADD COLUMN `page_key` varchar(60) NOT NULL AFTER `id`",
            ),
            $prefix . 'builder_page.page_type' => array(
                'table' => $prefix . 'builder_page',
                'column' => 'page_type',
                'sql' => "ALTER TABLE `{$prefix}builder_page` ADD COLUMN `page_type` varchar(20) NOT NULL DEFAULT 'home' AFTER `page_key`",
            ),
            $prefix . 'builder_page.ref_id' => array(
                'table' => $prefix . 'builder_page',
                'column' => 'ref_id',
                'sql' => "ALTER TABLE `{$prefix}builder_page` ADD COLUMN `ref_id` int(11) NOT NULL DEFAULT 0 AFTER `page_type`",
            ),
            $prefix . 'builder_page.title' => array(
                'table' => $prefix . 'builder_page',
                'column' => 'title',
                'sql' => "ALTER TABLE `{$prefix}builder_page` ADD COLUMN `title` varchar(150) DEFAULT '' AFTER `ref_id`",
            ),
            $prefix . 'builder_page.layout_json' => array(
                'table' => $prefix . 'builder_page',
                'column' => 'layout_json',
                'sql' => "ALTER TABLE `{$prefix}builder_page` ADD COLUMN `layout_json` mediumtext AFTER `title`",
            ),
            $prefix . 'builder_page.status' => array(
                'table' => $prefix . 'builder_page',
                'column' => 'status',
                'sql' => "ALTER TABLE `{$prefix}builder_page` ADD COLUMN `status` tinyint(4) DEFAULT 1 AFTER `layout_json`",
            ),
            $prefix . 'builder_page.is_enabled' => array(
                'table' => $prefix . 'builder_page',
                'column' => 'is_enabled',
                'sql' => "ALTER TABLE `{$prefix}builder_page` ADD COLUMN `is_enabled` tinyint(4) DEFAULT 1 AFTER `status`",
            ),
            $prefix . 'builder_page.version_no' => array(
                'table' => $prefix . 'builder_page',
                'column' => 'version_no',
                'sql' => "ALTER TABLE `{$prefix}builder_page` ADD COLUMN `version_no` int(11) DEFAULT 1 AFTER `is_enabled`",
            ),
            $prefix . 'builder_page.create_time' => array(
                'table' => $prefix . 'builder_page',
                'column' => 'create_time',
                'sql' => "ALTER TABLE `{$prefix}builder_page` ADD COLUMN `create_time` int(11) DEFAULT NULL AFTER `version_no`",
            ),
            $prefix . 'builder_page.update_time' => array(
                'table' => $prefix . 'builder_page',
                'column' => 'update_time',
                'sql' => "ALTER TABLE `{$prefix}builder_page` ADD COLUMN `update_time` int(11) DEFAULT NULL AFTER `create_time`",
            ),
            $prefix . 'builder_page.published_layout_json' => array(
                'table' => $prefix . 'builder_page',
                'column' => 'published_layout_json',
                'sql' => "ALTER TABLE `{$prefix}builder_page` ADD COLUMN `published_layout_json` mediumtext AFTER `layout_json`",
            ),
            $prefix . 'builder_page.published_at' => array(
                'table' => $prefix . 'builder_page',
                'column' => 'published_at',
                'sql' => "ALTER TABLE `{$prefix}builder_page` ADD COLUMN `published_at` int(11) NOT NULL DEFAULT 0 AFTER `status`",
            ),
            $prefix . 'builder_page_history.builder_page_id' => array(
                'table' => $prefix . 'builder_page_history',
                'column' => 'builder_page_id',
                'sql' => "ALTER TABLE `{$prefix}builder_page_history` ADD COLUMN `builder_page_id` int(11) NOT NULL AFTER `id`",
            ),
            $prefix . 'builder_page_history.version_no' => array(
                'table' => $prefix . 'builder_page_history',
                'column' => 'version_no',
                'sql' => "ALTER TABLE `{$prefix}builder_page_history` ADD COLUMN `version_no` int(11) NOT NULL AFTER `builder_page_id`",
            ),
            $prefix . 'builder_page_history.layout_json' => array(
                'table' => $prefix . 'builder_page_history',
                'column' => 'layout_json',
                'sql' => "ALTER TABLE `{$prefix}builder_page_history` ADD COLUMN `layout_json` mediumtext AFTER `version_no`",
            ),
            $prefix . 'builder_page_history.create_time' => array(
                'table' => $prefix . 'builder_page_history',
                'column' => 'create_time',
                'sql' => "ALTER TABLE `{$prefix}builder_page_history` ADD COLUMN `create_time` int(11) DEFAULT NULL AFTER `layout_json`",
            ),
        ),
        'indexes' => array(
            $prefix . 'builder_page.uniq_page_key' => array(
                'table' => $prefix . 'builder_page',
                'name' => 'uniq_page_key',
                'sql' => "ALTER TABLE `{$prefix}builder_page` ADD UNIQUE KEY `uniq_page_key` (`page_key`)",
            ),
            $prefix . 'builder_page.idx_page_type_ref_id' => array(
                'table' => $prefix . 'builder_page',
                'name' => 'idx_page_type_ref_id',
                'sql' => "ALTER TABLE `{$prefix}builder_page` ADD KEY `idx_page_type_ref_id` (`page_type`,`ref_id`)",
            ),
            $prefix . 'builder_page_history.idx_builder_page_id' => array(
                'table' => $prefix . 'builder_page_history',
                'name' => 'idx_builder_page_id',
                'sql' => "ALTER TABLE `{$prefix}builder_page_history` ADD KEY `idx_builder_page_id` (`builder_page_id`)",
            ),
            $prefix . 'builder_page_history.idx_builder_page_time' => array(
                'table' => $prefix . 'builder_page_history',
                'name' => 'idx_builder_page_time',
                'sql' => "ALTER TABLE `{$prefix}builder_page_history` ADD KEY `idx_builder_page_time` (`builder_page_id`,`create_time`)",
            ),
        ),
        'config_defaults' => array(
            'site_template' => array(
                'value' => 'default',
                'config_type' => 'text',
                'config_group' => 'basic',
            ),
            'last_template_key' => array(
                'from_config' => 'site_template',
                'fallback' => 'default',
                'config_type' => 'text',
                'config_group' => 'basic',
            ),
        ),
    );
}
