-- 家长微信账号表
CREATE TABLE parents (
    user_id BIGINT PRIMARY KEY AUTO_INCREMENT,
    openid VARCHAR(64) UNIQUE NOT NULL COMMENT '微信openid',
    unionid VARCHAR(64) COMMENT '微信unionid',
    nickname VARCHAR(64) COMMENT '家长昵称',
    avatar_url VARCHAR(255),
    phone VARCHAR(20),
    status TINYINT DEFAULT 1 COMMENT '0禁用 1正常',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_unionid (unionid),
    INDEX idx_phone (phone)
) ENGINE=InnoDB COMMENT='家长用户表';

CREATE TABLE children (
    profile_id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '档案主键',
    user_id BIGINT NOT NULL COMMENT '所属家长ID',
    
    -- 核心资料
    child_name VARCHAR(32) NOT NULL COMMENT '儿童昵称（设备唤醒名）',
    child_gender TINYINT DEFAULT 2 COMMENT '性别：0女 1男 2保密',
    child_birthday DATE COMMENT '出生日期',
    child_age TINYINT UNSIGNED GENERATED ALWAYS AS (
        TIMESTAMPDIFF(YEAR, child_birthday, CURDATE())
    ) STORED COMMENT '自动计算年龄（用于适龄内容过滤）',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    
    -- 约束与索引
    FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
)ENGINE=InnoDB COMMENT='儿童档案表（一个家长可创建多个档案）';

CREATE TABLE cards (
    card_id BIGINT PRIMARY KEY AUTO_INCREMENT,
    card_uuid VARCHAR(64) UNIQUE NOT NULL COMMENT 'NFC卡片UUID',
    device_id BIGINT UNIQUE COMMENT '绑定设备ID',
    card_name VARCHAR(64) COMMENT '名片名称',
    status TINYINT DEFAULT 0 COMMENT '0未绑定 1已绑定 2挂失',
    total_swaps INT DEFAULT 0 COMMENT '交换次数',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    
    FOREIGN KEY (device_id) REFERENCES devices(device_id),
    INDEX idx_uuid (card_uuid),
    INDEX idx_device (device_id)
) ENGINE=InnoDB COMMENT='实体交友名片表';
-- Todo 待修改
CREATE TABLE device_settings (
    setting_id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '设置主键',
    device_id BIGINT NOT NULL UNIQUE COMMENT '设备ID（一对一）',
    
    -- 休眠与时间管理（防熬夜）
    sleep_mode TINYINT DEFAULT 0 COMMENT '休眠模式：0关闭 1定时休眠 2连续使用限制',
    sleep_start TIME COMMENT '定时休眠开始时间，如 22:00:00',
    sleep_end TIME COMMENT '定时休眠结束时间，如 07:00:00',
    timezone VARCHAR(32) DEFAULT 'Asia/Shanghai' COMMENT '时区，解决跨时区问题',
    
    -- 音量与硬件控制
    volume TINYINT UNSIGNED DEFAULT 80 COMMENT '系统音量 0-100',
    brightness TINYINT UNSIGNED DEFAULT 100 COMMENT '指示灯亮度 0-100（0为关闭）',
    
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

) ENGINE=InnoDB COMMENT='设备设置表（家长远程管控配置）';

-- 初始化数据库表

-- 创建device_configs表
CREATE TABLE IF NOT EXISTS `device_configs` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `device_id` VARCHAR(64) NOT NULL,
  `selected_role_key` VARCHAR(64) NOT NULL,
  `preferred_language` VARCHAR(10) NULL,
  `volume` INT NULL,
  `last_update_time` FLOAT NOT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `device_id_UNIQUE` (`device_id` ASC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 创建conversation_histories表
CREATE TABLE IF NOT EXISTS `conversation_histories` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `device_id` VARCHAR(64) NOT NULL,
  `role_key` VARCHAR(64) NOT NULL,
  `last_interaction_time` FLOAT NOT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  INDEX `idx_device_id` (`device_id` ASC),
  INDEX `idx_role_key` (`role_key` ASC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 创建conversation_messages表
CREATE TABLE IF NOT EXISTS `conversation_messages` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `conversation_id` INT NOT NULL,
  `is_user` TINYINT(1) NOT NULL DEFAULT 0,
  `content` TEXT NOT NULL,
  `timestamp` FLOAT NOT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  INDEX `idx_conversation_id` (`conversation_id` ASC),
  CONSTRAINT `fk_messages_conversation`
    FOREIGN KEY (`conversation_id`)
    REFERENCES `conversation_histories` (`id`)
    ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 创建roles表
CREATE TABLE IF NOT EXISTS `roles` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `role_key` VARCHAR(64) NOT NULL,
  `name` VARCHAR(128) NOT NULL,
  `description` TEXT NULL,
  `content` TEXT NOT NULL,
  `default_language` VARCHAR(10) NULL,
  `asr_provider` VARCHAR(64) NULL,
  `llm_provider` VARCHAR(64) NULL,
  `tts_provider` VARCHAR(64) NULL,
  `competitive_llm_mode` TINYINT(1) NULL,
  `volcano_model_id` VARCHAR(64) NULL,
  `volcano_voice_type` VARCHAR(64) NULL,
  `tencent_voice_type` VARCHAR(64) NULL,
  `aliyun_voice_name` VARCHAR(64) NULL,
  `minimax_voice_id` VARCHAR(64) NULL,
  `url` VARCHAR(255) NULL,
  `homophones` JSON NULL,
  `enabled` TINYINT(1) NOT NULL DEFAULT 1,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `role_key_UNIQUE` (`role_key` ASC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 创建role_languages表
CREATE TABLE IF NOT EXISTS `role_languages` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `role_id` INT NOT NULL,
  `language_code` VARCHAR(10) NOT NULL,
  `name` VARCHAR(128) NULL,
  `content` TEXT NULL,
  `asr_provider` VARCHAR(64) NULL,
  `llm_provider` VARCHAR(64) NULL,
  `tts_provider` VARCHAR(64) NULL,
  `volcano_voice_type` VARCHAR(64) NULL,
  `tencent_voice_type` VARCHAR(64) NULL,
  `aliyun_voice_name` VARCHAR(64) NULL,
  `minimax_voice_id` VARCHAR(64) NULL,
  `url` VARCHAR(255) NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `uix_role_language` (`role_id`, `language_code`),
  CONSTRAINT `fk_role_languages_role`
    FOREIGN KEY (`role_id`)
    REFERENCES `roles` (`id`)
    ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 创建device_auth表
CREATE TABLE IF NOT EXISTS `device_auth` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `device_id` VARCHAR(64) NOT NULL,
  `serial_number` VARCHAR(64) NOT NULL,
  `batch_id` VARCHAR(20) NULL,
  `is_active` TINYINT(1) NOT NULL DEFAULT 1,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `device_id_UNIQUE` (`device_id` ASC),
  INDEX `idx_batch_id` (`batch_id` ASC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 创建device_firmware_update表
CREATE TABLE IF NOT EXISTS `device_firmware_update` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `device_id` VARCHAR(64) NOT NULL,
  `serial_number` VARCHAR(64) NOT NULL,
  `mac_address` VARCHAR(512) NULL,
  `firmware_version` VARCHAR(64) NOT NULL,
  `update_status` VARCHAR(32) NOT NULL DEFAULT 'success',
  `progress` FLOAT NULL DEFAULT 0.0,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `device_id_UNIQUE` (`device_id` ASC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 创建system_config表
CREATE TABLE IF NOT EXISTS `system_config` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `config_key` VARCHAR(128) NOT NULL,
  `config_value` TEXT NULL,
  `description` VARCHAR(255) NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `config_key_UNIQUE` (`config_key` ASC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 添加初始角色配置
INSERT INTO `device_configs` (`device_id`, `selected_role_key`, `preferred_language`, `last_update_time`)
VALUES ('default', 'assistant', 'zh', UNIX_TIMESTAMP())
ON DUPLICATE KEY UPDATE `selected_role_key`=`selected_role_key`;

-- 添加默认系统配置
INSERT INTO `system_config` (`config_key`, `config_value`, `description`)
VALUES 
('latest_firmware_version', '1.0.0', '最新固件版本'),
('update_firmware_url', 'https://example.com/firmware/latest.bin', '固件更新URL')
ON DUPLICATE KEY UPDATE `config_value`=`config_value`;

-- 添加默认角色
INSERT INTO `roles` (`role_key`, `name`, `description`, `content`, `default_language`, `enabled`)
VALUES (
  'assistant',
  '智能助手',
  '默认智能助手角色',
  '你是一个友好的智能助手，乐于帮助用户解答问题。',
  'zh',
  1
)
ON DUPLICATE KEY UPDATE `role_key`=`role_key`;