SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- 检查表是否存在，如果存在则跳过创建
CREATE TABLE IF NOT EXISTS `oauth_config` (
    `id` bigint NOT NULL AUTO_INCREMENT,
    `platform` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'platform',
    `config` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'OAuth Configuration',
    `redirect` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'Redirect URL',
    `enabled` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Is Enabled',
    `created_at` datetime(3) DEFAULT NULL COMMENT 'Create Time',
    `updated_at` datetime(3) DEFAULT NULL COMMENT 'Update Time',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uni_oauth_config_platform` (`platform`)
    ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- 插入记录时忽略重复记录
BEGIN;
INSERT IGNORE INTO `oauth_config` (`id`, `platform`, `config`, `redirect`, `enabled`, `created_at`, `updated_at`) VALUES
(1, 'apple', '{\"team_id\":\"\",\"key_id\":\"\",\"client_id\":\"\",\"client_secret\":\"\"}', '', 0, '2025-01-26 20:11:15.292', '2025-01-26 20:11:15.292'),
(2, 'google', '{\"client_id\":\"\",\"client_secret\":\"\"}', '', 0, '2025-01-26 20:11:15.292', '2025-01-26 20:11:15.292'),
(3, 'github', '{\"client_id\":\"\",\"client_secret\":\"\"}', '', 0, '2025-01-26 20:11:15.292', '2025-01-26 20:11:15.292'),
(4, 'facebook', '{\"client_id\":\"\",\"client_secret\":\"\"}', '', 0, '2025-01-26 20:11:15.292', '2025-01-26 20:11:15.292'),
(5, 'telegram', '{\"bot\":\"\",\"bot_token\":\"\"}', '', 0, '2025-01-26 20:11:15.292', '2025-01-26 20:11:15.292');
COMMIT;

-- 检测更新设置表
BEGIN;
INSERT IGNORE INTO `system` (`category`, `key`, `value`, `type`, `desc`, `created_at`, `updated_at`) VALUES
('sms', 'SmsEnabled', 'false', 'bool', '是否启用短信功能', NOW(), NOW()),
('sms', 'SmsKey', 'your-key', 'string', '短信服务用户名或Key',NOW(), NOW()),
('sms', 'SmsSecret', 'your-secret', 'string', '短信服务密码或Secret', NOW(), NOW()),
('sms', 'SmsSign', 'your-sign', 'string', '短信签名', NOW(), NOW()),
('sms', 'SmsTemplate', 'your-template', 'string', '短信模板ID', NOW(), NOW()),
('sms', 'SmsRegion', 'cn-hangzhou', 'string', '短信服务所在区域（适用于阿里云）', NOW(), NOW()),
('sms', 'SmsTemplate', '您的验证码是{{.Code}}，请在5分钟内使用。', 'string', '自定义短信模板', NOW(), NOW()),
('sms', 'SmsTemplateCode', 'SMS_12345678', 'string', '阿里云国内短信模板代码',NOW(),NOW()),
('sms', 'SmsTemplateParam', '{\"code\":{{.Code}}}', 'string', '短信模板参数', NOW(), NOW()),
('sms', 'SmsPlatform', 'smsbao', 'string', '当前使用的短信平台', NOW(), NOW()),
('sms', 'SmsLimit', '10', 'int64', '可以发送的短信最大数量', NOW(), NOW()),
('sms', 'SmsInterval', '60', 'int64', '发送短信的时间间隔（单位：秒）',NOW(), NOW()),
('sms', 'SmsExpireTime', '300', 'int64', '短信验证码的过期时间（单位：秒）',NOW(), NOW()),
('email', 'EmailEnabled', 'true', 'bool', '启用邮箱登陆',NOW(), NOW()),
('email', 'EmailSmtpHost', '', 'string', '邮箱服务器地址', NOW(), NOW()),
('email', 'EmailSmtpPort', '465', 'int', '邮箱服务器端口',NOW(), NOW()),
('email', 'EmailSmtpUser', 'domain@f1shyu.com', 'string', '邮箱服务器用户名', NOW(), NOW()),
('email', 'EmailSmtpPass', 'password', 'string', '邮箱服务器密码', NOW(), NOW()),
('email', 'EmailSmtpFrom', 'domain@f1shyu.com', 'string', '发送邮件的邮箱',NOW(), NOW()),
('email', 'EmailSmtpSSL', 'true', 'bool', '邮箱服务器加密方式',NOW(), NOW()),
('email', 'EmailTemplate', '%s', 'string', '邮件模板',NOW(), NOW()),
('email', 'VerifyEmailTemplate', '', 'string', 'Verify Email template',NOW(), NOW()),
('email', 'MaintenanceEmailTemplate', '', 'string', 'Maintenance Email template',NOW(), NOW()),
('email', 'ExpirationEmailTemplate', '', 'string', 'Expiration Email template', NOW(), NOW()),
('email', 'EmailEnableVerify', 'true', 'bool', '是否开启邮箱验证', NOW(), NOW()),
('email', 'EmailEnableDomainSuffix', 'false', 'bool', '是否开启邮箱域名后缀限制',NOW(), NOW()),
('email', 'EmailDomainSuffixList', 'qq.com', 'string', '邮箱域名后缀列表',NOW(), NOW());
COMMIT;

-- User Device
CREATE TABLE IF NOT EXISTS `user_device` (
   `id` bigint NOT NULL AUTO_INCREMENT,
   `user_id` bigint NOT NULL COMMENT 'User ID',
   `device_number` varchar(191) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT 'Device Number.',
   `online` tinyint(1) NOT NULL DEFAULT '1' COMMENT 'Online',
   `enabled` tinyint(1) NOT NULL DEFAULT '1' COMMENT 'EnableDeviceNumber',
   `last_online` datetime(3) DEFAULT NULL COMMENT 'Last Online',
   `created_at` datetime(3) DEFAULT NULL COMMENT 'Creation Time',
   `updated_at` datetime(3) DEFAULT NULL COMMENT 'Update Time',
   PRIMARY KEY (`id`),
   KEY `idx_user_id` (`user_id`),
   CONSTRAINT `fk_user_user_devices` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Mobile
CREATE TABLE IF NOT EXISTS `sms` (
   `id` bigint NOT NULL AUTO_INCREMENT,
   `content` text COLLATE utf8mb4_general_ci,
   `platform` varchar(64) COLLATE utf8mb4_general_ci DEFAULT NULL,
   `area_code` varchar(64) COLLATE utf8mb4_general_ci DEFAULT NULL,
   `telephone` varchar(64) COLLATE utf8mb4_general_ci DEFAULT NULL,
   `status` tinyint(1) DEFAULT '1',
   `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
   PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Application Config
CREATE TABLE IF NOT EXISTS `application_config` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `app_id` bigint NOT NULL DEFAULT '0' COMMENT 'App id',
  `encryption_key` text COLLATE utf8mb4_general_ci COMMENT 'Encryption Key',
  `encryption_method` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT 'Encryption Method',
  `domains` text COLLATE utf8mb4_general_ci,
  `startup_picture` text COLLATE utf8mb4_general_ci,
  `startup_picture_skip_time` bigint NOT NULL DEFAULT '0' COMMENT 'Startup Picture Skip Time',
  `created_at` datetime(3) DEFAULT NULL COMMENT 'Create Time',
  `updated_at` datetime(3) DEFAULT NULL COMMENT 'Update Time',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Application Version
CREATE TABLE IF NOT EXISTS `application_version` (
   `id` bigint NOT NULL AUTO_INCREMENT,
   `url` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '应用地址',
   `version` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '应用版本',
   `platform` varchar(50) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '应用平台',
   `is_default` tinyint(1) NOT NULL DEFAULT '0' COMMENT '默认版本',
   `description` text COLLATE utf8mb4_general_ci COMMENT '更新描述',
   `application_id` bigint DEFAULT NULL COMMENT '所属应用',
   `created_at` datetime(3) DEFAULT NULL COMMENT '创建时间',
   `updated_at` datetime(3) DEFAULT NULL COMMENT '更新时间',
   PRIMARY KEY (`id`),
   KEY `fk_application_application_versions` (`application_id`),
   CONSTRAINT `fk_application_application_versions` FOREIGN KEY (`application_id`) REFERENCES `application` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

UPDATE `subscribe` SET `unit_time`='Month' WHERE unit_time = '';

SET FOREIGN_KEY_CHECKS = 1;
