-- 1. 创建 agent_user 表
CREATE TABLE IF NOT EXISTS agent_user (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    username VARCHAR(255) UNIQUE NOT NULL,
    email VARCHAR(255) UNIQUE,
    password_hash VARCHAR(255) NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    last_login TIMESTAMP WITH TIME ZONE,
    is_active BOOLEAN DEFAULT TRUE,
    is_admin BOOLEAN DEFAULT FALSE
);

-- agent_user 索引
CREATE INDEX IF NOT EXISTS idx_agent_user_username ON agent_user(username);
CREATE INDEX IF NOT EXISTS idx_agent_user_email ON agent_user(email);
CREATE INDEX IF NOT EXISTS idx_agent_user_is_active ON agent_user(is_active);

-- 2. 创建 agent_bots 表
CREATE TABLE IF NOT EXISTS agent_bots (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    name VARCHAR(255) NOT NULL,
    bot_id VARCHAR(255) NOT NULL UNIQUE,
    settings JSONB DEFAULT '{"language": "zh", "enable_memori": false, "enable_thinking": false, "tool_response": false}'::jsonb,
    owner_id UUID NOT NULL REFERENCES agent_user(id) ON DELETE RESTRICT,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- agent_bots 索引
CREATE INDEX IF NOT EXISTS idx_agent_bots_bot_id ON agent_bots(bot_id);
CREATE INDEX IF NOT EXISTS idx_agent_bots_owner_id ON agent_bots(owner_id);

-- 3. 创建 agent_user_tokens 表
CREATE TABLE IF NOT EXISTS agent_user_tokens (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID NOT NULL REFERENCES agent_user(id) ON DELETE CASCADE,
    token VARCHAR(255) NOT NULL UNIQUE,
    expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- agent_user_tokens 索引
CREATE INDEX IF NOT EXISTS idx_agent_user_tokens_token ON agent_user_tokens(token);
CREATE INDEX IF NOT EXISTS idx_agent_user_tokens_user_id ON agent_user_tokens(user_id);
CREATE INDEX IF NOT EXISTS idx_agent_user_tokens_expires ON agent_user_tokens(expires_at);

-- 4. 创建 agent_admin_tokens 表
CREATE TABLE IF NOT EXISTS agent_admin_tokens (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    username VARCHAR(255) NOT NULL,
    token VARCHAR(255) NOT NULL UNIQUE,
    expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- agent_admin_tokens 索引
CREATE INDEX IF NOT EXISTS idx_agent_admin_tokens_token ON agent_admin_tokens(token);
CREATE INDEX IF NOT EXISTS idx_agent_admin_tokens_expires ON agent_admin_tokens(expires_at);

-- 5. 创建 agent_models 表
CREATE TABLE IF NOT EXISTS agent_models (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    name VARCHAR(255) NOT NULL,
    provider VARCHAR(100) NOT NULL,
    model VARCHAR(255) NOT NULL,
    server VARCHAR(500),
    api_key VARCHAR(500),
    is_default BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- agent_models 索引
CREATE INDEX IF NOT EXISTS idx_agent_models_is_default ON agent_models(is_default);

-- 6. 创建 agent_mcp_servers 表
CREATE TABLE IF NOT EXISTS agent_mcp_servers (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    bot_id UUID REFERENCES agent_bots(id) ON DELETE CASCADE,
    name VARCHAR(255) NOT NULL,
    type VARCHAR(50) NOT NULL,
    config JSONB NOT NULL,
    enabled BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- agent_mcp_servers 索引
CREATE INDEX IF NOT EXISTS idx_agent_mcp_servers_bot_id ON agent_mcp_servers(bot_id);
CREATE INDEX IF NOT EXISTS idx_agent_mcp_servers_enabled ON agent_mcp_servers(enabled);

-- 7. 创建 agent_chat_sessions 表
CREATE TABLE IF NOT EXISTS agent_chat_sessions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    bot_id UUID REFERENCES agent_bots(id) ON DELETE CASCADE,
    title VARCHAR(500),
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- agent_chat_sessions 索引
CREATE INDEX IF NOT EXISTS idx_agent_chat_sessions_bot_id ON agent_chat_sessions(bot_id);
CREATE INDEX IF NOT EXISTS idx_agent_chat_sessions_created ON agent_chat_sessions(created_at DESC);

-- 8. 创建 bot_shares 表
CREATE TABLE IF NOT EXISTS bot_shares (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    bot_id UUID NOT NULL REFERENCES agent_bots(id) ON DELETE CASCADE,
    user_id UUID NOT NULL REFERENCES agent_user(id) ON DELETE CASCADE,
    shared_by UUID NOT NULL REFERENCES agent_user(id) ON DELETE SET NULL,
    role VARCHAR(50) DEFAULT 'viewer' CHECK (role IN ('viewer', 'editor')),
    shared_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    expires_at TIMESTAMP WITH TIME ZONE,
    UNIQUE(bot_id, user_id)
);

-- bot_shares 索引
CREATE INDEX IF NOT EXISTS idx_bot_shares_bot_id ON bot_shares(bot_id);
CREATE INDEX IF NOT EXISTS idx_bot_shares_user_id ON bot_shares(user_id);
CREATE INDEX IF NOT EXISTS idx_bot_shares_shared_by ON bot_shares(shared_by);

-- 9. 创建 user_datasets 表（用户与 RAGFlow 数据集的关联表）
CREATE TABLE IF NOT EXISTS user_datasets (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID NOT NULL REFERENCES agent_user(id) ON DELETE CASCADE,
    dataset_id VARCHAR(255) NOT NULL,              -- RAGFlow 返回的 dataset_id
    dataset_name VARCHAR(255),                      -- 冗余存储数据集名称，方便查询
    owner BOOLEAN DEFAULT TRUE,                     -- 是否为所有者（预留分享功能）
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    UNIQUE(user_id, dataset_id)
);

-- user_datasets 索引
CREATE INDEX IF NOT EXISTS idx_user_datasets_user_id ON user_datasets(user_id);
CREATE INDEX IF NOT EXISTS idx_user_datasets_dataset_id ON user_datasets(dataset_id);

-- ===========================
-- 默认 Admin 账号
-- 用户名: admin
-- 密码: admin123
-- ===========================
INSERT INTO agent_user (id, username, email, password_hash, is_active, is_admin)
VALUES (
    '00000000-0000-0000-0000-000000000001',
    'admin',
    'admin@local',
    '240be518fabd2724ddb6f04eeb1da5967448d7e831c08c8fa822809f74c720a9',
    TRUE,
    TRUE
) ON CONFLICT (username) DO NOTHING;
