SET NAMES utf8mb4;
SET CHARACTER SET utf8mb4;
-- ============================================================
--  灵枢智能任务平台 —— 演示数据初始化
--  执行前请确保已执行 灵枢数据库设计.sql
-- ============================================================

USE lingshu;

-- ── 1. 用户 ──────────────────────────────────────────────────
INSERT INTO users (id, username, display_name, password_hash, avatar_color) VALUES
(1, 'wangpai',  '王牌参谋', '$2b$12$placeholder_hash', '#3b82f6'),
(2, 'liming',   '李明',     '$2b$12$placeholder_hash', '#8b5cf6'),
(3, 'zhaoqiang','赵强',     '$2b$12$placeholder_hash', '#10b981')
ON DUPLICATE KEY UPDATE display_name=VALUES(display_name);

-- ── 2. 团队 ──────────────────────────────────────────────────
INSERT INTO teams (id, team_code, team_name, description, owner_id) VALUES
(1, 'T-9082', '灵枢作战团队', '负责全域智能任务编排与情报处理', 1)
ON DUPLICATE KEY UPDATE team_name=VALUES(team_name);

-- ── 3. 团队成员 ──────────────────────────────────────────────
INSERT INTO team_members (team_id, user_id, role, is_online) VALUES
(1, 1, 'admin',  1),
(1, 2, 'editor', 1),
(1, 3, 'viewer', 0)
ON DUPLICATE KEY UPDATE role=VALUES(role);

-- ── 4. 数据源 ─────────────────────────────────────────────────
INSERT INTO datasources (owner_id, team_id, name, ds_type, host, database_name, username, credential_enc, connection_status) VALUES
(1, 1, '核心战情数据库 (MySQL)', 'mysql', '192.168.1.100:3306', 'lingshu', 'db_admin', 'cGxhY2Vob2xkZXI=', 'ok')
ON DUPLICATE KEY UPDATE name=VALUES(name);

-- ── 5. 任务 ───────────────────────────────────────────────────
-- 需要先有 agent id，取已插入的 agent
SET @agent_cv = (SELECT id FROM agents WHERE agent_code='a2-cv-target-detect' LIMIT 1);
SET @agent_llm = (SELECT id FROM agents WHERE agent_code='a1-qwen-summarize' LIMIT 1);
SET @agent_db = (SELECT id FROM agents WHERE agent_code='a3-db-statistics' LIMIT 1);

INSERT INTO tasks (owner_id, team_id, name, cron_expression, agent_id, trigger_type, status) VALUES
(1, 1, '边境巡逻无人机数据处理与告警', '0 */2 * * *', @agent_cv,  'cron', 'running'),
(1, 1, '每日战备情报自动分发',           '0 6 * * *',   @agent_llm, 'cron', 'running'),
(1, 1, '某型装备健康度定时评估',         '0 0 * * 0',   @agent_db,  'cron', 'stopped')
ON DUPLICATE KEY UPDATE status=VALUES(status);

-- ── 6. 执行记录与日志（最近几条演示数据） ─────────────────────
SET @task1_id = (SELECT id FROM tasks WHERE name='边境巡逻无人机数据处理与告警' LIMIT 1);

INSERT INTO task_executions (task_id, trigger_type, status, started_at, ended_at, duration_ms, cpu_usage_pct, memory_usage_pct) VALUES
(@task1_id, 'cron', 'success', NOW() - INTERVAL 2 HOUR, NOW() - INTERVAL 2 HOUR + INTERVAL 1 SECOND, 1150, 42.5, 68.3),
(@task1_id, 'cron', 'success', NOW() - INTERVAL 4 HOUR, NOW() - INTERVAL 4 HOUR + INTERVAL 1 SECOND, 1080, 38.2, 65.1),
(@task1_id, 'cron', 'failed',  NOW() - INTERVAL 6 HOUR, NOW() - INTERVAL 6 HOUR + INTERVAL 5 SECOND, NULL, NULL, NULL);

SET @exec1_id = (SELECT id FROM task_executions WHERE task_id=@task1_id ORDER BY started_at DESC LIMIT 1 OFFSET 2);

INSERT INTO task_logs (execution_id, task_id, log_level, log_time, message, node_name) VALUES
(@exec1_id, @task1_id, 'info',    NOW() - INTERVAL 2 HOUR,                      'Task Workflow Triggered',                    'Cron定时触发'),
(@exec1_id, @task1_id, 'info',    NOW() - INTERVAL 2 HOUR + INTERVAL 1 SECOND,  'Initializing node context... OK',            'Cron定时触发'),
(@exec1_id, @task1_id, 'info',    NOW() - INTERVAL 2 HOUR + INTERVAL 2 SECOND,  'Fetching UAV data stream... 32 frames',      '图像目标检测'),
(@exec1_id, @task1_id, 'info',    NOW() - INTERVAL 2 HOUR + INTERVAL 3 SECOND,  'Invoking Vision AI Agent (CV-Model)...',     '图像目标检测'),
(@exec1_id, @task1_id, 'success', NOW() - INTERVAL 2 HOUR + INTERVAL 4 SECOND,  'Agent analysis complete. Targets found: 2', '图像目标检测'),
(@exec1_id, @task1_id, 'info',    NOW() - INTERVAL 2 HOUR + INTERVAL 5 SECOND,  'Writing results to database...',             '写入数据库'),
(@exec1_id, @task1_id, 'success', NOW() - INTERVAL 2 HOUR + INTERVAL 6 SECOND,  'Task workflow finished in 1.15 seconds',     '写入数据库');

-- ── 7. 团队共享资源 ───────────────────────────────────────────
SET @tpl1_id = (SELECT id FROM task_templates LIMIT 1);
-- 仅在有模板时插入
INSERT INTO team_shared_resources (team_id, resource_type, resource_id, resource_name, shared_by, usage_count)
SELECT 1, 'task_template', id, title, 1, 45 FROM task_templates LIMIT 2
ON DUPLICATE KEY UPDATE usage_count=VALUES(usage_count);

-- ── 8. 团队审计日志 ───────────────────────────────────────────
INSERT INTO team_audit_logs (team_id, user_id, action, resource_type) VALUES
(1, 1, '创建了任务「边境巡逻无人机数据处理与告警」', 'task'),
(1, 2, '修改了流程编排「情报分发流程」', 'task'),
(1, 3, '查看了监控日志', 'task_log'),
(1, 1, '发布了模板到任务广场', 'task_template'),
(1, 2, '启动了任务「每日战备情报自动分发」', 'task');

-- ── 9. 团队留言 ───────────────────────────────────────────────
INSERT INTO team_comments (team_id, user_id, content) VALUES
(1, 2, '今天的情报摘要格式需要调整一下，加上来源标注。'),
(1, 1, '已修改模板，增加了一个来源字段节点。');

-- ── 10. 每日统计（供工作台图表） ─────────────────────────────
INSERT INTO daily_stats (stat_date, user_id, total_executions, success_count, fail_count, avg_duration_ms) VALUES
(CURDATE() - INTERVAL 6 DAY, 1, 12, 11, 1, 1200),
(CURDATE() - INTERVAL 5 DAY, 1, 18, 17, 1, 1100),
(CURDATE() - INTERVAL 4 DAY, 1, 15, 15, 0, 980),
(CURDATE() - INTERVAL 3 DAY, 1, 22, 20, 2, 1350),
(CURDATE() - INTERVAL 2 DAY, 1, 19, 18, 1, 1050),
(CURDATE() - INTERVAL 1 DAY, 1, 25, 24, 1, 1080),
(CURDATE(),                   1, 20, 19, 1, 1150)
ON DUPLICATE KEY UPDATE total_executions=VALUES(total_executions), success_count=VALUES(success_count), fail_count=VALUES(fail_count);

SELECT '演示数据初始化完成！' AS result;
