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

USE wecom_robot;

-- 会话表
CREATE TABLE IF NOT EXISTS `session` (
    `session_id` VARCHAR(128) NOT NULL COMMENT '会话ID',
    `customer_id` VARCHAR(64) NOT NULL COMMENT '客户ID (external_userid)',
    `kf_id` VARCHAR(64) NOT NULL COMMENT '客服账号ID (open_kfid)',
    `channel_type` VARCHAR(20) NOT NULL DEFAULT 'wechat' COMMENT '渠道类型: wechat/douyin/jd',
    `status` VARCHAR(20) NOT NULL DEFAULT 'AI' COMMENT '状态: AI/PENDING/MANUAL/CLOSED',
    `wx_service_state` INT DEFAULT 0 COMMENT '微信会话状态: 0-未处理/1-智能助手/2-待接入池/3-人工接待/4-已结束',
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `manual_cs_id` VARCHAR(64) DEFAULT NULL COMMENT '人工客服ID',
    `metadata` TEXT DEFAULT NULL COMMENT '扩展信息JSON',
    PRIMARY KEY (`session_id`),
    INDEX `idx_customer_id` (`customer_id`),
    INDEX `idx_kf_id` (`kf_id`),
    INDEX `idx_status` (`status`),
    INDEX `idx_channel_type` (`channel_type`),
    INDEX `idx_updated_at` (`updated_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='会话表';

-- 消息表
CREATE TABLE IF NOT EXISTS `message` (
    `msg_id` VARCHAR(128) NOT NULL COMMENT '消息ID',
    `session_id` VARCHAR(128) NOT NULL COMMENT '会话ID',
    `sender_type` VARCHAR(20) NOT NULL COMMENT '发送者类型: customer/ai/manual',
    `sender_id` VARCHAR(64) NOT NULL COMMENT '发送者标识',
    `content` TEXT NOT NULL COMMENT '消息内容',
    `msg_type` VARCHAR(20) NOT NULL DEFAULT 'text' COMMENT '消息类型: text/image/link等',
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `raw_data` TEXT DEFAULT NULL COMMENT '原始消息数据JSON',
    PRIMARY KEY (`msg_id`),
    INDEX `idx_session_id` (`session_id`),
    INDEX `idx_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='消息表';

-- 客服账号表
CREATE TABLE IF NOT EXISTS `kf_account` (
    `kf_id` VARCHAR(64) NOT NULL COMMENT '客服账号ID',
    `name` VARCHAR(100) DEFAULT NULL COMMENT '客服昵称',
    `avatar` VARCHAR(500) DEFAULT NULL COMMENT '头像URL',
    `status` VARCHAR(20) NOT NULL DEFAULT 'offline' COMMENT '状态: online/offline',
    `bind_manual_id` VARCHAR(64) DEFAULT NULL COMMENT '绑定的企业微信员工ID',
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    PRIMARY KEY (`kf_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='客服账号表';

-- 转人工记录表
CREATE TABLE IF NOT EXISTS `transfer_log` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '自增ID',
    `session_id` VARCHAR(128) NOT NULL COMMENT '会话ID',
    `trigger_reason` VARCHAR(200) DEFAULT NULL COMMENT '触发原因',
    `trigger_time` DATETIME NOT NULL COMMENT '触发时间',
    `accepted_time` DATETIME DEFAULT NULL COMMENT '客服接入时间',
    `accepted_cs_id` VARCHAR(64) DEFAULT NULL COMMENT '接入的客服ID',
    PRIMARY KEY (`id`),
    INDEX `idx_session_id` (`session_id`),
    INDEX `idx_trigger_time` (`trigger_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='转人工记录表';

-- 快捷回复表 (可选)
CREATE TABLE IF NOT EXISTS `quick_reply` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '自增ID',
    `cs_id` VARCHAR(64) DEFAULT NULL COMMENT '客服ID，为空表示公共',
    `category` VARCHAR(50) DEFAULT NULL COMMENT '分类',
    `content` VARCHAR(500) NOT NULL COMMENT '回复内容',
    `sort_order` INT DEFAULT 0 COMMENT '排序',
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    PRIMARY KEY (`id`),
    INDEX `idx_cs_id` (`cs_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='快捷回复表';

-- 如果表已存在，添加新字段
ALTER TABLE `session` ADD COLUMN IF NOT EXISTS `wx_service_state` INT DEFAULT 0 COMMENT '微信会话状态';
ALTER TABLE `session` ADD COLUMN IF NOT EXISTS `channel_type` VARCHAR(20) DEFAULT 'wechat' COMMENT '渠道类型: wechat/douyin/jd';
