-- ========================================
-- 虚境起源网站管理后台数据库初始化脚本
-- ========================================
-- 创建日期: 2026-02-02
-- 说明: 包含所有必需的数据库、表和初始数据
-- ========================================

-- 创建数据库
CREATE DATABASE IF NOT EXISTS website_admin CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

USE website_admin;

-- ========================================
-- 1. 管理员用户表
-- ========================================
CREATE TABLE IF NOT EXISTS admin_users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  username VARCHAR(50) UNIQUE NOT NULL,
  password VARCHAR(255) NOT NULL,
  email VARCHAR(100),
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  last_login TIMESTAMP NULL,
  INDEX idx_username (username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ========================================
-- 2. 合作伙伴表
-- ========================================
CREATE TABLE IF NOT EXISTS partners (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  logo VARCHAR(255) NOT NULL,
  url VARCHAR(255),
  sort_order INT DEFAULT 0,
  status TINYINT DEFAULT 1 COMMENT '1=active, 0=inactive',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX idx_status (status),
  INDEX idx_sort_order (sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ========================================
-- 3. 系统设置表
-- ========================================
CREATE TABLE IF NOT EXISTS system_settings (
  id INT AUTO_INCREMENT PRIMARY KEY,
  setting_key VARCHAR(100) UNIQUE NOT NULL,
  setting_value TEXT,
  description VARCHAR(255),
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX idx_setting_key (setting_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ========================================
-- 4. 视频表
-- ========================================
CREATE TABLE IF NOT EXISTS videos (
  id INT AUTO_INCREMENT PRIMARY KEY,
  file_id VARCHAR(100) UNIQUE COMMENT '腾讯云VOD文件ID',
  title_cn VARCHAR(200) NOT NULL COMMENT '中文标题',
  title_en VARCHAR(200) NOT NULL COMMENT '英文标题',
  desc_cn TEXT COMMENT '中文描述',
  desc_en TEXT COMMENT '英文描述',
  cover_url VARCHAR(500) COMMENT '封面图URL',
  video_url VARCHAR(500) COMMENT '视频播放URL',
  duration INT COMMENT '视频时长(秒)',
  file_size BIGINT COMMENT '文件大小(字节)',
  status TINYINT DEFAULT 1 COMMENT '1=active, 0=inactive, 2=processing',
  sort_order INT DEFAULT 0,
  upload_status VARCHAR(50) DEFAULT 'pending' COMMENT 'pending, uploading, success, failed',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX idx_status (status),
  INDEX idx_sort_order (sort_order),
  INDEX idx_file_id (file_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ========================================
-- 5. VOD配置表（包含COS和防盗链配置）
-- ========================================
CREATE TABLE IF NOT EXISTS vod_config (
  id INT AUTO_INCREMENT PRIMARY KEY,
  secret_id VARCHAR(255) NOT NULL COMMENT '腾讯云SecretId',
  secret_key VARCHAR(255) NOT NULL COMMENT '腾讯云SecretKey',
  region VARCHAR(50) DEFAULT 'ap-guangzhou' COMMENT '地域',
  sub_app_id VARCHAR(50) COMMENT '点播子应用ID',
  `procedure` VARCHAR(100) COMMENT '任务流',
  storage_region VARCHAR(50) COMMENT '存储地域',
  cos_bucket VARCHAR(100) COMMENT 'COS存储桶名称',
  cos_region VARCHAR(50) DEFAULT 'ap-guangzhou' COMMENT 'COS地域',
  cos_path VARCHAR(100) DEFAULT 'video-covers/' COMMENT 'COS存储路径前缀',
  sign_key VARCHAR(100) COMMENT 'VOD防盗链签名密钥',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ========================================
-- 初始数据插入
-- ========================================

-- 插入默认管理员账户
-- 用户名: admin
-- 密码: admin123456 (使用bcryptjs加密，10轮salt)
INSERT INTO admin_users (username, password, email)
VALUES ('admin', '$2a$10$PGOF6JXp3CGjg.5evfJlG.oeOr/JvxYECbLxn2ypzsRKcT7UK5.YK', 'admin@xuking.com')
ON DUPLICATE KEY UPDATE username=username;

-- 插入默认存储配置
INSERT INTO system_settings (setting_key, setting_value, description)
VALUES
  ('logo_upload_dir', 'img/logo', 'Directory for partner logo uploads'),
  ('logo_max_size', '5242880', 'Maximum logo file size in bytes (5MB)'),
  ('allowed_extensions', 'jpg,jpeg,png,gif,svg', 'Allowed file extensions for logo upload')
ON DUPLICATE KEY UPDATE setting_key=setting_key;

-- 插入现有合作伙伴数据
INSERT INTO partners (name, logo, url, sort_order, status) VALUES
('vivo', 'img/logo/vivo.png', '', 1, 1),
('BOE', 'img/logo/boe.png', '', 2, 1),
('北京大运河博物馆-首都博物馆东馆', 'img/logo/bwg.png', '', 3, 1),
('江西日报', 'img/logo/jxrb.png', '', 4, 1),
('丽江市文化和旅游局', 'img/logo/ljs.png', '', 5, 1),
('Meta', 'img/logo/meta.png', '', 6, 1),
('中国文化传媒', 'img/logo/zgwhcm.png', '', 7, 1)
ON DUPLICATE KEY UPDATE name=VALUES(name);

-- 插入默认VOD配置（空配置，需要后台填写）
INSERT INTO vod_config (secret_id, secret_key, region)
VALUES ('', '', 'ap-guangzhou')
ON DUPLICATE KEY UPDATE id=id;

-- ========================================
-- 初始化完成
-- ========================================
