-- 人民法院完整示例数据：应用中心（12个AI Agent）
-- 覆盖立案→送达→庭审→裁判→执行→审判管理→诉讼服务全流程
-- 可重复执行（ON CONFLICT DO NOTHING）

DO $$
DECLARE
  creator_id UUID;
  fayuan_org UUID := 'a0000000-0000-0000-0000-000000000010';
  cat_filing UUID;
  cat_service_doc UUID;
  cat_trial UUID;
  cat_verdict UUID;
  cat_enforce UUID;
  cat_mgmt UUID;
  cat_public UUID;
BEGIN
  SELECT id INTO creator_id FROM users WHERE email = 'lifaguan@govai.gov.cn';
  SELECT id INTO cat_filing      FROM categories WHERE slug = 'fayuan-filing';
  SELECT id INTO cat_service_doc FROM categories WHERE slug = 'fayuan-service';
  SELECT id INTO cat_trial       FROM categories WHERE slug = 'fayuan-trial';
  SELECT id INTO cat_verdict     FROM categories WHERE slug = 'fayuan-verdict';
  SELECT id INTO cat_enforce     FROM categories WHERE slug = 'fayuan-enforcement';
  SELECT id INTO cat_mgmt        FROM categories WHERE slug = 'fayuan-management';
  SELECT id INTO cat_public      FROM categories WHERE slug = 'fayuan-public';

  -- 1. 立案审查助手
  INSERT INTO applications (id, name, slug, description, long_description, icon_url, category_id, creator_id, org_id,
    dify_app_type, status, visibility, is_featured, usage_count, avg_rating, rating_count, version,
    app_config, welcome_message, suggested_prompts, published_at)
  VALUES (
    'f0000000-0000-0000-0000-000000000001',
    '立案审查助手', 'case-filing-review',
    '智能审查起诉材料，判断管辖权、案由定性、材料完整性',
    '立案庭AI辅助工具，根据当事人提交的诉讼材料智能分析：\n- 管辖权审查（级别管辖、地域管辖、专属管辖）\n- 案由自动定性（民事/刑事/行政/执行）\n- 起诉材料完整性检查\n- 诉讼费预计算\n- 立案风险提示',
    '📋', cat_filing, creator_id, fayuan_org,
    'chatbot', 'approved', 'public', true, 256, 4.6, 32, '1.0',
    '{"system_prompt": "你是人民法院立案庭的智能审查助手，精通《民事诉讼法》《刑事诉讼法》《行政诉讼法》关于立案的规定。请根据用户提交的案件信息，从管辖权、案由定性、材料完整性、诉讼费等角度进行审查分析，给出立案建议。", "model": "qwen-plus", "temperature": 0.3, "max_tokens": 4000}',
    '您好，我是立案审查助手。请描述案件基本情况，我将为您进行立案审查分析。',
    '["原告张某起诉被告李某民间借贷纠纷，标的额15万元", "行政诉讼案件，原告对市场监管局处罚决定不服", "执行立案申请，已有生效判决书"]',
    NOW()
  ) ON CONFLICT (id) DO NOTHING;

  -- 2. 送达文书生成
  INSERT INTO applications (id, name, slug, description, long_description, icon_url, category_id, creator_id, org_id,
    dify_app_type, status, visibility, is_featured, usage_count, avg_rating, rating_count, version,
    app_config, welcome_message, published_at)
  VALUES (
    'f0000000-0000-0000-0000-000000000002',
    '送达文书生成', 'service-doc-gen',
    '快速生成传票、应诉通知书、举证通知书等送达文书',
    '根据案件信息自动生成各类送达文书：\n- 受案传票（含开庭时间、地点）\n- 应诉通知书\n- 举证通知书\n- 权利义务告知书\n- 开庭公告',
    '📮', cat_service_doc, creator_id, fayuan_org,
    'workflow', 'approved', 'public', true, 389, 4.7, 41, '1.0',
    '{"system_prompt": "你是法院送达文书生成专家，熟悉各类送达文书的格式和法定要素。请根据案件信息，生成规范、完整的送达文书。", "model": "qwen-plus", "temperature": 0.2, "max_tokens": 6000, "app_type": "workflow", "steps": [{"key": "doc_type", "label": "文书类型", "type": "select", "options": ["传票", "应诉通知书", "举证通知书", "权利义务告知书", "开庭公告"], "required": true}, {"key": "case_info", "label": "案号", "type": "text", "placeholder": "如：(2024)京0101民初123号", "required": true}, {"key": "parties", "label": "当事人信息", "type": "textarea", "placeholder": "原告/被告姓名、地址、联系方式", "required": true}, {"key": "court_info", "label": "开庭信息", "type": "text", "placeholder": "开庭时间、地点、审判人员"}, {"key": "special_note", "label": "特别说明", "type": "textarea", "placeholder": "其他需要载明的事项"}]}',
    '请选择文书类型并填写案件信息，我将为您生成规范的送达文书。',
    NOW()
  ) ON CONFLICT (id) DO NOTHING;

  -- 3. 庭前会议纪要
  INSERT INTO applications (id, name, slug, description, long_description, icon_url, category_id, creator_id, org_id,
    dify_app_type, status, visibility, is_featured, usage_count, avg_rating, rating_count, version,
    app_config, welcome_message, published_at)
  VALUES (
    'f0000000-0000-0000-0000-000000000003',
    '庭前会议纪要', 'pretrial-meeting',
    '整理争议焦点、确认无争议事实、记录证据交换',
    '庭前会议AI辅助工具，帮助法官高效完成庭前准备：\n- 自动归纳争议焦点\n- 梳理无争议事实\n- 证据交换记录与整理\n- 举证责任分配建议\n- 庭审提纲生成',
    '📝', cat_trial, creator_id, fayuan_org,
    'workflow', 'approved', 'public', true, 178, 4.5, 23, '1.0',
    '{"system_prompt": "你是法院庭前会议辅助专家，精通庭前会议规程。请根据案件信息，整理争议焦点、无争议事实、证据交换情况，生成规范的庭前会议纪要和庭审提纲。", "model": "qwen-plus", "temperature": 0.3, "max_tokens": 5000, "app_type": "workflow", "steps": [{"key": "case_brief", "label": "案件简述", "type": "textarea", "placeholder": "案由、当事人、诉讼请求、事实概要", "required": true}, {"key": "plaintiff_evidence", "label": "原告证据", "type": "textarea", "placeholder": "原告提交的证据清单及证明目的"}, {"key": "defendant_evidence", "label": "被告证据", "type": "textarea", "placeholder": "被告提交的证据清单及证明目的"}, {"key": "focus_points", "label": "初步争议焦点", "type": "textarea", "placeholder": "法官初步归纳的争议焦点（可选）"}]}',
    '请输入案件信息和双方证据情况，我将为您生成庭前会议纪要。',
    NOW()
  ) ON CONFLICT (id) DO NOTHING;

  -- 4. 庭审笔录生成
  INSERT INTO applications (id, name, slug, description, long_description, icon_url, category_id, creator_id, org_id,
    dify_app_type, status, visibility, is_featured, usage_count, avg_rating, rating_count, version,
    app_config, welcome_message, published_at)
  VALUES (
    'f0000000-0000-0000-0000-000000000004',
    '庭审笔录生成', 'trial-record-gen',
    '根据庭审要点快速生成规范庭审笔录',
    '庭审记录AI辅助工具，根据庭审过程要点生成规范笔录：\n- 庭审全过程结构化记录\n- 当事人陈述、举证质证、辩论内容整理\n- 法庭调查、法庭辩论阶段分明\n- 自动补充法定程序性记载\n- 支持民事/刑事/行政三类庭审',
    '⚖️', cat_trial, creator_id, fayuan_org,
    'workflow', 'approved', 'public', true, 467, 4.8, 56, '1.0',
    '{"system_prompt": "你是法院庭审记录专家，精通各类案件庭审笔录的规范格式。请根据用户提供的庭审要点，生成结构完整、内容准确的庭审笔录。", "model": "qwen-plus", "temperature": 0.2, "max_tokens": 8000, "app_type": "workflow", "steps": [{"key": "case_type", "label": "案件类型", "type": "select", "options": ["民事案件", "刑事案件", "行政案件"], "required": true}, {"key": "case_info", "label": "案件基本信息", "type": "text", "placeholder": "案号、案由、当事人、审判人员", "required": true}, {"key": "hearing_points", "label": "庭审要点", "type": "textarea", "placeholder": "当事人陈述要点、举证质证情况、辩论焦点等", "required": true}, {"key": "special_notes", "label": "特别记录事项", "type": "textarea", "placeholder": "回避申请、延期审理等特殊情况"}]}',
    '请输入案件类型和庭审要点，我将为您生成规范的庭审笔录。',
    NOW()
  ) ON CONFLICT (id) DO NOTHING;

  -- 5. 裁判文书生成
  INSERT INTO applications (id, name, slug, description, long_description, icon_url, category_id, creator_id, org_id,
    dify_app_type, status, visibility, is_featured, usage_count, avg_rating, rating_count, version,
    app_config, welcome_message, suggested_prompts, published_at)
  VALUES (
    'f0000000-0000-0000-0000-000000000005',
    '裁判文书生成', 'verdict-writer',
    '智能生成判决书、裁定书、调解书，自动引用法条',
    '裁判文书AI辅助生成工具，根据案件审理情况生成规范文书：\n- 民事/刑事/行政判决书\n- 裁定书（驳回起诉、管辖异议等）\n- 调解书\n- 自动引用相关法条和司法解释\n- 争议焦点分析与裁判说理',
    '📄', cat_verdict, creator_id, fayuan_org,
    'workflow', 'approved', 'public', true, 612, 4.9, 78, '1.0',
    '{"system_prompt": "你是人民法院裁判文书撰写专家，精通各类裁判文书的规范格式和说理要求。请根据案件审理情况，生成结构完整、说理充分、法条引用准确的裁判文书。文书应包含：当事人信息、案件由来、审理经过、查明事实、裁判理由、法律依据、裁判主文。", "model": "qwen-plus", "temperature": 0.2, "max_tokens": 8000, "app_type": "workflow", "steps": [{"key": "doc_type", "label": "文书类型", "type": "select", "options": ["民事判决书", "刑事判决书", "行政判决书", "裁定书", "调解书"], "required": true}, {"key": "case_info", "label": "案件基本信息", "type": "text", "placeholder": "案号、案由、当事人、审判人员", "required": true}, {"key": "facts", "label": "查明事实", "type": "textarea", "placeholder": "经审理查明的事实", "required": true}, {"key": "arguments", "label": "双方主张", "type": "textarea", "placeholder": "原告诉称、被告辩称及争议焦点"}, {"key": "law_basis", "label": "法律依据", "type": "textarea", "placeholder": "拟引用的法条（可选，AI可自动补充）"}]}',
    '请选择文书类型并填写案件信息，我将为您生成规范的裁判文书。',
    '["民间借贷纠纷，原告借款10万元未还，有借条和转账记录", "离婚纠纷，双方同意离婚但争议财产分割", "交通事故责任认定，被告对责任划分有异议"]',
    NOW()
  ) ON CONFLICT (id) DO NOTHING;

  -- 6. 案件智能分析
  INSERT INTO applications (id, name, slug, description, long_description, icon_url, category_id, creator_id, org_id,
    dify_app_type, status, visibility, is_featured, usage_count, avg_rating, rating_count, version,
    app_config, welcome_message, suggested_prompts, published_at)
  VALUES (
    'f0000000-0000-0000-0000-000000000006',
    '案件智能分析', 'case-analysis-court',
    '归纳争议焦点、类案推送、裁判思路参考',
    '案件分析AI助手，辅助法官深入研究案件：\n- 争议焦点自动归纳\n- 类案智能推送（相似案情裁判结果）\n- 裁判思路参考与说理建议\n- 法律关系梳理\n- 证据关联性分析',
    '🔍', cat_verdict, creator_id, fayuan_org,
    'chatbot', 'approved', 'public', true, 423, 4.7, 52, '1.0',
    '{"system_prompt": "你是人民法院案件分析专家，精通各类案件的法律适用和裁判规则。请根据用户描述的案件信息，从争议焦点归纳、法律关系梳理、类案参考、裁判思路等方面进行专业分析。注意：分析仅供参考，不构成裁判意见。", "model": "qwen-plus", "temperature": 0.4, "max_tokens": 4000}',
    '您好，我是案件智能分析助手。请描述案件基本情况，我将为您提供分析参考。',
    '["买卖合同纠纷，买方收货后以质量不合格拒付尾款", "劳动争议案件，员工主张违法解除劳动合同赔偿金", "房屋租赁合同纠纷，承租方提前退租要求退还押金"]',
    NOW()
  ) ON CONFLICT (id) DO NOTHING;

  -- 7. 诉讼风险评估
  INSERT INTO applications (id, name, slug, description, long_description, icon_url, category_id, creator_id, org_id,
    dify_app_type, status, visibility, is_featured, usage_count, avg_rating, rating_count, version,
    app_config, welcome_message, published_at)
  VALUES (
    'f0000000-0000-0000-0000-000000000007',
    '诉讼风险评估', 'litigation-risk',
    '评估诉讼胜负概率，辅助诉前调解和判调结合',
    '诉讼风险智能评估工具，帮助法官和当事人预判诉讼风险：\n- 胜负概率初步评估\n- 证据充分性分析\n- 法律风险点提示\n- 调解可行性建议\n- 类案裁判结果参考',
    '📊', cat_verdict, creator_id, fayuan_org,
    'workflow', 'approved', 'public', false, 234, 4.4, 28, '1.0',
    '{"system_prompt": "你是诉讼风险评估专家，精通各类案件的风险分析。请根据案件信息，从证据、法律适用、类案参考等角度评估诉讼风险，给出胜负概率和调解建议。注意：评估仅供参考，不构成法律意见。", "model": "qwen-plus", "temperature": 0.3, "max_tokens": 4000, "app_type": "workflow", "steps": [{"key": "case_type", "label": "案件类型", "type": "select", "options": ["民间借贷", "合同纠纷", "劳动争议", "婚姻家庭", "交通事故", "行政争议"], "required": true}, {"key": "case_desc", "label": "案情描述", "type": "textarea", "placeholder": "详细描述案件事实和争议焦点", "required": true}, {"key": "evidence", "label": "现有证据", "type": "textarea", "placeholder": "列出各方持有的证据"}, {"key": "claim", "label": "诉讼请求", "type": "textarea", "placeholder": "原告的具体诉讼请求"}]}',
    '请输入案件信息，我将为您评估诉讼风险。',
    NOW()
  ) ON CONFLICT (id) DO NOTHING;

  -- 8. 执行文书生成
  INSERT INTO applications (id, name, slug, description, long_description, icon_url, category_id, creator_id, org_id,
    dify_app_type, status, visibility, is_featured, usage_count, avg_rating, rating_count, version,
    app_config, welcome_message, published_at)
  VALUES (
    'f0000000-0000-0000-0000-000000000008',
    '执行文书生成', 'enforcement-doc',
    '生成执行通知书、裁定书、失信曝光公告等执行文书',
    '执行局AI辅助工具，快速生成各类执行文书：\n- 执行通知书\n- 报告财产令\n- 限制消费令\n- 失信曝光公告\n- 执行裁定书（查封/冻结/扣划）',
    '⚡', cat_enforce, creator_id, fayuan_org,
    'workflow', 'approved', 'public', true, 345, 4.6, 37, '1.0',
    '{"system_prompt": "你是法院执行文书生成专家，精通执行程序和各类执行文书的规范格式。请根据案件信息生成规范、完整的执行文书，确保包含法定要素。", "model": "qwen-plus", "temperature": 0.2, "max_tokens": 6000, "app_type": "workflow", "steps": [{"key": "doc_type", "label": "文书类型", "type": "select", "options": ["执行通知书", "报告财产令", "限制消费令", "失信曝光公告", "执行裁定书"], "required": true}, {"key": "case_info", "label": "执行案号", "type": "text", "placeholder": "如：(2024)京0101执123号", "required": true}, {"key": "parties", "label": "当事人信息", "type": "textarea", "placeholder": "申请执行人/被执行人信息", "required": true}, {"key": "exec_content", "label": "执行内容", "type": "textarea", "placeholder": "执行标的、执行措施等", "required": true}]}',
    '请选择文书类型并填写执行案件信息，我将为您生成执行文书。',
    NOW()
  ) ON CONFLICT (id) DO NOTHING;

  -- 9. 案件评查助手
  INSERT INTO applications (id, name, slug, description, long_description, icon_url, category_id, creator_id, org_id,
    dify_app_type, status, visibility, is_featured, usage_count, avg_rating, rating_count, version,
    app_config, welcome_message, suggested_prompts, published_at)
  VALUES (
    'f0000000-0000-0000-0000-000000000009',
    '案件评查助手', 'case-quality-review',
    '检查文书规范性、程序合规性、裁判一致性',
    '审判管理AI辅助工具，助力案件质量评查：\n- 文书格式规范性检查\n- 程序合规性审查\n- 裁判尺度一致性分析\n- 法条引用准确性校验\n- 评查报告自动生成',
    '✅', cat_mgmt, creator_id, fayuan_org,
    'chatbot', 'approved', 'public', false, 156, 4.5, 19, '1.0',
    '{"system_prompt": "你是法院案件质量评查专家，精通审判管理规范和评查标准。请根据用户提供的案件信息或文书内容，从格式规范、程序合规、裁判一致性、法条引用等方面进行评查分析。", "model": "qwen-plus", "temperature": 0.3, "max_tokens": 4000}',
    '您好，我是案件评查助手。请提供案件文书内容，我将为您进行质量评查分析。',
    '["检查一份民事判决书的格式和法条引用是否规范", "审查案件审理程序是否合规", "分析同类案件裁判尺度是否一致"]',
    NOW()
  ) ON CONFLICT (id) DO NOTHING;

  -- 10. 司法统计报告
  INSERT INTO applications (id, name, slug, description, long_description, icon_url, category_id, creator_id, org_id,
    dify_app_type, status, visibility, is_featured, usage_count, avg_rating, rating_count, version,
    app_config, welcome_message, published_at)
  VALUES (
    'f0000000-0000-0000-0000-000000000010',
    '司法统计报告', 'judicial-statistics',
    '生成收结案统计、审判质效分析、态势研判报告',
    '审判管理办AI辅助工具，自动生成司法统计报告：\n- 收结案统计报告\n- 审判质效分析报告\n- 案件类型分布分析\n- 审判态势研判\n- 法官绩效数据汇总',
    '📈', cat_mgmt, creator_id, fayuan_org,
    'workflow', 'approved', 'public', true, 289, 4.6, 31, '1.0',
    '{"system_prompt": "你是法院司法统计专家，精通审判质效评估指标体系和统计分析方法。请根据用户提供的数据，生成结构清晰、分析深入的司法统计报告。", "model": "qwen-plus", "temperature": 0.3, "max_tokens": 6000, "app_type": "workflow", "steps": [{"key": "report_type", "label": "报告类型", "type": "select", "options": ["收结案统计", "审判质效分析", "案件类型分布", "审判态势研判", "法官绩效汇总"], "required": true}, {"key": "period", "label": "统计周期", "type": "text", "placeholder": "如：2024年第一季度", "required": true}, {"key": "data", "label": "统计数据", "type": "textarea", "placeholder": "收案数、结案数、未结数、调解率等数据", "required": true}, {"key": "focus", "label": "重点关注", "type": "textarea", "placeholder": "需要重点分析的指标或问题"}]}',
    '请选择报告类型并输入统计数据，我将为您生成司法统计报告。',
    NOW()
  ) ON CONFLICT (id) DO NOTHING;

  -- 11. 诉讼服务问答
  INSERT INTO applications (id, name, slug, description, long_description, icon_url, category_id, creator_id, org_id,
    dify_app_type, status, visibility, is_featured, usage_count, avg_rating, rating_count, version,
    app_config, welcome_message, suggested_prompts, published_at)
  VALUES (
    'f0000000-0000-0000-0000-000000000011',
    '诉讼服务问答', 'litigation-service-qa',
    '面向当事人：立案流程、诉讼费、执行进度等常见问题解答',
    '诉讼服务中心AI助手，为当事人提供智能问答服务：\n- 立案条件和流程指引\n- 诉讼费计算与缓减免政策\n- 开庭公告查询指引\n- 执行进度查询指引\n- 诉讼风险提示与法律释明',
    '🎧', cat_public, creator_id, fayuan_org,
    'chatbot', 'approved', 'public', true, 578, 4.7, 64, '1.0',
    '{"system_prompt": "你是法院诉讼服务中心的智能问答助手，熟悉立案、审判、执行各环节的办事流程。请用通俗易懂的语言为当事人解答诉讼相关问题，提供准确的办事指引。注意：不提供具体法律意见，仅做程序性指引。", "model": "qwen-plus", "temperature": 0.4, "max_tokens": 3000}',
    '您好，我是诉讼服务问答助手。请问您想了解立案、诉讼费、执行还是其他问题？',
    '["我想起诉别人借钱不还，需要准备什么材料？", "诉讼费怎么计算？可以申请缓交吗？", "我的案件已经判决了，怎么申请执行？"]',
    NOW()
  ) ON CONFLICT (id) DO NOTHING;

  -- 12. 信访接待助手
  INSERT INTO applications (id, name, slug, description, long_description, icon_url, category_id, creator_id, org_id,
    dify_app_type, status, visibility, is_featured, usage_count, avg_rating, rating_count, version,
    app_config, welcome_message, suggested_prompts, published_at)
  VALUES (
    'f0000000-0000-0000-0000-000000000012',
    '信访接待助手', 'petition-assistant',
    '信访分类引导、法律释明、转办建议',
    '信访接待AI辅助工具，助力信访工作规范化：\n- 信访事项智能分类（诉讼/非诉讼/执行/纪检）\n- 法律释明与政策解读\n- 信访转办建议\n- 信访事项登记辅助\n- 情绪安抚话术建议',
    '🗣️', cat_public, creator_id, fayuan_org,
    'chatbot', 'approved', 'public', false, 167, 4.3, 21, '1.0',
    '{"system_prompt": "你是法院信访接待助手，熟悉信访工作规程和涉诉信访处理流程。请根据信访人反映的问题，进行智能分类、法律释明，并给出转办建议。注意用语规范、态度平和，做好情绪疏导。", "model": "qwen-plus", "temperature": 0.4, "max_tokens": 3000}',
    '您好，我是信访接待助手。请描述您反映的问题，我将为您进行分类引导和法律释明。',
    '["对判决结果不服，想申诉", "案件执行不到位，被执行人转移财产", "法官审理案件程序有问题，想投诉"]',
    NOW()
  ) ON CONFLICT (id) DO NOTHING;

END $$;
