-- ============================================================================
-- 首页入口模块初始化脚本（按老系统真实语义迁移）
-- 用途：
--   1. 初始化首页入口模块
--   2. 绑定现有 leaudit_document_types.entry_module_id
--   3. 对齐老系统“入口模块表只存业务入口 + 图片路径，跳转由前端硬编码”的实际情况
--
-- 当前新系统落地策略：
--   - leaudit_entry_modules.path      = 前端跳转路由
--   - leaudit_entry_modules.icon_path = 入口图标路径
--
-- 老系统真实数据来源（docauditai）：
--   1. 合同管理
--      - 图片：documents/mz/static/img/entry_module_1.png
--      - 文档类型：HT
--      - 实际跳转：/contract-template/search
--   2. 案卷智能评查
--      - 图片：documents/mz/static/img/entry_module_2.png
--      - 文档类型：XZXK, XZCF
--      - 实际跳转：/home
--   3. 内部公文
--      - 图片：documents/mz/static/img/entry_module_15.png
--      - 文档类型：NBGW
--      - 实际跳转：/home
--
-- 另外补齐老前端硬编码入口：
--   4. 智慧法务助手 -> /chat-with-llm/chat
--   5. 交叉评查     -> /cross-checking
-- ============================================================================

BEGIN;

INSERT INTO leaudit_entry_modules (
    name,
    description,
    path,
    icon_path,
    areas,
    sort_order,
    is_enabled,
    created_at,
    updated_at,
    deleted_at
)
VALUES
    (
        '合同管理',
        '合同管理入口模块',
        '/contract-template/search',
        'documents/mz/static/img/entry_module_1.png',
        '[
            {"area":"梅州","enabled":true,"sort_order":1},
            {"area":"云浮","enabled":true,"sort_order":2},
            {"area":"揭阳","enabled":true,"sort_order":3},
            {"area":"潮州","enabled":true,"sort_order":4},
            {"area":"省局","enabled":true,"sort_order":5}
        ]'::jsonb,
        10,
        TRUE,
        CURRENT_TIMESTAMP,
        CURRENT_TIMESTAMP,
        NULL
    ),
    (
        '案卷智能评查',
        '案卷类型的入口模块',
        '/home',
        'documents/mz/static/img/entry_module_2.png',
        '[
            {"area":"梅州","enabled":true,"sort_order":1},
            {"area":"揭阳","enabled":true,"sort_order":2},
            {"area":"云浮","enabled":true,"sort_order":3},
            {"area":"潮州","enabled":true,"sort_order":4},
            {"area":"省局","enabled":true,"sort_order":5}
        ]'::jsonb,
        20,
        TRUE,
        CURRENT_TIMESTAMP,
        CURRENT_TIMESTAMP,
        NULL
    ),
    (
        '内部公文',
        '内部公文',
        '/home',
        'documents/mz/static/img/entry_module_15.png',
        '[
            {"area":"潮州","enabled":true,"sort_order":1},
            {"area":"省局","enabled":true,"sort_order":2},
            {"area":"揭阳","enabled":true,"sort_order":3},
            {"area":"梅州","enabled":true,"sort_order":4},
            {"area":"云浮","enabled":true,"sort_order":5}
        ]'::jsonb,
        30,
        TRUE,
        CURRENT_TIMESTAMP,
        CURRENT_TIMESTAMP,
        NULL
    ),
    (
        '智慧法务助手',
        '大模型法务助手入口',
        '/chat-with-llm/chat',
        '/images/icon_assistant.png',
        NULL,
        40,
        TRUE,
        CURRENT_TIMESTAMP,
        CURRENT_TIMESTAMP,
        NULL
    ),
    (
        '交叉评查',
        '交叉评查任务入口',
        '/cross-checking',
        '/images/icon_cross@2x.png',
        NULL,
        50,
        TRUE,
        CURRENT_TIMESTAMP,
        CURRENT_TIMESTAMP,
        NULL
    )
ON CONFLICT (name) DO UPDATE
SET
    description = EXCLUDED.description,
    path = EXCLUDED.path,
    icon_path = EXCLUDED.icon_path,
    areas = EXCLUDED.areas,
    sort_order = EXCLUDED.sort_order,
    is_enabled = EXCLUDED.is_enabled,
    updated_at = CURRENT_TIMESTAMP,
    deleted_at = NULL;

WITH module_map AS (
    SELECT id, name
    FROM leaudit_entry_modules
    WHERE deleted_at IS NULL
)
UPDATE leaudit_document_types dt
SET
    entry_module_id = mm.id,
    updated_at = CURRENT_TIMESTAMP
FROM module_map mm
WHERE dt.deleted_at IS NULL
  AND (
      (
          mm.name = '合同管理'
          AND dt.code LIKE 'contract.%'
      )
      OR (
          mm.name = '案卷智能评查'
          AND dt.code LIKE '行政卷宗.%'
      )
      OR (
          mm.name = '内部公文'
          AND dt.code IN ('NBGW', 'internal.document')
      )
  );

COMMIT;
