-- Memory System Database Initialization
CREATE DATABASE IF NOT EXISTS memory_system DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE memory_system;

-- Teams table
CREATE TABLE IF NOT EXISTS teams (
    id VARCHAR(64) PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    description TEXT,
    config JSON,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Agents table
CREATE TABLE IF NOT EXISTS agents (
    id VARCHAR(64) PRIMARY KEY,
    team_id VARCHAR(64) NOT NULL,
    name VARCHAR(255) NOT NULL,
    role VARCHAR(255),
    config JSON,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE CASCADE,
    INDEX idx_team (team_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Personal long-term memories
CREATE TABLE IF NOT EXISTS personal_memories (
    id VARCHAR(64) PRIMARY KEY,
    agent_id VARCHAR(64) NOT NULL,
    team_id VARCHAR(64) NOT NULL,
    content TEXT NOT NULL,
    summary TEXT,
    embedding BLOB,
    importance FLOAT DEFAULT 0.5,
    memory_type VARCHAR(32) DEFAULT 'long_term',
    metadata JSON,
    access_count INT DEFAULT 0,
    last_accessed TIMESTAMP NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (agent_id) REFERENCES agents(id) ON DELETE CASCADE,
    FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE CASCADE,
    INDEX idx_agent (agent_id),
    INDEX idx_team (team_id),
    INDEX idx_importance (importance),
    INDEX idx_created (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Team shared memories
CREATE TABLE IF NOT EXISTS team_memories (
    id VARCHAR(64) PRIMARY KEY,
    team_id VARCHAR(64) NOT NULL,
    content TEXT NOT NULL,
    summary TEXT,
    embedding BLOB,
    importance FLOAT DEFAULT 0.5,
    category VARCHAR(64) DEFAULT 'general',
    metadata JSON,
    access_count INT DEFAULT 0,
    last_accessed TIMESTAMP NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE CASCADE,
    INDEX idx_team (team_id),
    INDEX idx_category (category),
    INDEX idx_importance (importance),
    INDEX idx_created (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

FLUSH PRIVILEGES;

-- ============================================================
-- v3.0 新增表 - API Keys 认证
-- ============================================================
CREATE TABLE IF NOT EXISTS api_keys (
    id VARCHAR(64) PRIMARY KEY,
    team_id VARCHAR(64) NOT NULL,
    api_key VARCHAR(128) NOT NULL UNIQUE,
    secret VARCHAR(128) NOT NULL,
    name VARCHAR(255) DEFAULT '',
    is_active TINYINT DEFAULT 1,
    last_used TIMESTAMP NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE CASCADE,
    INDEX idx_team (team_id),
    INDEX idx_api_key (api_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ============================================================
-- v3.0 新增表 - Pipeline 自动化配置
-- ============================================================
CREATE TABLE IF NOT EXISTS pipeline_config (
    id INT AUTO_INCREMENT PRIMARY KEY,
    team_id VARCHAR(64) NOT NULL UNIQUE,
    compress_every_n INT DEFAULT 0,
    compress_target_count INT DEFAULT 5,
    cleanup_idle_days INT DEFAULT 0,
    cleanup_min_importance FLOAT DEFAULT 0.2,
    enabled TINYINT DEFAULT 1,
    warmup_max_memories INT DEFAULT 0,
    warmup_compress_every_n INT DEFAULT 1,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE CASCADE,
    INDEX idx_team (team_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ============================================================
-- v3.0 新增表 - 场景块（LLM 聚合）
-- ============================================================
CREATE TABLE IF NOT EXISTS memory_scenarios (
    id INT AUTO_INCREMENT PRIMARY KEY,
    team_id VARCHAR(64) NOT NULL,
    agent_id VARCHAR(64) NOT NULL,
    name VARCHAR(255),
    summary TEXT,
    memory_ids JSON,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE CASCADE,
    FOREIGN KEY (agent_id) REFERENCES agents(id) ON DELETE CASCADE,
    INDEX idx_agent (team_id, agent_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ============================================================
-- v3.0 新增表 - 用户画像（LLM 生成）
-- ============================================================
CREATE TABLE IF NOT EXISTS user_personas (
    id INT AUTO_INCREMENT PRIMARY KEY,
    team_id VARCHAR(64) NOT NULL,
    agent_id VARCHAR(64) NOT NULL,
    preferences JSON,
    habits JSON,
    expertise JSON,
    communication_style TEXT,
    summary TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE CASCADE,
    FOREIGN KEY (agent_id) REFERENCES agents(id) ON DELETE CASCADE,
    INDEX idx_agent (team_id, agent_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
