-- ----------------------------
-- 1. 护照实体表 (预先生成，供用户扫码绑定)
-- ----------------------------
CREATE TABLE `adam_caomei_passport` (
                                        `mid` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                        `passport_no` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '护照编号 (全局唯一，用于生成二维码)',
                                        `status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '绑定状态: 0-未绑定, 1-已绑定, 2-已作废',
                                        `user_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '绑定的用户ID',
                                        `bound_at` datetime DEFAULT NULL COMMENT '首次绑定时间',
                                        `unbound_at` datetime DEFAULT NULL COMMENT '最近一次解绑时间 (后台操作解绑时记录)',
                                        `batch_no` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '生成/印刷批次号 (便于后期溯源管理)',
                                        `remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '后台运营备注',
                                        `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                        `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                        PRIMARY KEY (`mid`),
                                        UNIQUE KEY `uk_passport_no` (`passport_no`),
                                        KEY `idx_user_id` (`user_id`),
                                        KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='草莓护照-实体编号表';

-- ----------------------------
-- 2. 徽章配置表 (运营后台配置，已移除 claimed_count，后台列表实时 COUNT 统计)
-- ----------------------------
CREATE TABLE `adam_caomei_badge` (
                                     `mid` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                     `badge_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '徽章ID',
                                     `name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '徽章名称',
                                     `icon` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '徽章图标 (Emoji字符或图片URL)',
                                     `type` tinyint(4) NOT NULL COMMENT '徽章类型: 1-护照纪念徽章, 2-演出纪念徽章, 3-特殊徽章',
                                     `performance_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '关联演出ID (仅演出纪念徽章必填，其他类型为空)',
                                     `display_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '上架状态: 0-下架(默认), 1-已发布',
                                     `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
                                     `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                     PRIMARY KEY (`mid`),
                                     KEY `idx_badge_id` (`badge_id`),
                                     KEY `idx_type_status` (`type`,`display_status`),
                                     KEY `idx_performance_id` (`performance_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='草莓护照-徽章配置表';

-- ----------------------------
-- 3. 用户徽章获得记录表 (支撑用户徽章墙与后台领取人数统计)
-- ----------------------------
CREATE TABLE `adam_caomei_user_badge` (
                                          `mid` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                          `user_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '用户id',
                                          `badge_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '徽章ID',
                                          `source` tinyint(4) NOT NULL COMMENT '获取途径: 1-绑定护照自动发放, 2-购票自动发放, 3-补签审核通过, 4-现场管理员手动发放',
                                          `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '获得时间',
                                          PRIMARY KEY (`mid`),
                                          UNIQUE KEY `uk_user_badge` (`user_id`,`badge_id`) COMMENT '联合唯一索引: 同一用户同一徽章只能获得一次',
                                          KEY `idx_badge_id` (`badge_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='草莓护照-用户徽章获得记录表';

-- ----------------------------
-- 4. 徽章补签申请记录表 (支撑补签审核流程)
-- ----------------------------
CREATE TABLE `adam_caomei_badge_apply_record` (
                                                  `mid` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                                  `apply_record_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '补签id',
                                                  `user_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '用户id',
                                                  `badge_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '申请补签的徽章ID',
                                                  `performance_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '关联的演出ID (冗余字段，便于后台筛选)',
                                                  `proof_image_url` varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '订单截图/凭证图片URL',
                                                  `audit_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '审核状态: 0-待审核, 1-已通过, 2-已驳回',
                                                  `reject_reason` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '驳回理由 (驳回时必填，用户端可见)',
                                                  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '申请时间',
                                                  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '审核/更新时间',
                                                  PRIMARY KEY (`mid`),
                                                  KEY `idx_record_id`(`apply_record_id`),
                                                  KEY `idx_user_id` (`user_id`),
                                                  KEY `idx_audit_status` (`audit_status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='草莓护照-徽章补签申请记录表';

-- 2026-04-13 新增排序字段
ALTER TABLE `adam_caomei_badge` ADD COLUMN `sort` int(11) NOT NULL DEFAULT 0 COMMENT '排序值，数值越大越靠前' AFTER `display_status`;

-- 2026-04-16 新增分享文案字段
ALTER TABLE `adam_caomei_badge` ADD COLUMN `share_text` varchar(255) NOT NULL DEFAULT '' COMMENT '徽章分享文案' AFTER `sort`;

-- 2026-04-23 新增徽章副标题字段
ALTER TABLE `adam_caomei_badge` ADD COLUMN `sub_title` varchar(32) NOT NULL DEFAULT '' COMMENT '徽章副标题（最多20字）' AFTER `name`;

-- TODO 上线前需要运行：adam_caomei_passport_inserts_BATCH-CAOMEI-B1.sql