-- cms_website 发布字段迁移（幂等版，兼容 MySQL 5.7 / 8.0）
-- 通过存储过程逐列判断，已存在则跳过，可重复执行
-- 注意：本脚本使用 DELIMITER，需在支持该语法的客户端执行
--   （mysql CLI、Navicat、MySQL Workbench 等）；
--   若用 1Panel 网页 SQL 控制台（不支持 DELIMITER），请改跑 cms_website_publish.sql（一次性）。

DELIMITER $$

DROP PROCEDURE IF EXISTS `add_cms_website_publish_columns`$$
CREATE PROCEDURE `add_cms_website_publish_columns`()
BEGIN
    IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='modules' AND TABLE_NAME='cms_website' AND COLUMN_NAME='publish_status') THEN
        ALTER TABLE `cms_website` ADD COLUMN `publish_status` VARCHAR(20) NULL DEFAULT 'developing' COMMENT '发布状态: developing开发中 pending_review待审核 published已上架 rejected审核未通过 deprecated已下架' AFTER `market`;
    END IF;
    IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='modules' AND TABLE_NAME='cms_website' AND COLUMN_NAME='price_type') THEN
        ALTER TABLE `cms_website` ADD COLUMN `price_type` VARCHAR(20) NULL COMMENT '定价模式: free免费 one_time一次性 subscription订阅' AFTER `publish_status`;
    END IF;
    IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='modules' AND TABLE_NAME='cms_website' AND COLUMN_NAME='subscription_period') THEN
        ALTER TABLE `cms_website` ADD COLUMN `subscription_period` VARCHAR(10) NULL COMMENT '订阅周期: month按月 year按年' AFTER `price_type`;
    END IF;
    IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='modules' AND TABLE_NAME='cms_website' AND COLUMN_NAME='app_description') THEN
        ALTER TABLE `cms_website` ADD COLUMN `app_description` VARCHAR(500) NULL COMMENT '应用简介（市场展示用）' AFTER `subscription_period`;
    END IF;
    IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='modules' AND TABLE_NAME='cms_website' AND COLUMN_NAME='detail_description') THEN
        ALTER TABLE `cms_website` ADD COLUMN `detail_description` TEXT NULL COMMENT '详细说明（富文本）' AFTER `app_description`;
    END IF;
    IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='modules' AND TABLE_NAME='cms_website' AND COLUMN_NAME='screenshots') THEN
        ALTER TABLE `cms_website` ADD COLUMN `screenshots` TEXT NULL COMMENT '应用截图（JSON数组字符串）' AFTER `detail_description`;
    END IF;
    IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='modules' AND TABLE_NAME='cms_website' AND COLUMN_NAME='install_count') THEN
        ALTER TABLE `cms_website` ADD COLUMN `install_count` INT NULL DEFAULT 0 COMMENT '安装/使用次数' AFTER `screenshots`;
    END IF;
    IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='modules' AND TABLE_NAME='cms_website' AND COLUMN_NAME='rating') THEN
        ALTER TABLE `cms_website` ADD COLUMN `rating` DECIMAL(3,1) NULL DEFAULT 0.0 COMMENT '评分（1-5）' AFTER `install_count`;
    END IF;
    IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='modules' AND TABLE_NAME='cms_website' AND COLUMN_NAME='reject_reason') THEN
        ALTER TABLE `cms_website` ADD COLUMN `reject_reason` VARCHAR(500) NULL COMMENT '审核拒绝原因' AFTER `rating`;
    END IF;
    IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='modules' AND TABLE_NAME='cms_website' AND COLUMN_NAME='publish_apply_time') THEN
        ALTER TABLE `cms_website` ADD COLUMN `publish_apply_time` DATETIME NULL COMMENT '提交审核时间' AFTER `reject_reason`;
    END IF;
    IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='modules' AND TABLE_NAME='cms_website' AND COLUMN_NAME='publish_time') THEN
        ALTER TABLE `cms_website` ADD COLUMN `publish_time` DATETIME NULL COMMENT '正式发布上架时间' AFTER `publish_apply_time`;
    END IF;
    IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='modules' AND TABLE_NAME='cms_website' AND COLUMN_NAME='reviewer_id') THEN
        ALTER TABLE `cms_website` ADD COLUMN `reviewer_id` INT NULL COMMENT '审核人用户ID' AFTER `publish_time`;
    END IF;
    IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='modules' AND TABLE_NAME='cms_website' AND COLUMN_NAME='review_time') THEN
        ALTER TABLE `cms_website` ADD COLUMN `review_time` DATETIME NULL COMMENT '审核操作时间' AFTER `reviewer_id`;
    END IF;
END$$

DELIMITER ;

CALL `add_cms_website_publish_columns`();
DROP PROCEDURE IF EXISTS `add_cms_website_publish_columns`;
